4 ms·
I could probably figure it out, or find it documented, but it would be nice to have concise instructions on creating a Python command-line utility.
by analog31 2y ago
I could probably figure it out, or find it documented, but it would be nice to have concise instructions on creating a Python command-line utility.
- fforflo 2y agoif you want complex subcommands and a truly fluent CLI interface, go with a click. https://click.palletsprojects.com/ https://click.palletsprojects.com/ else: argparse is more than enough https://docs.python.org/3/library/argparse.html https://docs.python.org/3/library/argparse.html
- amlozano 2y agoOr go a step beyond and get typer (which builds on click IIRC) https://typer.tiangolo.com/ https://typer.tiangolo.com/ If you use Poetry and a pyproject.toml, you can even make your package installable with something like pipx straight from Github. Its a trick I use often for little command line utilities. https://python-poetry.org/docs/pyproject/#scripts https://python-poetry.org/docs/pyproject/#scripts
- kstrauser 2y agoI've not personally stumbled on a UI where Click was used to make something nicer than argparse could. I'm not saying those don't exist, just that I haven't seen one. Every time I've seen someone use Click at work, it was to implement some simple args that argparse could handle identically. "Why are we using Click here?" "Because it's nice!" "In what way?" "It's nice!" "But we're not actually using the nice parts." "Click is nice!" Click is nice. IMO it's used way too often in cases where argparse would do just as well.
- noitpmeder 2y agoI've reached for click when I need to create a layered command scheme .. ex where `mytool command1 --help` and `mytool command2 --help` would provide two different sets of arguments. Similar to what the `git` suite provides.
- kstrauser 2y agoThe argparse version of that is `add_subparsers`: https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_subparsers https://docs.python.org/3/library/argparse.html#argparse.Arg...
- LeoPanthera 2y agoAs long as your python program checks for __name__ like this: if __name__ == "__main__": # Your code here Then it should mostly "just work". Use the argparse module to parse command line arguments if necessary. Create a setup.py and use pip to install it, if you want.
- fragmede 2y agoif you get lost using argparse, give docopt a try, it's self documenting.
- Doxin 2y agoClick is a nice option too. Slightly less magic than docopt, though docopt is great for quick hacks.
- philomath_mn 2y agoI've been making zero-cost Python CLIs with this for years: https://github.com/google/python-fire https://github.com/google/python-fire
- eigenvalue 2y agoThe great tiangolo (creator of FastAPI) has a library just for this which makes it easy: https://github.com/tiangolo/typer https://github.com/tiangolo/typer
- selcuka 2y agoThere is also a (unofficial) Google project called fire [1]: > Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object. [1] https://github.com/google/python-fire https://github.com/google/python-fire