命令行提示错误
我的程序需要2个或3个命令行参数:命令行提示错误
-s是一个可选参数,指示以后 -infile在我的程序的开关是文件输入 -outfile是成为书面文件
我需要我的程序打印错误消息并退出如果下列任何发生:
- 用户指定不.genes结束一个INFILE名
- 个用户细节不与任一.fa或.fasta
- 用户提供小于2,或3个以上,参数
- 用户的第一参数开始以短划线结束,但不是“一个OUTFILE名-s'
我已经写:
def getGenes(spliced, infile, outfile):
spliced = False
if '-s' in sys.argv:
spliced = True
sys.argv.remove('-s')
infile, outfile = sys.argv[1:]
if not infile.endswith('.genes'):
print('Incorrect input file type')
sys.exit(1)
if not outfile.endswith('.fa' or '.fasta'):
print('Incorrect output file type')
sys.exit(1)
if not 2 <= len(sys.argv) <= 3:
print('Command line parameters missing')
sys.exit(1)
if sys.argv[1] != '-s':
print('Invalid parameter, if spliced, must be -s')
sys.exit(1)
然而,有些东西用一些条件句的相互冲突,其中包括第一个和最后一个是矛盾的,由于这样的事实,s.argv [1]如果'pre',总是不等于'-s' nt in argv,它早先被删除了。所以,我不知道如何正确地写这个...
sliced=False
没有缩进
def getGenes(spliced, infile, outfile):
spliced = False
sys.argv.remove('s')
应该sys.argv.remove('-s')
两个条件相互矛盾:
if '-s' in sys.argv:
spliced = True
sys.argv.remove('-s') # you removed '-s' from sys.argv ,so the below if condition becomes false
infile, outfile = sys.argv[1:]
if sys.argv[1] != '-s':
print('Invalid parameter, if spliced, must be -s')
sys.exit(1)
已修改的程式码:
import sys
def getGenes(spliced, infile, outfile):
spliced = False
if '-s' in sys.argv:
spliced = True
infile, outfile = sys.argv[2:]
if not infile.endswith('.genes'):
print('Incorrect input file type')
sys.exit(1)
if not outfile.endswith('.fa' or '.fasta'):
print('Incorrect output file type')
sys.exit(1)
if not 3 <= len(sys.argv) <= 4:
print('Command line parameters missing')
sys.exit(1)
if sys.argv[1] != '-s':
print('Invalid parameter, if spliced, must be -s')
sys.exit(1)
你为什么将2和3改为3和4以上? – 2012-04-24 21:32:28
是的,因为我没有从sys.srgv中删除'-s',所以它的长度现在是4。 – 2012-04-24 21:34:33
谢谢!我认为这有效! – 2012-04-24 21:42:08
只是挑剔,但错误应发送到stderr。 – 2012-04-24 21:15:55
什么是'stderr'? – 2012-04-24 21:17:50
'sys.argv.remove('s')''' - >'-s',不是? – 2012-04-24 21:17:57