News for hackers. Stuff that matters.
Re:Hypermodern Python by tgb on Thursday May 28, 2020 @08:30AM (#23336460)
I have to say, at first blush I would not choose click over argparse. I do have to look up the docs of argparse every time I use it, but I like that it's just gives me the args and lets me structure the program flow how I want to, which I think is more natural.

And then I can do things like import a big package (pandas) only after parsing the args, which is highly convenient to users that want to check the argument options without a five second lag.

Re:Hypermodern Python by jobeirne on Thursday May 28, 2020 @08:56AM (#23336658)
I recently wrote this (https://github.com/jamesob/clii) because I can't stand click and got sick of having to check the argparse docs every time I wanted to write a CLI. I guarantee you'll spend a tenth of the time trying to figure out how to use this thing, it has no dependencies, and is implemented in a single vendor-friendly file.
Re:Hypermodern Python by fastball on Thursday May 28, 2020 @09:27AM (#23336964)
There is also Typer[1]

[1] https://github.com/tiangolo/typer

Re:Hypermodern Python by jobeirne on Thursday May 28, 2020 @09:41AM (#23337083)
Similar interface, different design goals. This lib has 6x the code, dependencies, and isn't as easy to vendor/audit.
Re:Hypermodern Python by fastball on Thursday May 28, 2020 @09:45AM (#23337139)
Yep, good work! Though I do think the name "clii" could generate some confusion. Maybe not though.
Re:Hypermodern Python by jobeirne on Thursday May 28, 2020 @09:58AM (#23337275)
Thanks!
Re:Hypermodern Python by sk0g on Thursday May 28, 2020 @10:31AM (#23337713)
Does it handle help automatically? If not, and you think that'd be a valuable addition, I would like to take a crack at implementing it :)

What I mean is if you execute script.py help, it will print out the possible options, along with their docstrings, or maybe just the first line.

Re:Hypermodern Python by jobeirne on Thursday May 28, 2020 @10:57AM (#23338033)
It does parse docstrings in a very basic way (https://github.com/jamesob/clii#help-text-from-docstrings) but if you end up hacking in something you want, PRs are welcome.
Re:Hypermodern Python by tboerstad on Thursday May 28, 2020 @09:52AM (#23337220)
You really nailed this, imho. Awesome work.

What is the reason for requiring Python >=3.7?

Re:Hypermodern Python by jobeirne on Thursday May 28, 2020 @09:57AM (#23337267)
Thanks. That's a really good point - maybe it's usable on earlier versions. Can't remember why I thought otherwise, will have to test.
Re:Hypermodern Python by darkteflon on Thursday May 28, 2020 @09:05AM (#23336770)
This looks really good actually - bullshit-minimising. Will check it out.
Re:Hypermodern Python by fabioz on Thursday May 28, 2020 @09:45AM (#23337130)
I personally enjoy using https://github.com/google/python-fire nowadays.

-- it's probably not the most flexible tool, but IMHO it's the one with the best API: fire.Fire(obj) and everything else is taken care of ;)

p.s.: for lazy imports, just don't put them in the top of your module in the entry point.

Re:Hypermodern Python by minimaxir on Thursday May 28, 2020 @12:16PM (#23339030)
Fire plays nice with package entry_points as well.

Here's a very minimal CLI for one of my PyPI packages: https://github.com/minimaxir/aitextgen/blob/master/aitextgen...

Re:Hypermodern Python by i3winner on Thursday May 28, 2020 @08:44AM (#23336573)
I concur. I've found argparse to be more straightforward and also has the benefit of being part of python itself.
Re:Hypermodern Python by kortex on Thursday May 28, 2020 @08:47AM (#23336599)
Click has one feature which is huge for me: parsing env variables following some pattern eg MYAPP_FOO.

For lazy import - You can do it with click, too, just put your imports in your entrypoint functions.

Click isn't minimalistic though. Nor is poetry or pyenv. Seems to be the main complaints about the article.

Re:Hypermodern Python by jabwork on Thursday May 28, 2020 @01:11PM (#23339772)
Configargparse is a great drop in replacement for argparse that I use to handle bridging env vars with cli flags

https://pypi.org/project/ConfigArgParse/

Re:Hypermodern Python by kortex on Thursday May 28, 2020 @01:33PM (#23340042)
YES I need this in my life! Tried searching for such a thing but I guess had the wrong keywords.
Re:Hypermodern Python by empthought on Thursday May 28, 2020 @08:38AM (#23336533)
I agree; click's approach is like some kind of old-school 4GL that tries to automatically create GUI elements from your database tables, except mapping CLI to functions in your modules. People should be putting enough thought into their CLI that Click doesn't really help them much.
Re:Hypermodern Python by alexhutcheson on Thursday May 28, 2020 @11:35AM (#23338520)
absl.flags[1] is another pretty good option.

[1] https://abseil.io/docs/python/guides/flags