Live data from Hacker News

Write yourself a Git (2018)

wyag.thb.lt

11–20 of 111 posts

Re: Write yourself a Git (2018)

#11
post #9

I have nothing to directly comment on the tutorial. Just a tangential mention regarding the tedious argument parsing boilerplate in Python, I have found Python Fire to be much more convenient: https://github.com/google/python-fire It would have shaved off another 15-20 lines from the 503 line example ;-)

Yeah, i couldn't help but notice the first piece of code is an ugly "switch case". There's a python idiom for this, it's putting your functions in a dictionary and doing something like `cmds_dict.get(args.command, default)(args)`. I guess we all have our religious habits for argument parsing (more of a docopt-er myself).

Re: Write yourself a Git (2018)

#12
post #9

I have nothing to directly comment on the tutorial. Just a tangential mention regarding the tedious argument parsing boilerplate in Python, I have found Python Fire to be much more convenient: https://github.com/google/python-fire It would have shaved off another 15-20 lines from the 503 line example ;-)

Python Fire looks much more concise. But do you know of any other languages that handles argument parsing better then Python ?

Re: Write yourself a Git (2018)

#14
post #12
post #9

I have nothing to directly comment on the tutorial. Just a tangential mention regarding the tedious argument parsing boilerplate in Python, I have found Python Fire to be much more convenient: https://github.com/google/python-fire It would have shaved off another 15-20 lines from the 503 line example ;-)

Python Fire looks much more concise. But do you know of any other languages that handles argument parsing better then Python ?

Clojure.

Re: Write yourself a Git (2018)

#15
post #12
post #9

I have nothing to directly comment on the tutorial. Just a tangential mention regarding the tedious argument parsing boilerplate in Python, I have found Python Fire to be much more convenient: https://github.com/google/python-fire It would have shaved off another 15-20 lines from the 503 line example ;-)

Python Fire looks much more concise. But do you know of any other languages that handles argument parsing better then Python ?

Installed by default I cannot find any. But pretty much any language with a package manager has sane and easy to use libraries to bang out a small CLI app with subcommands and option parsing in minutes. For example PicoCli in Java, Thor in Ruby and Clap in Rust.

Re: Write yourself a Git (2018)

#17
post #9

I have nothing to directly comment on the tutorial. Just a tangential mention regarding the tedious argument parsing boilerplate in Python, I have found Python Fire to be much more convenient: https://github.com/google/python-fire It would have shaved off another 15-20 lines from the 503 line example ;-)

I'm a huge fan of the Click library.

Click => command line interface creation kit.

Ironic little name...

Re: Write yourself a Git (2018)

#19
post #9

I have nothing to directly comment on the tutorial. Just a tangential mention regarding the tedious argument parsing boilerplate in Python, I have found Python Fire to be much more convenient: https://github.com/google/python-fire It would have shaved off another 15-20 lines from the 503 line example ;-)

When using argparse, I find subparsers [1] useful in these situations.

  import argparse
  
  def add_file(file):
      print('Added ' + file)
  
  def main():
  
      parser = argparse.ArgumentParser()
      subparsers = parser.add_subparsers(title='Sub Commands')
  
      # add parser
      add_parser = subparsers.add_parser('add', help='Add a file')
      add_parser.add_argument('file', help='File to add')
      add_parser.set_defaults(func=lambda args: add_file(args.file))
  
      args = parser.parse_args()
      args.func(args)

[1] https://docs.python.org/3/library/argparse.html#sub-commands
Post reply on HN