命令行应用程序分析器

clips的Python项目详细描述


夹子

一个简单的解析器,用于构建优美的命令行接口,它具有链式命令和丰富多彩的使用消息clips使用getopt解析命令参数和选项(扫描模式是gnu风格的)。

颜色是通过ansi代码实现的,它们不应该在windows上工作。更一般地说,请注意Clips没有在Windows环境下测试

快速启动

使用pip安装:

pip install clips

用法示例

带有链式命令的控制台:

# init console
console = ArgParser(
    'myconsole',                # prog. name
    description=description,    # prog. description
    banner=banner               # usage banner
)

# add a console-level argument
console.add_argument('-v','--version',help='Show version')

# add a console command
cmd = console.add_command('command',help='A command')

# add a subcommand
subcmd = cmd.add_command('subcommand',help='A subcommand')

# add an argument to subcommand
subcmd.add_argument('arg',help='Argument for this subcommand')

解析方法返回用户上下文的字典,

# parse arguments
args = console.parse_args(['command','subcommand','this'])
print(args)

所以输出是:

{
    '-h': False,
    '--help': False,
    '-v': False,
    '--version': False,
    'command': True,
    'subcommand': True,
    'arg': 'this'
}

要显示使用帮助:

# console-level usage
print(console.usage_help())

# command-level usage
print(console.usage_help(['command']))
print(console.usage_help(['command','subcommand']))

要禁用帮助选项的内部管理,请执行以下操作:

# init console
console = ArgParser(
    'myconsole',                # prog. name
    add_help=False              # no help hook
)

对标题和文本使用颜色:

# init console
console = ArgParser(
    'myconsole',                # prog. name
    title_fg='orange',          # title foreground color
    title_bg='green',           # title background color
    text_fg='green',            # text foreground color
    text_bg='orange'            # text background color
    )

按节分组命令(这仅影响使用帮助):

# create section with commands
console.add_section('First section')
cmd1 = console.add_command('cmd1')
cmd2 = console.add_command('cmd2')

# another section
console.add_section('Second section')
cmd3 = console.add_command('cmd3')
cmd4 = console.add_command('cmd4')

标题和文本部分的可用颜色:

  • foreground: black , red , green , orange , blue , cyan , lightgrey , darkgrey , lightred , lightgreen , yellow , lightblue , pink , lightcyan
  • 背景:blackredgreenorangebluepurplecyanlightgrey

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
jframe为什么JAVA paint()方法不起作用?   java Guice:将ContainerRequestContext注入拦截器   java如何优雅地关闭Spring JMS MessageListenerAdapter   java如何在Spring中设置快照隔离级别   Java中的安卓平台独立信任存储路径   java无法在eclipse中运行hello world程序   java Sinch空指针问题   使用Java将JSON流式传输到BigQuery   java从“大数据”中选择什么Swing控件?   java通过对象字段过滤/排序集合?   java将数据从活动传递到另一个活动中的片段   java访问打包在jar文件中的文档   Java获取事件的大小。getDragboard()。getFiles()。流()。映射(文件::长度)。toString());   java Android libgdx:启动程序图标按下后,启动屏幕不会立即显示   java如何在Google App Engine灵活环境中配置oracle jdk   java有没有办法减少这些行?   Java:客户端socket不读取第二行,在终止符行之后保持打开状态   java以编程方式获取api 29上的所有APK文件   java ActionBar按钮不显示