简体   繁体   English

python,如何使参数接受参数组合?

[英]python, How to make an argument accept a combination of arguments?

I did see another post on making python args accept multiple inputs but I don't think that applies to my question here.我确实看到了另一篇关于使 python args 接受多个输入的帖子,但我认为这不适用于我的问题。

I have a script that executes using command line and I made an argument where I could input a model and based on the model, my script will update a file for me that I can use in another program (this is for codeml.ctl file for PAML in case anyone is interested).我有一个使用命令行执行的脚本,我提出了一个参数,我可以在其中输入一个模型,并根据该模型,我的脚本将为我更新一个文件,我可以在另一个程序中使用该文件(这是用于 codeml.ctl 文件的) PAML 以防万一有人感兴趣)。

I execute the script as:我执行脚本为:

python my_script.py -g=geneA.phy -t=treefile.phy -M="0 1 2 3"

But sometimes I may want to rerun just one model so:但有时我可能只想重新运行一个模型,因此:

-M=0

Or perhaps I want to rerun two random models或者我想重新运行两个随机模型

-M="0 3"

In my python script, I have it setup so based on the model input for my -M flag, I edit lines in the control file.在我的 python 脚本中,我根据 -M 标志的模型输入进行了设置,我编辑了控制文件中的行。

def main():
    parser = argparse.ArgumentParser(description="Update codeml.ctl file using model names")
    parser.add_argument('-M', '--modeltype', type=str)
    parser.add_argument('-g', '--genefile', type=str)
    parser.add_argument('-t', '--treefile', type=str)
    args = parser.parse_args()

def lines_to_replace(args):
    replace_seq = args.genefile
    replace_treefile = args.treefile
    replace_outfile = args.genefile.split(".phy")[0] + "_OUT"

def model(args):
    for index, line in enumerate(fileinput.input('codeml.ctl', inplace=1)):
        if args.modeltype == '0 1 2 3':
             if index == 30:
                 line = "Model is 0 1 2 3"; print(line)
        if args.modeltype == '0 1 2':
             if index == 30:
                 line = "Model is 0 1 2"; print(line)

And I would do this but this is tedious and I cannot figure out a way for my args.modeltype to accept a combination of model inputs.我会这样做,但这很乏味,我无法找到一种方法让我的 args.modeltype 接受模型输入的组合。 The actual file only takes model inputs with spaces in between and it has to be on one line.实际文件只采用中间有空格的模型输入,它必须在一行上。 It would have to be something like:它必须是这样的:

 if args.modeltype == ???:
      if index == 30:
           line = "Model is" + str(???); print(line)

Combinations always confuse me.组合总是让我感到困惑。 Would something like *args work for this, but then how would I parse *args?像 *args 这样的东西会为此工作,但是我将如何解析 *args? Perhaps using user input and saving that as a variable would work?也许使用用户输入并将其保存为变量会起作用? I've never used that before but I will try it.我以前从未使用过,但我会尝试一下。


Edit to be more clear:编辑更清楚:

I want my model argument to accept arguments from this list of models [0, 1, 2, 3].我希望我的模型参数接受来自这个模型列表 [0, 1, 2, 3] 的参数。 I can just type 0, or I could type "0 1" or even "2 3" and the script should execute and update the model line (index == 30) appropriately.我可以只输入 0,或者我可以输入“0 1”甚至“2 3”,脚本应该执行并适当地更新模型行(索引 == 30)。 However, if I type "9 99" that is wrong because those models don't exist.但是,如果我输入“9 99”,那是错误的,因为这些模型不存在。 The suggestions below work where I use nargs="+" and then " ".join(args.model), but this still accepts incorrect input.下面的建议适用于我使用 nargs="+" 然后使用 " ".join(args.model),但这仍然接受不正确的输入。 Is there a way to input the list of accepted models into my parser.add_argument('model', type='str', nargs='+', help='Input model').有没有办法将接受的模型列表输入到我的 parser.add_argument('model', type='str', nargs='+', help='Input model') 中。

If you replaced your -M argument with the following:如果您将-M参数替换为以下内容:

parser.add_argument('models', metavar='N', type=int, nargs='+',
                help='list of models to run')

then args.models would be a list of the models you specified, as in那么args.models将是您指定的模型列表,如

python my_script.py -g=geneA.phy -t=treefile.phy 0 1 2 3

would result in args.models being ['0', '1', '2', '3'] , which you can then do whatever you want with (to be honest, your question is not at all clear on what you want done with this information).会导致args.models['0', '1', '2', '3'] ,然后你可以做任何你想做的事情(说实话,你的问题根本不清楚你想要什么完成此信息)。

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

相关问题 如何在python中使参数接受多个输入 - How to make an argument in python accept multiple inputs 如何编写python函数来接受*参数和关键字参数? - How can I write a python function to accept *arguments and a keyword argument? 如何让 Python argparse 接受“-a”标志代替位置参数? - How to make Python argparse accept a "-a" flag in place of the positional argument? python argparse - 如何通过使用 arguments 的组合来防止 - python argparse - How to prevent by using a combination of arguments 如何接受文件或路径作为python中方法的参数 - how to accept file or path as arguments to method in python 如何在Python 3中仅接受字符串类型的参数 - How to accept only string type arguments in Python 3 如何重写此python函数以接受列表作为参数? - How to rewrite this python function to accept lists as arguments? 如何制作一个 Discord Bot 可以接受来自 Python 的 arguments 的什么消息发送给谁并能够关闭自己? - How to make a Discord Bot that can accept arguments from Python of what message to send to who and be able to close itself? 在 python 类型提示中,如何让参数接受基类的任何子类? - In python type-hinting, how can I make an argument accept any subclass of a base class? Python:接受多个参数 - Python: Accept multiple arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM