python-2.7 - Python argparse AssertionError 当参数数量超过阈值时

标签 python-2.7 argparse

我正在尝试编写一个包含很多参数的 Python 脚本,我想使用 argparse 模块将这些参数分成清晰的组,如下所示:

import argparse
parser = argparse.ArgumentParser(description='Placeholder text', add_help=False)
req = parser.add_argument_group('required arguments')
req.add_argument('-m','--mode', action='store', dest='mode', help='Operation mode', choices=['single', 'multi'], required=True, metavar='')
req.add_argument('-s','--snps', action='store', dest='snps', help='SNP BED file', required=True, metavar='')
req.add_argument('-r','--reads', action='store', dest='reads', help='Mapped reads file [sam or bam]', required=True, metavar='')
uni = parser.add_argument_group('universal optional arguments')
uni.add_argument('-p','--prefix', action='store', dest='prefix', help='Prefix for temp files and output [Default: TEST]', default='TEST', metavar='')
uni.add_argument('-b','--bam', action='store_true', dest='bam', help='Mapped read file type is bam (auto-detected if *.bam)')
uni.add_argument('-t','--single', action='store_true', dest='single', help='Mapped reads are single-end [Default: False]')
uni.add_argument('-n','--noclean', action='store_true', dest='noclean', help='Do not delete intermediate files (for debuging)')
uni.add_argument('-h', '--help', action='help', help='show this help message and exit')
mult = parser.add_argument_group('multi(plex) mode arguments')
mult.add_argument('-j','--jobs', action='store', dest='jobs', type=int, help='Divide into # of jobs [Default: 100]', default=100, metavar='')
mult.add_argument('-w','--walltime', action='store', dest='walltime', help='Walltime for each job [Default: 3:00:00]', default='3:00:00', metavar='')
mult.add_argument('-k','--mem', action='store', dest='memory', help='Memory for each job [Default: 5000MB]', default='5000MB', metavar='')
single = parser.add_argument_group('single mode arguments')
single.add_argument('-f','--suffix', action='store', dest='suff', help='Suffix for multiplexed files [set automatically]', default='', metavar='')

args = parser.parse_args()

但是,当我使用“-h”选项运行此脚本时,它没有打印帮助文本,而是显示以下错误:
Traceback (most recent call last):
  File "CountSNPLevelASE.py", line 61, in <module>
    args = parser.parse_args()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1688, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1720, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1926, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1866, in consume_optional
    take_action(action, args, option_string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1794, in take_action
    action(self, namespace, argument_values, option_string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 994, in __call__
    parser.print_help()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2327, in print_help
    self._print_message(self.format_help(), file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2301, in format_help
    return formatter.format_help()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 279, in format_help
    help = self._root_section.format_help()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 209, in format_help
    func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 330, in _format_usage
    assert ' '.join(opt_parts) == opt_usage
AssertionError

奇怪的是,如果我注释掉任何非必需的参数,我会得到正确的帮助文本输出,如下所示:
usage: CountSNPLevelASE.py -m  -s  -r  [-b] [-t] [-n] [-h] [-j] [-w] [-k] [-f]

Placeholder text

required arguments:
  -m , --mode       Operation mode
  -s , --snps       SNP BED file
  -r , --reads      Mapped reads file [sam or bam]

universal optional arguments:
  -b, --bam         Mapped read file type is bam (auto-detected if *.bam)
  -t, --single      Mapped reads are single-end [Default: False]
  -n, --noclean     Do not delete intermediate files (for debuging)
  -h, --help        show this help message and exit

multi(plex) mode arguments:
  -j , --jobs       Divide into # of jobs [Default: 100]
  -w , --walltime   Walltime for each job [Default: 3:00:00]
  -k , --mem        Memory for each job [Default: 5000MB]

single mode arguments:
  -f , --suffix     Suffix for multiplexed files [set automatically]

对于为什么会发生这种情况,我找不到明确的解释。我试过删除所有空格无济于事。有任何想法吗? (如果有帮助,我在 Mac OS X 10.10.2 上使用 Python 2.7.6)。

最佳答案

问题在于包装用法。 usage 时显示正常适合一行,但将其拆分为 2 时失败。

有一个涉及此 assertion 的已知错误问题.使用格式化代码很脆弱,可以提出这个assertion使用中存在“常用”字符时出错,包括和额外的空格。

这是metavar=''导致问题的 3 个必需参数中的一个。可选的选项可以有一个 '' 元变量。

注意到简短用法行中这些参数之间的双空格吗?

...py -m  -s  -r  [-b]...
assertion error是那些空格没有完整地通过包装代码的结果。

如果您无法为这 3 个参数找到合适的替代元变量,那么我们将不得不弄清楚如何修改您的 argparse。代码。可能性包括注释掉断言语句,或从单行用法中删除那些额外的空白。最大的修复是更强大的使用格式化程序。

关于python-2.7 - Python argparse AssertionError 当参数数量超过阈值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157874/

相关文章:

Python 将 *args 转换为列表

python - 通过合并两列数据来计算有多少个唯一标识符?

python - 替换 Python 中的非 ASCII 字符 : eg, ' vs. ’

python-2.7 - 导入错误 : No module named twisted. 互联网

python - 在 Python 的 argparse 中多次使用相同的选项

python - 是什么使得可选参数是可选的而位置参数是必需的?

python - 如何使用 argparse IP 地址和社区字符串?

python - 帮助屏幕中忽略了 argparse 的 "required"?

mysql - 在python中构建多行数据库查询

Python argparse 如果不是 int 则跳过