for name, value in opts: if name in ('-h', '--help'): usage() sys.exit() elif name in ('-i', '--ifile'): input_file = value elif name in ('-o', '--ofile'): output_file = value print(f'<<< Input file is {input_file} <<<') print(f'>>> Output file is {output_file} >>>')
if __name__ == '__main__': main(sys.argv)
演示結果
1 2 3 4 5
$ python demo-getopt.py -h # Usage:demo-getopt.py [-i name | --ifile=name][-o name | --ofile=name] # -h help # -i input file path # -o output file path
1 2 3
$ python demo-getopt.py -i text.txt --ofile=data.csv # <<< Input file is text.txt <<< # >>> Output file is data.csv >>>