Options with Options with Python argparse?

时间秒杀一切 提交于 2019-12-06 06:18:47

In general, I think what you want is impossible because you cannot associate the optional parameter values together. That is, I can't see how to associate --param 12 with --aligner aligner1.

However.

You can use argparse as follows:

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

This will create multiple aligner arguments, each requiring at least one argument (the aligner name). You then can use an additional encoding scheme (that you can document in the help text for the parser) which encodes the parameters for each aligner. For example, you could call your script with:

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

You then split out the additional arguments for each aligner into a list, split by '=' and then create a dict. Potentially updating with a default set of arguments.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!