如何在python脚本中添加命令行参数帮助?

时间:2018-12-02 21:45:15

标签: python

我正在尝试创建一个python脚本,在命令行中使用以下命令可以运行该脚本:

webmail

为此,我正在尝试建立论据,以便如果使用以下命令,则帮助输出可以为用户提供帮助:

python3 myprogramname.py –in_file input.fasta –out_file output.fasta –unk_file unknown.fasta –n_mismatch 3 –min_len 150 -forward GTGCCAGCMGCCGCGGTAA -reverse ACAGCCATGCANCACCT

到目前为止,我有以下脚本,但是似乎无法修复错误:

python3 myprogramname.py -help

理想情况下,from sys import argv, exit import argparse parser = argparse.ArgumentParser(prog="arg.py", usage="%(arg.py)s [options]") #Mandatory arguments parser.add_argument("--in_file", help = "The name of the input FASTA file.") parser.add_argument("--out_file", help = "The name of the trimmed reads file.") parser.add_argument("--unk_file", help = "The name of the file containing unprocessed reads.") parser.add_argument("--n_mismatch", help = "The tolerance for mismatches in the forward or reverse primer sequence.") parser.add_argument("--min_len", help = "The minimum length a sequence must be in order to be processed") parser.add_argument("--h", help = "Insert helpful information.") #Parse arguments args = parser.parse_args() parser.print_help() 的输出应产生以下内容:

python3 myprogramname.py -help

错误消息:

usage: myprogramname.py -in_file <inputfile> -out_file <outfile> -unk_file <unknownfile> -n_mismatch <mismatchnumber> -min_len <minimumsequencelengthnumber> -forward <forwardprimerstring> -reverse <reverseprimerstring>

required arguments:
--in_file The name of the input FASTA file
--out_file The name of the trimmed reads file
--unk_file The name of the file containing unprocessed reads
--n_mismatch The tolerance for mismatches in the forward or reverse primer sequence
--min_len The minimum length a sequence must be in order for it to be processed
--h The help information for your program

1 个答案:

答案 0 :(得分:1)

ArgumentParser的{​​{3}}关键字仅允许使用%(prog)s格式说明符。 %(arg.py)s不是有效的格式说明符。写usage='arg.py [options]'或写usage='%(prog)s [options]'