4 ms·
I always found argparse did argument parsing well enough but it felt clunky when you need something more complicated like lots of subcommands. I find myself usi
by onei 6y ago
I always found argparse did argument parsing well enough but it felt clunky when you need something more complicated like lots of subcommands. I find myself using it exclusively when I'm trying to avoid dependencies outside the Python standard library.
My choice of argument parsing in Python is Click. It has next to no rough edges and it's a breath of fresh air compared to argparse. I recently recommended it to a colleague who fell in love with it with minimal persuasion from me. I recommend it highly.
[1] https://click.palletsprojects.com/en/7.x/ https://click.palletsprojects.com/en/7.x/
- cb321 6y agoI feel like argh and plac preceded/inspired Click. Also, it's not Python but in Nim there is https://github.com/c-blake/cligen https://github.com/c-blake/cligen which also does spellcheck/typo suggestions, and allows --kebab-case --camelCase or --snake_case for long options, among other bells & whistles.
- onei 6y agoSpell check is something I'd love to see in Click. As complicated as git can be, I always liked the spell check it has for subcommands. As for the different cases, I personally avoid using camel or snake case purely because I don't need to reach for the shift key. Maybe some people like it, but I find it jarring.
- cb321 6y agoAgreed vis a vis SHIFT. cligen also has a global config file [1] to control syntax options like whether '-c=val' requires an '=' (and if it should be '=' or ':', etc.) which has been discussed here, various colorization/help rendering controls, etc. Shell scripts can always export CLIGEN=/dev/null to force more strict and/or default modes. [1] https://github.com/c-blake/cligen/wiki/Example-Config-File https://github.com/c-blake/cligen/wiki/Example-Config-File
- juped 6y agoTry Typer (https://typer.tiangolo.com/ https://typer.tiangolo.com/) sometime, which is built on Click.
- onei 6y agoAt a glance it looks a bit too simple. I didn't look to far into it, but it seems to be missing short options, prompting for options and using environment variables for options. As it's built on click, I'd guess you can call into click to do those, but at that point I don't see a major benefit typer is providing.
- juped 6y agoLook, much of the purpose of posting things in a public forum is not to directly converse with the person to whom you are replying, but to add something relevant and interesting to third-party readers of the thread. It's up to you whether you're interested in the software package linked in the comment or not, but it is harmful to third-party readers who might potentially be interested in it when you inexplicably reply with unsubstantiated lies about it, which they might take as true.
- Noumenon72 6y agoA comment actually directed toward third-party readers would read like "For people who like Click but could also use X, try Typer." Using the second-person imperative to say 'Try this' challenges your interlocutor to ask "Why should I?", to evaluate it for their own use case, to say "You shouldn't have recommended this to me with no qualification, it's not for everyone."
- juped 6y agoTo be clear, not only is the package in question not "missing short options, prompting for options and using environment variables for options", there is not even the slightest indication on its docs site that it might be missing such features. I'm not harmed by the reckless indifference of that comment, because I know a decent amount about these libraries, and it's not my business if people want to self-harm by believing false things, but people scrolling the thread are potentially harmed ("damn, I like the syntax of this, but it doesn't support short options?").
- m463 6y agoThat is a fascinating project. I will look at it. What I wonder is -- there are some very nice third-party libraries for python, and I wonder why they don't make it into the standard library. It would be nice if there were ways of "a tide that raises all ships"
- onei 6y agoI don't disagree, but I think there's a fair amount of friction when maintaining standard libraries rather than third party ones just because of the implied stability. Your versioning is tied to python itself so it's probably kind of dull having to work with an API that you can't improve simply by bumping the version of the library.