我尝试创建一个python脚本,使用命令行中的以下命令可以运行脚本: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
为此,我尝试设置一些争论,以便在使用以下命令时,帮助输出可以帮助用户:
^{2}$
到目前为止,我有以下脚本,但有些错误我似乎无法修复: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 -out_file -unk_file -n_mismatch -min_len -forward -reverse
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
错误消息:Traceback (most recent call last):
File "/usr/lib/python3.6/argparse.py", line 1775, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib/python3.6/argparse.py", line 1981, in _parse_known_args
start_index = consume_optional(start_index)
File "/usr/lib/python3.6/argparse.py", line 1889, in consume_optional
raise ArgumentError(action, msg % explicit_arg)
argparse.ArgumentError: argument -h/--help: ignored explicit argument 'elp'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "arg.py", line 18, in
args = parser.parse_args()
File "/usr/lib/python3.6/argparse.py", line 1743, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python3.6/argparse.py", line 1782, in parse_known_args
self.error(str(err))
File "/usr/lib/python3.6/argparse.py", line 2400, in error
self.print_usage(_sys.stderr)
File "/usr/lib/python3.6/argparse.py", line 2370, in print_usage
self._print_message(self.format_usage(), file)
File "/usr/lib/python3.6/argparse.py", line 2336, in format_usage
return formatter.format_help()
File "/usr/lib/python3.6/argparse.py", line 291, in format_help
help = self._root_section.format_help()
File "/usr/lib/python3.6/argparse.py", line 222, in format_help
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.6/argparse.py", line 222, in
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.6/argparse.py", line 308, in _format_usage
usage = usage % dict(prog=self._prog)
KeyError: 'arg.py'