Live data from Hacker News

Easy Python CLI with Click

codingwithricky.com

41–50 of 64 posts

Re: Easy Python CLI with Click

#41
post #11

My favorite library for this is docopt[0], which parses the docstring at the top of your script. It’s a lot easier for people reading your code to see the usage up front rather than scrolling to the bottom and finding your main() and reading the argparse calls: #!/usr/bin/env python3 “”” Usage: ./myscript.py [ ] “”” from docopt import docopt if __name__ == ‘__main__’: args = docopt(__doc__) print(args[‘ ’], args[‘ ’]…

That's what I came in here to mention. I mostly write small, personal utilities with a max of 5-10 commands and a few parameters, and I think click's architecture is too strange and complex. Docopt is simple and clear, doesn't do anything more than I need, and leaves me free to architect the rest of the script however seems best. It also works the same in a bunch of languages.

I do concede that click may be better for CLI apps that grow to tens of thousands of LOC and involve multiple full-time developers. I've never built one that big though.

I also worry a bit that docopt doesn't seem to have gotten much attention in a while. It's basically complete, so shouldn't matter, but still.

Re: Easy Python CLI with Click

#42
post #24

Earlier quoted context omitted.

if it's a simple basic script, i wouldn't even go through the pain of using argparse. i would just manually check sys.argv.

You ALWAYS need a CLI framework at least for generating help automatically, otherwise I hate you when I want to use your frugal tool and I have to look into it to find out how it works.

In which case you would be using click. If I have any expectation that someone else is going to use my script I give it a REAME.md, a requirements.txt, a setup.py and I use click. It literally takes about 30's to do and you now have a cli interface indistinguishable from any other cli on your system.

Re: Easy Python CLI with Click

#43
post #28

Earlier quoted context omitted.

Why format()?

I like it. Maybe because it's explicit. I never liked the old % style. I may switch to f-strings one day. So much for "There should be one-- and preferably only one --obvious way to do it."

So you're comfortable using something because you like it?

Re: Easy Python CLI with Click

#44
post #11

My favorite library for this is docopt[0], which parses the docstring at the top of your script. It’s a lot easier for people reading your code to see the usage up front rather than scrolling to the bottom and finding your main() and reading the argparse calls: #!/usr/bin/env python3 “”” Usage: ./myscript.py [ ] “”” from docopt import docopt if __name__ == ‘__main__’: args = docopt(__doc__) print(args[‘ ’], args[‘ ’]…

Docopt is a killer arg parser for build scripts.

I drop a doc-string like this one in every build script and get the command line interfaces for free:

    """
    Install:
      pipenv install --dev
    
    Usage:
      make.py [] [options]
    
    Commands:
      build    Build wheel.
      push     Push wheel to pypi.
      test     Run tests.
      bump     Run interacitve bump sequence.
      git      Run interactive git sequence.
    
    Options:
      -h, --help  Show this screen.
    """

Re: Easy Python CLI with Click

#45
I have 3 live projects using click. I like it.

Caveat: Click's docs were out of date and had open bugs for what feels like years until it finally updated to 7. version 6 was "unstable" https://github.com/pallets/click/issues/503. Hopefully it's over now.

Good things:

- More concise than standard library argparse

- Testing via CliRunner (http://click.palletsprojects.com/en/7.x/testing/), example: https://github.com/tmux-python/tmuxp/blob/v1.5.3/tests/test_...

Feel free to study / copy from mine if you like (license MIT):

- https://cihai-cli.git-pull.com/: https://github.com/cihai/cihai-cli/blob/v0.5.0/cihai_cli/cli...

- https://tmuxp.git-pull.com/: https://github.com/tmux-python/tmuxp/blob/v1.5.3/tmuxp/cli.p...

- https://vcspull-git-pull.com/: https://github.com/vcs-python/vcspull/blob/v1.2.0/vcspull/cl...

Re: Easy Python CLI with Click

#46

Earlier quoted context omitted.

Perhaps a non-starter for you if you don't want to involve Docker but I have had good experiences wrapping a docker invocation in a shell script shim with the same name as the program and forwarding everything I need into the container. Then folks just grab the shell script and they're off... added bonus it's really easy to add update functionality to your tools.

So you're replacing a single executable Python script with a dependency that requires four additional separate artefacts (Dockerfile, image, container and the shell script) to be executed...

As well as Docker being installed, and configured, which implies admin access to the hardware you're running it on

Re: Easy Python CLI with Click

#47
post #5
post #4

I've used Fire[0] for a few projects and have been meaning to give Click a try, but I'm not sure I like the syntax after reading through the article (holy moly decorators). Seems unwieldy to me, but does anyone have experience with both? [0] https://github.com/google/python-fire

Based on the examples in the article, explicitly writing a line for each CLI option seems like excessive boilerplate and verbosity that Fire manages to sidestep completely. To me, this is the principal point of using a Python library to create CLIs. Am I missing something?

Fire works with bare args, automatically supports --arg=value for constructor's arguments and provides more features like tracing and bash completion generator.

Re: Easy Python CLI with Click

#48
post #28

Earlier quoted context omitted.

I like it. Maybe because it's explicit. I never liked the old % style. I may switch to f-strings one day. So much for "There should be one-- and preferably only one --obvious way to do it."

So you're comfortable using something because you like it?

Of course.
Post reply on HN