2

I'm writing a script in Python, and using argparse to parse my arguments. The script is supposed to compare two different "aligners" from a pool of available aligners, and each aligner has some configuration options.

I want to be able to call my script with something like:

./script.py --aligner aligner1 --param 12 --aligner aligner2 --param 30 --other_param 28

I want to get out of this some sort of structure where the first --param option "belongs" to the first --aligner option, and the second --param and the --other_param options "belong" to the second --aligner option.

Is argparse capable of this sort of structured option parsing?

If so, what is the best way to do it? If not, is there another library I should look at?

Is there a drastically better UI design that I could be using instead of this?

4

1 回答 1

1

一般来说,我认为您想要的是不可能的,因为您无法将可选参数值关联在一起。也就是说,我看不到如何--param 12--aligner aligner1.

然而。

您可以argparse按如下方式使用:

p = argparse.ArgumentParser ()
p.add_argument ("--aligner", action="append", nargs="+")

这将创建多个对齐参数,每个参数至少需要一个参数(对齐名称)。然后,您可以使用额外的编码方案(您可以在解析器的帮助文本中记录该方案)对每个对齐器的参数进行编码。例如,您可以使用以下命令调用您的脚本:

./script.py --aligner aligner1 param=12 --aligner aligner2 param=30 other_param=28

然后,您将每个对齐器的附加参数拆分为 a list,拆分'=',然后创建 a dict。可能会使用一组默认参数进行更新。

于 2013-07-09T01:58:20.920 回答