V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
winkidney
V2EX  ›  Python

[CmdTree] 又一个 Python 命令行库

  •  4
     
  •   winkidney ·
    winkidney · 2016-08-30 17:59:41 +08:00 · 2649 次点击
    这是一个创建于 2801 天前的主题,其中的信息可能已经有所发展或是发生改变。

    CMDTree

    因为之前做一个需求,遇到需要大量使用子命令( sub-command )和命令行自动生成的功能,现有的库都不能良好的满足需求,所以自己写了这个库。

    如果有以下业务场景,你大概是这个库的目标用户:)

    • 需要分类子命令到不同命令组内
    • 希望click装饰器不会影响原有的函数调用
    • 不希望直接使用 argparse 的低层 API ( what a pain!)
    • 希望能根据描述生成CRUD REST-API 的命令行调用

    特性

    • 为子命令和命令行自动生成而设计
    • 使用类click的装饰器写命令行
    • 友好的低层 API ,可以用["docker", "run"]这样的方式来添加命令
    • 仅依赖six
    • 支持 Python3
    • 支持类 click 的 argument type 且可拓展
    • 装饰器对函数调用本身没有副作用

    项目请戳 https://github.com/winkidney/cmdtree

    请轻拍:)


    CMDTree

    Build Status Coverage Status

    安装

    pip install cmdtree

    Quick Start

    Hello world

    from cmdtree import INT
    from cmdtree import command, argument, option
    
    
    @argument("host", help="server listen address")
    @option("reload", is_flag=True, help="if auto-reload on")
    @option("port", help="server port", type=INT, default=8888)
    @command(help="run a http server on given address")
    def run_server(host, reload, port):
        print(
            "Your server running on {host}:{port}, auto-reload is {reload}".format(
                host=host,
                port=port,
                reload=reload
            )
        )
    
    if __name__ == "__main__":
        from cmdtree import entry
        entry()
    

    Get help

    ➜  examples git:(master) python command.py --help
    usage: command.py [-h] {run_server} ...
    
    positional arguments:
      {run_server}  sub-commands
        run_server
    
    optional arguments:
      -h, --help    show this help message and exit
    

    Run command

    ➜  examples git:(master) python command.py run_server localhost
    Your server running on localhost:8888, auto-reload is False
    

    SubCommand of SubCommand

    Code here:

    from cmdtree import group, argument, entry
    
    @group("docker")
    @argument("ip")
    def docker():
        pass
    
    
    # nested command
    @docker.command("run")
    @argument("container-name")
    def run(ip, container_name):
        print(
            "container [{name}] on host [{ip}]".format(
                ip=ip,
                name=container_name,
            )
        )
    
    # nested command group
    @docker.group("image")
    def image():
        pass
    
    
    @image.command("create")
    @argument("name")
    def image_create(ip, name):
        print(
            "iamge {name} on {ip} created.".format(
                ip=ip,
                name=name,
            )
        )
    
    
    if __name__ == "__main__":
        entry()
    

    Run command:

    ➜  examples git:(master) python command_group.py docker localhost image create your-docker
    iamge your-docker on localhost created.
    
    3 条回复    2016-09-01 21:42:40 +08:00
    Yinz
        1
    Yinz  
       2016-08-30 23:29:01 +08:00
    酷,论装饰器的一百种用法:D
    winkidney
        2
    winkidney  
    OP
       2016-08-31 09:53:53 +08:00
    @Yinz 23333 总不是装饰器,爆炸……
    gucheen
        3
    gucheen  
       2016-09-01 21:42:40 +08:00
    Cool!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2086 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:42 · PVG 09:42 · LAX 18:42 · JFK 21:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.