Basic Descriptors in Python Click Library

Basic Descriptors in Python Click Library

To get introductory information about Click ,go to my previous article by clicking here.


Click is based on declaring commands through decorators.


A function becomes a Click command line tool by decorating it through click.command().
At its simplest, just decorating a function with this decorator will make it into a 
callable script.

click.command(name=None, cls=None, **attrs)

Creates a new Command and uses the decorated function as callback. This will also
automatically attach all decorated option()s and argument()s as parameters to the command.
The name of the command defaults to the name of the function. If you want to change that,
you can pass the intended name as the first argument.All keyword arguments are forwarded 
the underlying command class.Once decorated the function turns into a Command instance
that can be invoked as a command line utility or be attached to a command Group.

name %u2013 the name of the command. This defaults to the function name with underscores
replaced by dashes.
cls %u2013 the command class to instantiate. This defaults to Command.


import click

def cli():
click.echo('Hello World!')

What%u2019s happening is that the decorator converts the function into a Command which
then can be invoked:

if __name__ == '__main__':

And what it looks like:

$ python
Hello World!

And the corresponding help page:

$ python --help
Usage: [OPTIONS]

--help Show this message
and exit.


class click.Option(param_decls=None, show_default=False, prompt=False, confirmation_prompt
=False, hide_input=False, is_flag=None, flag_value=None, multiple=False, count=False, 
allow_from_autoenv=True, type=None, help=None, **attrs)

Options are usually optional values on the command line
and have some extra features that
arguments don%u2019t have.All other parameters are passed onwards to the parameter constructor.

%u2013 controls if the default value should be shown on the help page. Normally,
defaults are not shown.

%u2013 if set to True or a non empty string then the user will be prompted for input
if not set. If set to True the prompt will be the option name capitalized.

%u2013 if set then the value will need to be confirmed if it was prompted

hide_input %u2013 if this is True then the input on the prompt will be hidden from the user. 
This is useful for password input.

is_flag %u2013 forces this option to act as a flag. The default is auto detection.

%u2013 which value should be used for this flag if it%u2019s enabled. This is set to a
boolean automatically if the option string contains a slash to mark two options.

%u2013 if this is set to True then the argument is accepted multiple times and
recorded. This is similar to nargs in how it works but supports arbitrary number of 

count %u2013 this flag makes an option increment an integer.

%u2013 if this is enabled then the value of this parameter will be pulled
from an environment variable in case a prefix is defined on the context.

%u2013 the help string.

Basic Value Options:
Adding options to commands can be accomplished by the option() decorator. Since options
can come in various different versions, there are a ton of parameters to configure their
behaviour. Options in click are distinct from positional arguments.You can refer the 
option implicitly by the longest dash-prefixed argument:

@click.option('-s', 'name')
def cli(string_to_echo):
'Hello %s!'% string_to_echo)

Or, explicitly, by giving one non-dash-prefixed argument:

@click.option('-s', '--string-to-echo', 'string')
def cli(string):

To call this module you
type the following command:

$myhello --s Ranjit
Hello Ranjit

Multi-Value Options:

Sometimes you have options that take more than one value.So we use multiple=
True as in
the example below where we pass more than one name as an argument and the names are read
as a tuple.

import click

@click.option('--verbose', is_flag=True, help="Will print verbose messages.")
@click.option('--name', '-n', multiple=True, default='', help='Who are you?')
def cli(verbose,name):
if verbose:
"We are in the verbose mode.")
"Hello World")
for n in name:
'Bye {0}'.format(n))

When we need help on the command we get the following:

$ myhello --help
Usage: myhello [OPTIONS]

--verbose Will
print verbose messages.
-n, --name TEXT Who are you
--help Show this message
and exit.

$ myhello --verbose
We are
in the verbose mode.
Hello World

You can
type any one of the following commands:
$myhello -n Ranjit -name Tony
$myhello --name Ranjit --name Tony

The output
is :
$ Hello World
Bye Ranjit
Bye Tony

The click.option class allows much more like counting variables, boolean flags, feature 
switches, choice options, prompting for input from user and password encryptions, values 
from environment variables, etc.


class click.Argument(param_decls, required=None, **attrs)
Arguments are positional parameters to a command. They generally provide fewer features
than options but can have infinite nargs and are required by default.All parameters are 
passed onwards to the parameter constructor.

import click

def cli(name):
'Hello %s!'%name)

$myhello Ranjit
Hello Ranjit

The click.option class allows much more like counting variables, boolean flags, feature 
switches, choice options, prompting for input from user and password encryptions, values 
from environment variables, etc.
None, **attrs)
Creates a new Group
with a function as callback. This works otherwise the same as command()
just that the cls parameter is set to Group.

Click here to learn more about Click Parameters.