manage.py: 简明好用的 CLI builder

"CLI is everything."

PIP 官方项目地址

https://pypi.org/project/manage.py/ (含官方用例)

安装

pip install mange.py

使用

1.创建命令

from manager import Manager

manager_echo = Manager()


@manager_echo.command
def echo_with_no_args():
    """Command description, will show on command line.
    """
    print("Hello")


if __name__ == '__main__':
    manager_echo.main()
$ python3 cli_sample.py
usage: cli_sample.py <command> [<args>]

positional arguments:
  command     the command to run

optional arguments:
  -h, --help  show this help message and exit

available commands:
  echo_with_no_args        Command description, will show on command line.
$ python3 cli_sample.py echo_with_no_args
Hello

2. 多重 namepspace 管理

from manager import Manager
import time

manager_echo = Manager()
manager_ops = Manager()
manager_main = Manager()

manager_main.merge(manager_echo, namespace='echo')
manager_main.merge(manager_ops, namespace='ops')


@manager_echo.command
def echo_with_no_args():
    """Command description, will show on command line.
    """
    print("Hello")


@manager_ops.command
def ops_with_no_args():
    """Do some operation without args.
    """
    print(time.time() + 1.0)


manager_main.merge(manager_echo, namespace='echo')
manager_main.merge(manager_ops, namespace='ops')

if __name__ == '__main__':
    manager_main.main()
python3 cli_sample.py
usage: cli_sample.py [<namespace>.]<command> [<args>]

positional arguments:
  command     the command to run

optional arguments:
  -h, --help  show this help message and exit

available commands:

  [echo]
    echo_with_no_args      Command description, will show on command line.


  [ops]
    ops_with_no_args       Do some operation without args.
python3 cli_sample.py ops.ops_with_no_args
1603078473.9244218

3.不带提示、按照顺序输入的参数

@manager_echo.command
def echo_with_args(name, pid):
    """Echo given <params>
    """
    print(f'Name is {name}, PID is {pid}')
$ python3 cli_sample.py echo.echo_with_args  test 77
Name is test, PID is 77

4.带提示的顺序输入的参数

@manager_echo.arg('name', help='User\'s name')
@manager_echo.arg('pid', help='App\'s PID')
@manager_echo.command
def echo_with_args(name, pid):
    """Echo given <params>
    """
    print(f'Name is {name}, PID is {pid}')
$ python3 cli_sample.py echo.echo_with_args -h
usage: cli_sample.py echo.echo_with_args [-h] name pid

Echo given <params>

positional arguments:
  name        User's name
  pid         App's PID

optional arguments:
  -h, --help  show this help message and exit

5. 必要参数和非必要参数的提示输入

@manager_echo.arg('name', help='User\'s name')
@manager_echo.arg('pid', help='App\'s PID')
@manager_echo.command
def echo_with_args(name, pid=''):
    """Echo given <params>
    """
    print(f'Name is {name}, PID is {pid}')
$ python3 cli_sample.py echo.echo_with_args -h
usage: cli_sample.py echo.echo_with_args [-h] [--name NAME] [--pid PID]

Echo given <params>

optional arguments:
  -h, --help   show this help message and exit
  --name NAME  User's name
  --pid PID    App's PID
$ temp python3 cli_sample.py echo.echo_with_args 123
Name is 123, PID is
$ temp python3 cli_sample.py echo.echo_with_args 123 --pid 777
Name is 123, PID is 777

或者作为布尔值使用:

@manager_echo.arg('name', help='User\'s name')
@manager_echo.arg('pid', help='App\'s PID')
@manager_echo.command
def echo_with_args(name, pid=False):
    """Echo given <params>
    """
    if pid:
        print(f'Name is {name}, PID exists')
    else:
        print(f'Name is {name}, PID not exists')

$ python3 cli_sample.py echo.echo_with_args batman
Name is batman, PID not exists
$ python3 cli_sample.py echo.echo_with_args batman --pid
Name is batman, PID exists

6.参数 shortcuts

@manager_echo.arg('name', shortcut='n', help='User\'s name')
@manager_echo.arg('pid', shortcut='p', help='App\'s PID')
@manager_echo.command
def echo_with_args(name, pid=False):
    """Echo given <params>
    """
    if pid:
        print(f'Name is {name}, PID exists')
    else:
        print(f'Name is {name}, PID not exists')

$ python3 cli_sample.py echo.echo_with_args batman --p
Name is batman, PID exists