Your python code is now 100% more elegant and usable as a command-line program using Click
Many people who use python are used to the scriptfile workflow, where you write a rigid script in python and run it, somewhat similar to how people use bash scripts.
- You find an answer in a blog somewhere that describes how to do something in python
- You draft up a python script for the task and get it to work
- You hard-code all the parameters the script needs
Don't get me wrong – drafting up python scripts to prototype your idea is really useful. You get your idea working fast, even if your code becomes messy and unmaintainable.
However, wouldn't you like your python script to be as elegant and usable as all the command-line utilities you're used to?
For kicks, lets take the python3
print() function, and give it a CLI. Kind of like the
echo command in bash.
This is the definition of
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
So in python, you can print in all sorts of ways, more than just
print('hello'). For example, you can add customized string to append to the end, change the seperator between words, and output to a file instead of stdout.
Take a look on how easy it is to turn the print statement above into a full-featured CLI.
Essentially this script is taking all the command line arguments and giving them directly to the
print() function. So we are in effect giving the bash-terminal all the capabilities of python's
print(). Instead of
Now that we have our command-line-enabled python script, lets see how well it does…
./print.py can it echo this?
can it echo this?
Usage: print.py [OPTIONS] [ARGS]... Print the arguments you give this program to stdout or to a file. Similar to echo. Options: -s, --sep TEXT String to seperate each argument (default: " ")] -e, --end TEXT String to append to end (default: "\n") -o, --out TEXT Output file (default: stdout) --help Show this message and exit.
Wow… we can already see this has more features than the ever-so-useful
./print.py these args are seperated by dashes --sep='-'
./print.py maybe I want 2 newlines --end=$'\n\n' && print.py instead
maybe I want 2 newlines instead
./print.py write this to a file --out=file.txt
Then read the file
write this to a file
This was a demonstration to show you how easy it is to create your own CLI. You don't need write plain python scriptfiles anymore.
Take this template and apply it to your own program.
Visit the Click website to reference the
Click documentation while building your projects.
Also, for a slightly more complex example, you can check out my QuadArt project which uses Click CLI to be usable by anyone through the terminal. I wrote a blog post about it here. Here is an exerpt of the part of
quadart.py that uses
This is the magic of combining python and the terminal with the glue of CLI. The possibilities are endless!
See you next post.