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

为此,我尝试设置一些争论,以便在使用以下命令时,帮助输出可以帮助用户:

^{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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值