首页 > 解决方案 > 如何向 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'

标签: python

解决方案


usage关键字 toArgumentParser只允许使用格式%(prog)s说明符。 %(arg.py)s不是有效的格式说明符。要么写usage='arg.py [options]',要么写usage='%(prog)s [options]'


推荐阅读