0
커맨드 라인에서 실행되는 Python 프로그램을 만들었습니다. 사용자가 처리 할 파일을 입력합니다. 파일은 fileinput에서 읽어 들이고 optparse 옵션을 사용할 수 있습니다. 내 문제는 사용자가 옵션이나 파일 이름을 입력하지 않으면 프로그램이 아무 것도하지 않고 계속 실행된다는 것입니다. fileinput이 비어 있으면 프로그램이 기본적으로 도움말 옵션을 표시하기를 원합니다.fileinput이 비어 있는지 확인하는 방법? Python
fileinput.input (argv)이 비어 있는지 확인하는 방법이 있습니까? 그것 defaults to stdin when empty,하지만 내가 비어 있는지 미리 확인할 수 있습니까? optparse
파서가 위치 인수 목록을 반환하는 경우
def parse_options():
parser = optparse.OptionParser()
parser.add_option('-o', '--output', dest='output',
default='c',
help='[c/f/h] output to (c)onsole, (f)ile or (h)tml')
parser.add_option('-s', '--sort', dest='sort',
default='pa',
help='[p/c/m/d] sort by (p)ath, (c)all frequency, (m)ean duration or (d)uration,\n'
'[a/d] sort by (a)scending or (d)escending order')
options, argv = parser.parse_args()
if options.output == 'f':
output_action = LogAnalyser.output_to_file
elif options.output == 'h':
output_action = LogAnalyser.output_to_html
else:
output_action = LogAnalyser.output_to_console
#if fileinput.input(argv) is None:
# parser.print_help()
# quit()
return output_action, options.sort, fileinput.input(argv)
사용자가 파일 이름을 입력하는 것만으로 프로그램을 사용할 수 있기를 바랍니다. 따라서 옵션 인수를 항상 사용할 수있는 것은 아닙니다. 미안하지만 분명히하지 않았어. – user3075082
* 어디에서 * 파일 이름을 입력 하시겠습니까? – Netch
커맨드 라인에서, 파이썬 scriptname.py file1.txt file2.txt에 -o f와 같은 가능한 옵션을주고 싶습니다. – user3075082