python - 如何向 python 脚本添加命令行参数帮助?
问题描述
我正在尝试创建一个 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
为此,我试图设置争论,以便如果使用以下命令,帮助输出可以帮助用户:
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
错误信息:
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 <module>
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 <listcomp>
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'
解决方案
usage
关键字 toArgumentParser
只允许使用格式%(prog)s
说明符。 %(arg.py)s
不是有效的格式说明符。要么写usage='arg.py [options]'
,要么写usage='%(prog)s [options]'
。
推荐阅读
- typescript - AWS S3 - 遇到错误“Jest 检测到以下 1 个打开的句柄可能阻止 Jest 退出”
- asp.net - 在 NET 应用程序中处理传入的 Postfix 电子邮件
- sql - 查找具有不同键的两个表之间的差异
- ssl - 如何修复自托管 nginx 服务器上的无限重定向循环?
- python - 如何通过 Python 脚本使用 Volatility3 插件?
- sql - 如何在我的 Date 变量上实现这个 CASE 语句逻辑?
- android - 如何过滤firebase中的值?
- docker - 如何设置 docker nextcloud 以使用 SSL
- tensorflow - add_loss() 如何处理复合模型?
- c# - 如何“禁用”计算列以便应用迁移?