Introduction to Click-A python library to create Command Line Interfaces

Introduction to Click-A python library to create Command Line Interfaces


Click is a Python package for creating beautiful command line interfaces in a composable
way with as little code as necessary. It%u2019s the %u201CCommand Line Interface Creation Kit%u201D. 
It%u2019s highly configurable but comes with sensible defaults out of the box.
It aims to make the process of writing command line tools quick and fun while also
preventing any frustration caused by the inability to implement an intended CLI API.

Click in three points:

1. Allows nesting of commands arbitrarily.

2. Automatically generates help page.

3. Supports lazy loading of subcommands at runtime


You can easily install click using the following command:

pip install click

The installation into a virtualenv is heavily recommended.Virtual Environment should be
used whenever you work on any Python based project. It is generally good to have one new
virtual environment for every Python based project you work on. So the dependencies of 
every project are isolated from the system and each other.

What problem does virtualenv solve?
Chances are that you want to use it for other projects
besides your Click script. But the more projects you have, the more likely it is that 
you will be working with different versions of Python itself, or at least different 
versions of Python libraries. If two or more of your projects have conflicting 
dependencies, Virtualenv enables multiple side-by-side installations of Python, one for
each project. It doesn%u2019t actually install separate copies of Python, but it does provide
a clever way to keep different project environments isolated. 
Installing virtualenv if you are on Mac OS X or Linux:

$ sudo pip install virtualenv

If you are on Windows (or none of the above methods worked) you must install pip first.Once
you have it installed, run the pip command from above, but without the sudo prefix.

Once you have virtualenv installed, just fire up a shell and create your own environment.
I created a project folder named ClickProject and a venv folder within:

$ mkdir ClickProject
$ cd ClickProject
$ virtualenv venv
New python executable in venv/bin/python
Installing setuptools, pip............done.

Now, whenever you want to work on a project, you only have to activate the corresponding
environment. On OS X and Linux, do the following:

$ . venv/bin/activate

If you are a Windows user, the following command is for you:

$ venvscriptsactivate
Either way, you should now be using your virtualenv (notice how the prompt of your shell
has changed to show the active environment).

Now let's start by creating a simply script in a file called

def cli():
print("Hello World")

In order to be able to use this python script as a CLI, we need a file in the
same directory.So we create one as below:

from setuptools import setup


The starting point of our tool in the entry_points, hello:cli points to the right function
to start with. The setup file above allows us to specify a name for the module, like in
the example above I have used "myhello" . We need to make our project editable by using 
the following  command:

pip install --editable .

Now we are all set to run our first Click program.Simply type the module name and you
should see Hello World as output

$ myhello
Hello World

And if you want to go back to the real world, use the following command:
$ deactivate

To learn more about some basic Click Decorators in my next article, click here.