命令行应用程序分析器
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
- 背景:black,red,green,orange,blue,purple,cyan,lightgrey