如何有效地在 argparse 参数中构建一个、两个或三个选择的选择?
How to structure the selection of one, two, or three choices in an argparse argument efficiently?
我正在设计一个基于 argparse 的命令行程序,必须提出的参数之一要求用户 select 输出格式的总共三种选择中的一种或多种图形。如果用户没有在命令行中提及此参数,则默认情况下,此参数会输出所有三种类型的输出图。
因此,论点本身看起来或多或少是这样的:
import argparse
if __name__ == "__main__":
BobsProgram = argparse.ArgumentParser(prog= "BobsProgram")
BobsProgram.description= "This code analyzes these inputs, and will output one or more graphs of your choosing."
BobsProgram.add_argument("-output_graph", choices= ["pie", "bar", "scatter"], default= all, nargs= "+",
help= "Unless otherwise indicated, the output graphs will be given in the pie, bar, and scatter forms.")
因此,在我 运行 args= BobsProgram.parse_args()
行并开始发送我的参数之后,我希望设置它以便用户可以按他们想要的顺序输入他们的选择。当我设置七个条件块时,我才发现可以使命令行程序运行:
if args.output_graph == ["pie"]:
##format the output file as a pie chart
elif args.output_graph == ["bar"]:
##format the output file as a bar chart
elif args.output_graph == ["scatter"]:
##format the output as a scatter chart
elif args.output_graph == ["pie","bar"] and ["bar", "pie"]:
##format the output as pie and bar charts
elif args.output_graph == ["pie","scatter"] and ["scatter","pie"]:
##format the output as pie and scatter charts
elif args.output_graph == ["bar", "scatter"] and ["scatter","bar"]:
##format the output as bar and scatter charts
else:
##format the output as bar, pie, and scatter charts
最终,虽然代码可以运行,但它似乎不是很 Pythonic,因为我必须在每个条件块中复制很多相同的代码。我该如何修改它以使其更有效率?
我会这样做:
for arg in args.output_graph:
if arg == 'pie':
#add_pie_chart()
if arg == 'bar':
#add_bar_chart()
if arg == 'scatter':
#add_scatter_plot()
现在每个图表只调用一次图形功能。只要您的添加图表功能相互配合得比较好,这应该就可以工作,即在显示结果之前,所有功能都会添加到母版 canvas。
如果顺序无关紧要,那么您可以这样做:
alist = args.your_name
if 'foo' in alist:
# do foo
elif 'bar' in alist:
# do bar
# etc
如果用户提供的顺序很重要,那么就像这样:
for fn in alist:
if fn in ['foo']: # or `fn == 'foo'`
# do foo
elif fn in ['bar']:
# do bar
我正在设计一个基于 argparse 的命令行程序,必须提出的参数之一要求用户 select 输出格式的总共三种选择中的一种或多种图形。如果用户没有在命令行中提及此参数,则默认情况下,此参数会输出所有三种类型的输出图。
因此,论点本身看起来或多或少是这样的:
import argparse
if __name__ == "__main__":
BobsProgram = argparse.ArgumentParser(prog= "BobsProgram")
BobsProgram.description= "This code analyzes these inputs, and will output one or more graphs of your choosing."
BobsProgram.add_argument("-output_graph", choices= ["pie", "bar", "scatter"], default= all, nargs= "+",
help= "Unless otherwise indicated, the output graphs will be given in the pie, bar, and scatter forms.")
因此,在我 运行 args= BobsProgram.parse_args()
行并开始发送我的参数之后,我希望设置它以便用户可以按他们想要的顺序输入他们的选择。当我设置七个条件块时,我才发现可以使命令行程序运行:
if args.output_graph == ["pie"]:
##format the output file as a pie chart
elif args.output_graph == ["bar"]:
##format the output file as a bar chart
elif args.output_graph == ["scatter"]:
##format the output as a scatter chart
elif args.output_graph == ["pie","bar"] and ["bar", "pie"]:
##format the output as pie and bar charts
elif args.output_graph == ["pie","scatter"] and ["scatter","pie"]:
##format the output as pie and scatter charts
elif args.output_graph == ["bar", "scatter"] and ["scatter","bar"]:
##format the output as bar and scatter charts
else:
##format the output as bar, pie, and scatter charts
最终,虽然代码可以运行,但它似乎不是很 Pythonic,因为我必须在每个条件块中复制很多相同的代码。我该如何修改它以使其更有效率?
我会这样做:
for arg in args.output_graph:
if arg == 'pie':
#add_pie_chart()
if arg == 'bar':
#add_bar_chart()
if arg == 'scatter':
#add_scatter_plot()
现在每个图表只调用一次图形功能。只要您的添加图表功能相互配合得比较好,这应该就可以工作,即在显示结果之前,所有功能都会添加到母版 canvas。
如果顺序无关紧要,那么您可以这样做:
alist = args.your_name
if 'foo' in alist:
# do foo
elif 'bar' in alist:
# do bar
# etc
如果用户提供的顺序很重要,那么就像这样:
for fn in alist:
if fn in ['foo']: # or `fn == 'foo'`
# do foo
elif fn in ['bar']:
# do bar