我必须编写一个从文件中读取的程序,并将一些分析写入文本文件。该程序必须通过命令行获取一些信息,但我看不到,即使给出了模板,也要弄明白。我写了一个测试程序,看看我是否可以成功地将命令行输入传递给类。
#!/usr/bin/env python3
########################################################################
# CommandLine
########################################################################
class CommandLine() :
'''
Handle the command line, usage and help requests.
CommandLine uses argparse, now standard in 2.7 and beyond.
it implements a standard command line argument parser with various argument options,
a standard usage and help, and an error termination mechanism do-usage_and_die.
attributes:
all arguments received from the commandline using .add_argument will be
avalable within the .args attribute of object instantiated from CommandLine.
For example, if myCommandLine is an object of the class, and requiredbool was
set as an option using add_argument, then myCommandLine.args.requiredbool will
name that option.
'''
def __init__(self, inOpts=None) :
'''
CommandLine constructor.
Implements a parser to interpret the command line argv string using argparse.
'''
import argparse
self.parser = argparse.ArgumentParser(description = 'Program prolog - a brief description of what this thing does',
epilog = 'Program epilog - some other stuff you feel compelled to say',
add_help = True, #default is True
prefix_chars = '-',
usage = '%(prog)s [options] -option1[default] <input >output'
)
self.parser.add_argument('inFile', action = 'store', help='input file name')
self.parser.add_argument('outFile', action = 'store', help='output file name')
self.parser.add_argument('-lG', '--longestGene', action = 'store', nargs='?', const=True, default=True, help='longest Gene in an ORF')
self.parser.add_argument('-mG', '--minGene', type=int, choices= range(0, 2000), action = 'store', help='minimum Gene length')
self.parser.add_argument('-s', '--start', action = 'append', nargs='?', help='start Codon') #allows multiple list options
self.parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
if inOpts is None :
self.args = self.parser.parse_args()
else :
self.args = self.parser.parse_args(inOpts)
########################################################################
#MAIN GOES HERE
########################################################################
def main(myCommandLine=None):
'''
Implements the Usage exception handler that can be raised from anywhere in process.
'''
myCommandLine = CommandLine(myCommandLine)
#myCommandLine.args.inFile #has the input file name
#myCommandLine.args.outFile #has the output file name
#myCommandLine.args.longestGene #is True if only the longest Gene is desired
#myCommandLine.args.start #is a list of start codons
#myCommandLine.args.minGene #is the minimum Gene length to include
print (myCommandLine.args) # print the parsed argument string .. as there is nothing better to do
if myCommandLine.args.longestGene:
print ('longestGene is', str(myCommandLine.args.longestGene) )
else :
pass
class Test:
def __init__(self):
print(myCommandLine.args.minGene)
if __name__ == "__main__":
main()
class Test:
def __init__(self):
self.test()
def test(self, infile = myCommandLine.args.inFile, outfile = myCommandLine.args.outFile, longest = myCommandLine.args.longestGene, start = myCommandLine.args.start, min = myCommandLine.args.minGene):
print(infile)
print(outfile)
print(longest)
print(start)
print(min)
new_obj = Test()
命令行输入应如下所示:python testcommand.py -minG 100 -longestG -starts ATG tass2ORFdata-ATG-100.txt
据说主程序在“MAIN GOES HERE”的位置,但是当我尝试时,我收到“myCommandline未定义”的错误。所以我把程序移到了最后。但我收到错误'the'&gt;'运营商留作将来使用“
如果重要的话,我正在使用Powershell。如何将这些数据输入我的班级?
答案 0 :(得分:0)
您不需要CommandLine类。正如James Mills所建议的,这是一个例子:
import argparse
class Test:
def __init__(self, infile, outfile, longest, start, min):
self.infile = infile
self.test()
def test(self):
print(self.infile)
def main():
parser = argparse.ArgumentParser(description = 'Program prolog',
epilog = 'Program epilog',
add_help = True, #default is True
prefix_chars = '-',
usage = 'xxx')
parser.add_argument('-i', '--inFile', action = 'store', help='input file name')
parser.add_argument('-o', '--outFile', action = 'store', help='output file name')
parser.add_argument('-lG', '--longestGene', action = 'store', nargs='?', const=True, default=True, help='longest Gene in an ORF')
parser.add_argument('-mG', '--minGene', type=int, choices= range(0, 20), action = 'store', help='minimum Gene length')
parser.add_argument('-s', '--start', action = 'append', nargs='?', help='start Codon') #allows multiple list options
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
args = parser.parse_args()
test = Test(args.inFile, args.outFile, args.longestGene, args.minGene, args.start)
if __name__ == '__main__':
main()