PyCLI
A Tiny Python CLI Library Based On argparse, make it easier for us to add subcommands.
If you use argparse to parse arguments, and you are very upset about adding subcommands and their handlers, then PyCLI is what you want.
Demo
Here is the simplest demo for pycli.
# demo.py
from pycli import CLI
cli = CLI(prog="app", version="v1.0.0")
@cli.command
def add(x, y):
"""get sum of two numbers"""
return x + y
print(cli.run())
Then we run it to get help information, a subcommand named add is added:
$ python demo.py -h
usage: app [-h] [-v] {add} ...
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
subcommands:
{add}
Next we lookup the help information of the subcommand, two positional arguments are added:
$ python demo.py add -h
usage: app add [-h] x y
get sum of two numbers
positional arguments:
x
y
optional arguments:
-h, --help show this help message and exit
We use the subcommand to get sum of two numbers, the output we expected is 3, but 12 is given, so we need to specify the type of arguments:
$ python demo.py add 1 2
12
Argument Type
By default, type of arguments is str, we can change it by function annotation.
We specify argument x and y of type int:
# demo.py
from pycli import CLI
cli = CLI(prog="app", version="v1.0.0")
@cli.command
def add(x: int, y: int):
"""get sum of two numbers"""
return x + y
print(cli.run())
We run the subcommand to get sum of 1 and 2, now result 3 is given:
# python demo.py add 1 2
3
Default Argument
Some arguments may have default values, so we can make it optional and no need to pass them while run.
# demo.py
from pycli import CLI
cli = CLI(prog="app", version="v1.0.0")
@cli.command
def add(x: int, y: int = 3):
"""get sum of two numbers"""
return x + y
print(cli.run())
We lookup the help information of the subcommand, y becomes a optional argument:
$ python demo.py add -h
usage: app add [-h] [--y Y] x
get sum of two numbers
positional arguments:
x type: <int>
optional arguments:
-h, --help show this help message and exit
--y Y type: <int> (default: 3)
List Argument
Sometimes we may need a list argument, for example, specifying multiple configuration files:
# demo.py
from pycli import CLI
cli = CLI(prog="app", version="v1.0.0")
@cli.command
def init(conf_files: list):
"""Initialize system"""
# do something here
return conf_files
print(cli.run())
Now we can pass multiple values to the same argument, and a list of string will return:
$ python demo.py init --conf-files a.ini --conf-files b.ini
['a.ini', 'b.ini']
Bool Argument
Sometimes we need a argument as a switch, we can speficy its type as bool and give it a default value True/False.
If we pass it when run, its value will be switched:
# demo.py
import queue
from pycli import CLI
cli = CLI(prog="app", version="v1.0.0")
q = queue.Queue()
@cli.command
def get(block: bool = True):
"""get a item"""
return q.get(block)
print(cli.run())
Custom Subcommand
By default, PyCLI use function name or object's class name as subcommand title, and use docstring as subcommand description.
You can custom them as you wish:
# demo.py
from pycli import CLI
cli = CLI(prog="app", version="v1.0.0")
@cli.command_with_args(title="sum", description="Sum of two integers")
def add(x: int, y: int):
"""get sum of two numbers"""
return x + y
print(cli.run())
Now a subcommand named sum is added as follow:
$ python demo.py sum -h
usage: app sum [-h] x y
Sum of two integers
positional arguments:
x type: <int>
y type: <int>
optional arguments:
-h, --help show this help message and exit
Integration With Argparse
pycli.CLI is a subclass of argparse.ArgumentParser, so it has some APIs as ArgumentParser.
Metadata
Release files for py3cli 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py3cli-1.0.1.tar.gz | 10.4 kB | Without provenance | Details for py3cli-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py3cli-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for py3cli-1.0.1-py3-none-any.whl |
Total release size: 17.0 kB
Release files / py3cli-1.0.1.tar.gz
| Download URL | py3cli-1.0.1.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
df2cac3ac85b480ecdb4cfc813fa98e42a2d98fb133f73b6c5be8e211b73f7fc
|
|
BLAKE2b-256 checksum How to use checksums |
9e879b48c74d6654a673d93048f17c8246b30fff02dd671faee7ef3005d0cbec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / py3cli-1.0.1-py3-none-any.whl
| Download URL | py3cli-1.0.1-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3605d3371eadacd5b2223e0f266783ce0d6e6e4795f62e6b77867d58c032eb04
|
|
BLAKE2b-256 checksum How to use checksums |
ec2a8f4969fac653dcc1739d563679fad0b29f5b4a37ade3846dbc20640e48ef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for py3cli 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.