4 ms·
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
by ssivark 8y ago
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 https://github.com/google/python-fire
It would have shaved off another 15-20 lines from the 503 line example ;-)
- lapinot 8y agoYeah, 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).
- rob74 8y agoI guess the author wanted the code to also look friendly to people who are not that familiar with Python - everybody understands a switch, while what you are describing would probably puzzle some people...
- bibyte 8y agoPython Fire looks much more concise. But do you know of any other languages that handles argument parsing better then Python ?
- agigao 8y agoClojure.
- bibyte 8y agoCan you please post a hello world example ?
- vbsteven 8y agoInstalled 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.
- atoav 8y agoHave you tried looking at docopt? There are cases where it is not flexible enough but it is good for quick and dirty little scripts because: * you use the docstring to generate the args. This way you always have a minimum usage that is valid * Argument parsing is a bit more limited than doimg it yourself, but everything you usually need is there * Docopt is exists not only for Python, but many other languages implemented it too.
- singingfish 8y agoPerl has the problem of too many ways to do it. I like MooX::Options. Example here: https://metacpan.org/source/GETTY/AquariumHive-0.003/lib/App/AquariumHive.pm https://metacpan.org/source/GETTY/AquariumHive-0.003/lib/App...
- mcqueenjordan 8y agoI'm a huge fan of the Click library. Click => command line interface creation kit. Ironic little name...
- eadan 8y agoWhen 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 https://docs.python.org/3/library/argparse.html#sub-commands
- aepiepaey 8y agoI largely find argparse to be OK apart from a couple of issues. First, it allows the user to abbreviate flags. They can pass --fl and it will be interpreted as --flag, assuming no other flag shares the same prefix. This sucks for maintainability: add a new flag and any abbreviation for a previously existing flag that shares the same prefix will now stop working, breaking user workflows. Since Python 3.5 there's the allow_abbrev parameter that allows disabling this behaviour, but then you also lose the ability to combine multiple single-character flags (so you can't pass e.g. '-Ev' any more, and would have to pass '-E -v' instead[1]. The other issue is that it's tedious to keep all the .add_argument calls readable, while maintaining a reasonable maximum line length. [1]: https://bugs.python.org/issue26967 https://bugs.python.org/issue26967
- giancarlostoro 8y agoReminds me of CherryPy. An object oriented app can become a web server with a few function decorators (to make them public endpoints). Coincidentally still my favorite Python web framework. https://cherrypy.org/ https://cherrypy.org/
- Myrmornis 8y agodocopt deserves a link in this thread: https://github.com/docopt/docopt https://github.com/docopt/docopt It’s a magical idea if you haven’t seen it. You just write the help text and it automatically creates the argument parsing code.
- gibsjose 8y agoMagical is truly the word to describe Docopt. I use it for all Python code I write and have also used the C and C++ variants. I would highly recommend Docopt to anyone writing command-line utilities.
- oweiler 8y agoFor Java (or any other JVM language), nothing beats https://picocli.info/ https://picocli.info/. Works well with GraalVM, too.
- justbaker 8y agoNice! Thanks for sharing.