简体   繁体   English

如何有效地构造argparse参数中一个,两个或三个选择的选择?

[英]How to structure the selection of one, two, or three choices in an argparse argument efficiently?

I am designing an argparse-based command line program, and one of the arguments that must be made asks the user to select one or more out of a total of three choices for the format of an output graph. 我正在设计一个基于argparse的命令行程序,必须输入的参数之一要求用户从输出图格式的三个选项中选择一个或多个。 If the user does not mention this argument within the command line, then by default, this argument outputs all three types of the output graphs. 如果用户未在命令行中提及此参数,则默认情况下,此参数输出所有三种类型的输出图。

So, the argument itself looks more or less like this: 因此,参数本身看起来大致如下:

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.")

So, after I ran the args= BobsProgram.parse_args() line and started to dispatch my arguments, I wanted it set up so that the user could type in their choices in order they wanted. 因此,在运行args= BobsProgram.parse_args()行并开始分派我的参数之后,我希望对其进行设置,以便用户可以按自己的args= BobsProgram.parse_args()键入他们的选择。 I have only found it possible to make the command line program function when I set up seven conditional blocks: 我只有在设置七个条件块时才可以使命令行程序起作用:

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

Ultimately, although the code works, it does not seem very Pythonic, as I must replicate a lot of the same code within each conditional block. 最终,尽管代码可以正常工作,但它似乎不是Python风格的,因为我必须在每个条件块中复制很多相同的代码。 How can I amend this to make it more efficient? 我如何修改它以使其更有效率?

I'd do something like: 我会做类似的事情:

for arg in args.output_graph:
    if arg == 'pie':
        #add_pie_chart()
    if arg == 'bar':
        #add_bar_chart()
    if arg == 'scatter':
        #add_scatter_plot()

The graph functionality now is only called once for each chart. 现在,每个图表只调用一次图形功能。 This should work as long as your add chart functions play relatively nice with each other, ie all get added to a master canvas before results are displayed. 只要您的添加图表功能相互之间发挥得比较好,即应该将所有功能都添加到主画布上,然后再显示结果,这就应该起作用。

If the order doesn't matter then you could do: 如果顺序无关紧要,则可以执行以下操作:

alist = args.your_name
if 'foo' in alist:
   # do foo
elif 'bar' in alist:
   # do bar
# etc

If the user provided order matters then the something like this: 如果用户提供的订单很重要,则如下所示:

for fn in alist:
    if fn in ['foo']:    # or `fn == 'foo'`
        # do foo
    elif fn in ['bar']:
        # do bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM