Dan*_*JAB 5 argparse python-3.x
我正在编写一个 python 程序,我想要一个以特定方式运行的命令行界面
命令行界面应接受以下调用:
my_prog test.svg foo
my_prog --font=Sans test.svg foo
Run Code Online (Sandbox Code Playgroud)
(它将生成一个 svg,其中包含foo以指定或默认字体编写的单词)
现在我希望也能够让这个命令接受以下调用...
my_prog --list-fonts
Run Code Online (Sandbox Code Playgroud)
这将列出--font由系统上可用的字体确定的所有有效选项。
我正在使用argparse,我有这样的事情:
parser = argparse.ArgumentParser()
parser.add_argument('output_file')
parser.add_argument('text')
parser.add_argument('--font', help='list options with --list-fonts')
parser.add_argument('--list-fonts', action='store_true')
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
但是,这不会使该--list-fonts选项表现得像我希望的那样,因为仍然需要两个位置参数。
我也尝试过使用子解析器,但这些仍然需要一种解决方法来防止每次都需要其他选项。
如何使用 argparse 获得所需的行为。
argparse 允许您根据action关键字参数定义在遇到参数时采取的任意操作add_argument(请参阅文档)
您可以定义一个操作来列出您的字体,然后中止参数解析,这将避免检查其他必需的参数。
这可能是这样的:
class ListFonts(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
print("list of fonts here")
parser.exit() # exits the program with no more arg parsing and checking
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样将它添加到你的论点中:
parser.add_argument('--list-fonts', nargs=0, action=ListFonts)
Run Code Online (Sandbox Code Playgroud)
nargs=0已添加注释,以便此参数不需要值(问题中的代码使用 实现了这一点action='store_true')
此解决方案有一个副作用,即启用如下所示的调用也可以在不运行主程序的情况下列出字体和退出:
my_prog --font Sans test.svg text --list-fonts
Run Code Online (Sandbox Code Playgroud)
这可能不是问题,因为它不是典型的用例,特别是如果帮助文本解释了这种行为。
如果为每个这样的选项定义一个新类感觉太重了,或者您可能有多个具有此行为的选项,那么您可以考虑使用一个函数来为每个参数实现所需的操作,然后拥有一种工厂函数返回一个包装函数的类。下面显示了一个完整的示例。
def list_fonts():
print("list of fonts here")
def override(func):
""" returns an argparse action that stops parsing and calls a function
whenever a particular argument is encountered. The program is then exited """
class OverrideAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
func()
parser.exit()
return OverrideAction
parser = argparse.ArgumentParser()
parser.add_argument('output_file')
parser.add_argument('text')
parser.add_argument('--font', help='list options with --list-fonts')
parser.add_argument('--list-fonts', nargs=0, action=override(list_fonts),
help='list the font options then stop, don\'t generate output')
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1711 次 |
| 最近记录: |