我想创建一个使用文件和目录的非常简单的终端菜单,所以我尝试了以下内容:
if __name__ == '__main__':
p = argparse.ArgumentParser(NAME)
p.add_argument('-data:', '--input_data', default=sys.stdin)
p.add_argument('-labels:', '--targets', default=sys.stdin)
p.add_argument('-input_directory:', '--input', default=sys.stdin)
p.add_argument('-output_data:', '--output', default=sys.stdout)
opts = p.parse_args()
if not any([opts.input, opts.output, opts.targets,opts.input_data]):
p.print_usage()
quit()
print '\npress -h to see the help\n'
elif any([opts.targets,opts.input_data]):
print '\ncompleted action\n'
p.print_usage()
#Recibe (datos, etiquetas)
perform_stuff(opts.input_data, opts.targets)
quit()
#Here is the problem:
elif any([opts.input, opts.output]):
print '\ncompleted action\n'
retrive(opts.input)
write(opts.output)
请注意,为简单起见,我省略了perform_stuff(path/in/file, path/out/file)
,retrive(opts.input)
和write(opts.output)
实施。问题是唯一有效的命令是:
if not any([opts.input, opts.output, opts.targets,opts.input_data]):
p.print_usage()
quit()
print '\npress -h to see the help\n'
elif any([opts.targets,opts.input_data]):
print '\ncompleted action\n'
p.print_usage()
#Recibe (datos, etiquetas)
perform_stuff(opts.input_data, opts.targets)
quit()
另一方面,当我尝试跑步时:
#Here is the problem:
elif any([opts.input, opts.output]):
print '\ncompleted action\n'
retrive(opts.input)
write(opts.output)
我的简单程序就冻结了。我是argparse的新手,我猜这个问题与正确使用if / else语句有关。因此,我应该如何创建简单程序的菜单?
答案 0 :(得分:1)
我已经清理并压缩了您的代码,重点是让if / else树工作。
在解析器中,我将short options
重写为单个字符。我还将默认值保留为None
。这使if
测试更容易。 (如果opts.input
为stdin
并且您测试if not opts.input:
,该怎么办?)。
import argparse
def quit():
import sys
sys.exit(1)
def perform_stuff(*args):
print 'stuff', args
def retrive(*args):
print 'retrieve', args
if __name__ == '__main__':
p = argparse.ArgumentParser()
# single letter `-d` string; use default None
p.add_argument('-d', '--input_data')
p.add_argument('-t', '--targets')
p.add_argument('-i', '--input')
p.add_argument('-o', '--output')
opts = p.parse_args()
print 'opts', opts
if not any([opts.input, opts.output, opts.targets, opts.input_data]):
p.print_usage()
print 'press -h to see the help'
quit()
elif any([opts.targets,opts.input_data]):
perform_stuff(opts.input_data, opts.targets)
quit()
elif any([opts.input, opts.output]):
retrive(opts.input, opts.output)
else:
print 'else'
样品运行:
1814:~/mypy$ python stack36877714.py
opts Namespace(input=None, input_data=None, output=None, targets=None)
usage: stack36877714.py [-h] [-d INPUT_DATA] [-t TARGETS] [-i INPUT]
[-o OUTPUT]
press -h to see the help
1815:~/mypy$ python stack36877714.py -d input_data
opts Namespace(input=None, input_data='input_data', output=None, targets=None)
stuff ('input_data', None)
1815:~/mypy$ python stack36877714.py -t targets
opts Namespace(input=None, input_data=None, output=None, targets='targets')
stuff (None, 'targets')
1815:~/mypy$ python stack36877714.py -i input
opts Namespace(input='input', input_data=None, output=None, targets=None)
retrieve ('input', None)
1816:~/mypy$ python stack36877714.py -o output
opts Namespace(input=None, input_data=None, output='output', targets=None)
retrieve (None, 'output')
您的功能可以写成:
def retrieve(in_arg, out_arg):
if in_arg is None:
data = sys.stdin.read()
# cannot open/close stdin
else:
with open(in_arg,'r') as f:
data = f.read()
if out_arg is None:
# write to sys.stdout
else:
# write to open(out_arg,'w')