关于python:Python-Cli-编写指南

7次阅读

共计 2888 个字符,预计需要花费 8 分钟才能阅读完成。

Python Cli 编写指南

python 实现 cli

环境:

  • python 3.8

  • python 自带 argparse

指南

简略示例 : cli.py

import argparse


def cli():
    parser = argparse.ArgumentParser(description='CLI 形容')
    subparsers = parser.add_subparsers(metavar='子命令')

    # 增加子命令,演示没有参数
    one_parser = subparsers.add_parser('one', help='第一个命令')
    one_parser.set_defaults(handle=handle_one)

    # 解析命令
    args = parser.parse_args()
    # 1. 第一个命令会解析成 handle,应用 args.handle() 就可能调用
    if hasattr(args, 'handle'):
        args.handle(args)
    # 2. 如果没有 handle 属性,则示意未输出子命令,则打印帮忙信息
    else:
        parser.print_help()


def handle_one(args):
    print('handle_one')


if __name__ == '__main__':
    cli()

调用:

$ python cli.py
usage: cli_1.py [-h] 子命令 ...

CLI 形容

positional arguments:
  子命令
    one       第一个命令

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

$ python cli.py one
handle_one

减少第二个命令

import argparse


def cli():
    parser = argparse.ArgumentParser(description='CLI 形容')
    subparsers = parser.add_subparsers(metavar='子命令')

    # 增加子命令,演示没有参数
    one_parser = subparsers.add_parser('one', help='第一个命令')
    one_parser.set_defaults(handle=handle_one)

    # 增加子命令,演示有参数
    two_parser = subparsers.add_parser('two', help='第二个命令')
    # 参数 (简写,全称,类型,是否必填,帮忙阐明)
    two_parser.add_argument('-s', '--str', type=str, required=True,
                            help='一个字符串类型参数')
    # 参数 (简写,全称,类型,默认值,帮忙阐明)
    two_parser.add_argument('-d', '--default', type=str, default='默认值',
                            help='这个命令有默认值')
    # 参数 (简写,全称,类型,帮忙阐明)
    two_parser.add_argument('-ts', '--the-str', type=str,
                            help='当全称有横线时,属性名转换为下划线,即 the_str')
    two_parser.set_defaults(handle=handle_two)

    # 解析命令
    args = parser.parse_args()
    # 1. 第一个命令会解析成 handle,应用 args.handle() 就可能调用
    if hasattr(args, 'handle'):
        # 1.1. 其余参数会被解析成 args 的属性,以命令全称为属性名
        args.handle(args)
    # 2. 如果没有 handle 属性,则示意未输出子命令
    else:
        parser.print_help()


def handle_one(args):
    # 无参数
    print('handle_one')


def handle_two(args):
    print('handle_two')
    print(f'str:{args.str}')
    print(f'default:{args.default}')
    print(f'the-str:{args.the_str}')


if __name__ == '__main__':
    cli()
  • 调用:

    $ python cli.py
    usage: cli.py [-h] 子命令 ...
    
    CLI 形容
    
    positional arguments:
    子命令
      one       第一个命令
      two       第二个命令
    
    optional arguments:
    -h, --help  show this help message and exit
  • 调用 one 不受影响

    $ python cli.py one
    handle_one
  • 调用 two 会显示须要必填项 -s

    $ python cli.py two
    usage: cli.py two [-h] -s STR [-d DEFAULT] [-ts THE_STR]
    cli.py two: error: the following arguments are required: -s/--str
  • 输出 - h 查看帮忙

    $ python cli.py two -h
    usage: cli.py two [-h] -s STR [-d DEFAULT] [-ts THE_STR]
    
    optional arguments:
    -h, --help            show this help message and exit
    -s STR, --str STR     一个字符串类型参数
    -d DEFAULT, --default DEFAULT
                          这个命令有默认值
    -ts THE_STR, --the-str THE_STR
                          当全称有横线时,属性名转换为下划线,即 the_str
  • 输出 - s 参数(或全称 –str)

    • 能够看到加了默认值的会显示默认值
    • 没默认值且非必填项值为 None
    $ python cli.py two -s 哈哈
    handle_two
    str: 哈哈
    default: 默认值
    the-str:None
  • 输出有默认值的参数 -d (或全称 –default)

    • 输出的值会笼罩默认值
    $ python cli.py two -s 哈哈 --default 改了
    handle_two
    str: 哈哈
    default: 改了
    the-str:None
  • 输出所有参数

    $ python cli.py two -s 哈哈 --default 改了 -ts 赋值了
    handle_two
    str: 哈哈
    default: 改了
    the-str: 赋值了 

其余代码应用

在 ”setup.py” 文件中

import setuptools

setuptools.setup(
  # 省略其余参数
  entry_points={
    'console_scripts': ['my-cli = my.cli:cli']
  }
)
  • ‘my-cli = my.cli:cli’ 就是替换内容

    • ‘my-cli’

      • 上传 pypi 后,拉取这个库,就可能应用 ’my-cli’ 来调用 cli
      • 如 ’my-cli one’ / ‘my-cli two -h’
    • ‘my.cli:cli’

      • ‘ 残缺类名: 办法名 ’
      • 目录构造如下方构造
      • cli 命令须要放在模块下
      • 不能放在根目录下,否则会报错找不到模块

文件目录构造:

root

  • my

    • \_\_init\_\_.py
    • cli.py

参考资料:

https://zhuanlan.zhihu.com/p/…

正文完
 0