I have always been fascinated by commandline.
I wanted to build a command-line application for myself. I had the idea, I knew
in which language I am going to write, which is Python. After some research, I
found that the built-in module that comes with Python, argparse
is not enough.
But after some research, I found the "Click" module. So, today, we'll try to create a basic application using Python and Click.
Install Click
$ pip install click
I am using the version 6.7 of Click.
Hellow, World
Keeping up the traditions let's make a "Hello, World" program first. I am writing this in the file named cli.py
import click
@click.command()
def hello():
click.echo('Hello World!')
The click.echo()
prints a message plus a newline to the given file or stdout. On first sight, this looks like the print function, but it has improved support for handling Unicode and binary data that does not fail no matter how badly configured the system is.
What’s happening is that the decorator converts the function into a Command which then can be invoked:
if __name__ == '__main__':
hello()
And what it looks like:
$ python hello.py
Hello World!
And the corresponding help page:
$ python hello.py --help
Usage: hello.py [OPTIONS]
Options:
--help Show this message and exit.
Arguments
Now at the moment, our program doesn't do much. Let's give it some arguments.
import click
@click.command()
@click.argument('name')
def hello(name):
click.echo(f"Hello, {name}!")
if __name__ == '__main__':
hello()
And what it looks like:
$ python cli.py Anshul
Hello, Anshul!
Now let's see what happens if I don't give any arguments:
$ python cli.py
Usage: cli.py [OPTIONS] NAME
Error: Missing argument "name".
Options
So, as the name suggests, these are Options, unlike Arguments we saw above, these are optional. They are not mandatory to give.
import click
@click.command()
@click.option('--count', default=1, help="Number of greetings.")
@click.argument('name')
def hello(count, name):
for i in range(count):
click.echo(f"Hello, {name}!")
if __name__ == '__main__':
hello()
What it looks like:
$ python cli.py Anshul --count 3
Hello, Anshul!
Hello, Anshul!
Hello, Anshul!
Boolean flags
There are some options which do not need to take any value. they are just flags.
For example the --help
command. It doesn't take the value of it. Another
example that you may have noticed while using cli tools is --version
.
import click
@click.command()
@click.option('--flag', is_flag=True)
def hello(flag):
if flag:
click.echo("Our flag will sail high and above!")
else:
click.echo("Well, that didn't go as planned!")
if __name__ == '__main__':
hello()
That looks like:
$ python cli.py
Well, that didn't go as planned!
$ python cli.py --flag
Our flag will sail high and above!
That's it for now. There's so much you can do from here onwards.
I made a command-line application to see the currency exchange rates. You can see it here in action https://github.com/anshulc95/exch . I have here utilised same concepts that I taught just now. You can look at the source code. And if you have any suggestions or any critique for me, you can find me at Twitter/anshulc95.
Keep coding.