从shell获取多个参数的Python脚本

从shell获取多个参数的Python脚本,python,getopt,Python,Getopt,我试图从终端获取多个文件作为输入。输入编号可能至少为1到多个。这是我的程序的输入 F3.py -e <Energy cutoff> -i <inputfiles> F3.py-e-i 我希望参数-I取1到多个之间的任意数量的值 F3.py -e <Energy cutoff> -i file1 file2 F3.py -e <Energy cutoff> -i *.pdb F3.py-e-i文件1文件2 F3.py-e-i*.pdb 现在它

我试图从终端获取多个文件作为输入。输入编号可能至少为1到多个。这是我的程序的输入

F3.py -e <Energy cutoff> -i <inputfiles>
F3.py-e-i
我希望参数-I取1到多个之间的任意数量的值

F3.py -e <Energy cutoff> -i file1 file2
F3.py -e <Energy cutoff> -i *.pdb
F3.py-e-i文件1文件2
F3.py-e-i*.pdb
现在它只需要第一个文件,然后停止。 这就是我到目前为止所做的:

def main(argv):
try:
    opts,args=getopt.getopt(argv,"he:i:")
    for opt,arg in opts:
        if opt=="-h":
            print 'F3.py -e <Energy cutoff> -i <inputfiles>'
            sys.exit()
        elif opt == "-e":
            E_Cut=float(arg)
            print 'minimum energy=',E_Cut
        elif opt == "-i":
            files.append(arg)
            print files
    funtction(files)
except getopt.GetoptError:
    print 'F3.py -e <Energy cutoff> -i <inputfiles>'
    sys.exit(2)
def主管道(argv):
尝试:
opts,args=getopt.getopt(argv,“he:i:”)
对于opt,opt中的参数:
如果opt==“-h”:
打印'F3.py-e-i'
sys.exit()
elif opt==“-e”:
E_切割=浮动(arg)
打印“最小能量=”,E_Cut
elif opt==“-i”:
files.append(arg)
打印文件
功能(文件)
除getopt.GetoptError外:
打印'F3.py-e-i'
系统出口(2)

任何帮助都将不胜感激。谢谢

尝试使用@larsks建议,下一个代码片段应该适合您的用例:

import argparse 

parser = argparse.ArgumentParser()

parser.add_argument('-i', '--input', help='Input values', nargs='+', required=True)

args = parser.parse_args()

print args
kwargs解释:

  • nargs
    允许您将值解析为一个列表,这样您就可以在args.input中使用类似于:
    for i的内容进行迭代
  • required
    强制此参数,因此必须至少添加一个元素
通过使用argparse模块,还可以使用-h选项来描述参数。因此,请尝试使用:

$ python P3.py -h
usage: a.py [-h] -i INPUT [INPUT ...]

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT [INPUT ...], --input INPUT [INPUT ...]
                        Input values


$ python P3.py -i file1 file2 filen
Namespace(input=['file1', 'file2', 'filen'])

尝试使用@larsks建议,下一个代码片段应该适合您的用例:

import argparse 

parser = argparse.ArgumentParser()

parser.add_argument('-i', '--input', help='Input values', nargs='+', required=True)

args = parser.parse_args()

print args
kwargs解释:

  • nargs
    允许您将值解析为一个列表,这样您就可以在args.input中使用类似于:
    for i的内容进行迭代
  • required
    强制此参数,因此必须至少添加一个元素
通过使用argparse模块,还可以使用-h选项来描述参数。因此,请尝试使用:

$ python P3.py -h
usage: a.py [-h] -i INPUT [INPUT ...]

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT [INPUT ...], --input INPUT [INPUT ...]
                        Input values


$ python P3.py -i file1 file2 filen
Namespace(input=['file1', 'file2', 'filen'])

如果您坚持使用
getopt
,则必须将多个参数与delimeter组合,而不是像
这样的空格,然后像这样相应地修改代码

import getopt
import sys

try:
    opts,args=getopt.getopt(sys.argv[1:],"he:i:")
    for opt,arg in opts:
        if opt=="-h":
            print 'F3.py -e <Energy cutoff> -i <inputfiles>'
            sys.exit()
        elif opt == "-e":
            E_Cut=float(arg)
            print 'minimum energy=',E_Cut
        elif opt == "-i":
            files = arg.split(",")
            print files
    #funtction(files)
except getopt.GetoptError:
    print 'F3.py -e <Energy cutoff> -i <inputfiles>'
    sys.exit(2)
注意
我已经对你的函数调用进行了注释,并从主函数中删除了“展开”你的代码,你可以在代码中重做这些事情,它不会改变你的结果。

如果你坚持使用
getopt
你将不得不将多个参数与delimeter组合,而不是像
那样使用空格,
然后像这样相应地修改代码

import getopt
import sys

try:
    opts,args=getopt.getopt(sys.argv[1:],"he:i:")
    for opt,arg in opts:
        if opt=="-h":
            print 'F3.py -e <Energy cutoff> -i <inputfiles>'
            sys.exit()
        elif opt == "-e":
            E_Cut=float(arg)
            print 'minimum energy=',E_Cut
        elif opt == "-i":
            files = arg.split(",")
            print files
    #funtction(files)
except getopt.GetoptError:
    print 'F3.py -e <Energy cutoff> -i <inputfiles>'
    sys.exit(2)
注意
我已经对您的函数调用进行了注释,并从主函数中删除了“展开”代码,您可以在代码中重做这些操作,但不会改变结果。

我建议阅读Python模块,它通常比
getopt
更容易使用。我建议阅读Python模块,一般来说,使用它比使用
getopt
更容易。谢谢,这很有效。但有一个问题是,为什么它会在“提示时显示可选参数-h”下显示所需的选项呢。不过有一个问题,为什么它会在“提示时显示可选参数-h”下显示所需选项谢谢,我想这在使用目录中的所有文件进行输入时会很困难。是的,getopt有其自身的限制,argparse肯定更好:)谢谢,我想当使用目录中的所有文件进行输入时,这会很困难