From Click‘s website:
click is a Python package for creating beautiful command line interfaces in a composable way with as little amount of code as necessary. It’s the “Command Line Interface Creation Kit”. It’s highly configurable but comes with good defaults out of the box.
Its main features include:
- arbitrary nesting of commands
- automatic help page generation
- supports lazy loading of subcommands at runtime
The example useage code demonstrates how it is used in real world:
import click @click.command() @click.option('--count', default=1, help='number of greetings') @click.option('--name', prompt='Your name', help='the person to greet', required=True) def hello(count, name): for x in range(count): print('Hello %s!' % name) if __name__ == '__main__': hello()
It can automatically generate help usage message:
$ python usage.py --help Usage: usage.py [OPTIONS] Options: --count=COUNT number of greetings --name=NAME the person to greet [required] --help Show this message and exit.
Interestingly to note that internally Click uses optparse instead of argparse, even optparse has been deprecated since Python 2.7:
The reason however click is not using argparse is that it has some problematic behaviors that make handling arbitrary command line interfaces hard: […]
By default, out-of-the-box, click can handle parameters types such as string, integer, float, boolean, click.File, and click.Choice. Customized type can certainly be done.
click.group for nesting commands, click.prompt and click.confirm for handling user input and it’s needed for an input from user. There are more to click, read its well-written documentation for more.
It’s an alternative to argparse for handling command-line options. It’s quite different than argparse or any other libraries, since it utilizes Python decorator to connect options and handling functions for commands.
Click is written by Armin Ronacher, licensed under the New BSD License (3-clause), for both Python 2 and 3.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.