- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个包含很多参数的 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()
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]
最佳答案
问题在于包装用法。 usage
时显示正常适合一行,但将其拆分为 2 时失败。
有一个涉及此 assertion
的已知错误问题.使用格式化代码很脆弱,可以提出这个assertion
使用中存在“常用”字符时出错,包括和额外的空格。
这是metavar=''
导致问题的 3 个必需参数中的一个。可选的选项可以有一个 '' 元变量。
注意到简短用法行中这些参数之间的双空格吗?
...py -m -s -r [-b]...
assertion error
是那些空格没有完整地通过包装代码的结果。
argparse
。代码。可能性包括注释掉断言语句,或从单行用法中删除那些额外的空白。最大的修复是更强大的使用格式化程序。
关于python-2.7 - Python argparse AssertionError 当参数数量超过阈值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157874/
我正在尝试使用 parser = argparse.ArgumentParser对于我写的一个小程序。 程序接受作为输入 EITHER ( txt 文件的路径 ) OR ( opt1 && opt2
我为需要遵循特定格式的参数定义了自定义正则表达式类型。我使用了另一篇文章 ( regex custom type ) 中的代码,它非常有用。我的问题是我正在编写单元测试,我希望正则表达式失败并尝试断言
我写了下面的代码。 import argparse parser = argparse.ArgumentParser() parser.add_argument('-v', '--version',
我一定遗漏了一些明显的东西。目标是使用 argparse,第一个参数是必需的,第二个是可选的,其他任何剩余参数都是可选的。 为了展示这个问题,我制作了两个测试解析器;它们之间的唯一区别是在一个中使用
我正在努力寻找一种将参数传递给 docker container 中的 python 脚本的方法。基于ubuntu 。我正在与docker-compose.yml合作. 请查看下面的示例! docke
我正在努力寻找一种将参数传递给 docker container 中的 python 脚本的方法。基于ubuntu 。我正在与docker-compose.yml合作. 请查看下面的示例! docke
我像这样创建一个 argparser: parser = argparse.ArgumentParser(description='someDesc') parser.add_argument
我正在编写一个脚本,其中包含 2 个相互排斥的参数,以及一个仅对其中一个参数有意义的选项。如果您使用毫无意义的参数调用它,我会尝试将 argparse 设置为失败。 要清楚: -m -f 有意义 -s
我正在使用 Python 3.6 和 argparse 1.1。 除了 -h/--help 我还想有一个选项 -v/--version打印带有版本信息的字符串并退出程序(就像使用帮助字符串一样)。但是
有没有办法将来自父解析器的参数分组到不同的组中?我无权访问父解析器本身,所以我不能在那里添加组。 (我使用的是 Google 的 OAuth2 框架)。 目前我的代码是: # test.py from
GNU grep 有一个参数可以在匹配行周围打印一定数量的额外行。从手册页: -C NUM, -NUM, --context=NUM Print NUM lines of output context
现在,我有一个脚本可以使用 argparse 接受命令行参数。例如,像这样: #foo.py def function_with_args(optional_args=None): parser
我在脚本中的 argparse 解析器中添加了一些参数,如下所示: parser = argparse.ArgumentParser() parser.add_argument("--eval_mod
我在 python 代码中使用 argparse 作为参数解析器。将字典解析为 argparse 对象的最佳方法是什么? 例如,我的字典是: { "activation_dropout": 0
我正在使用 argparse从命令行接收输入以运行我的脚本。 我当前的输入字符串如下所示: path> python -t T1 T2 T3 -f F1 F2 argparse 中是否有一个参数,而
我看到了有关使用 argparse 库将字典和列表传递给 Python 的问题。这些示例都显示了我的 Python 代码的样子。但是没有人告诉我它们在命令行上的样子。我在哪里需要大括号、方括号和引号?
我想模拟大多数命令行实用程序的行为,其中可选参数可以放在命令行中的任何位置,包括位置参数之间,例如 mkdir例子: mkdir before --mode 077 after 在这种情况下,我们知道
我想获取传递给 sys.argv 的所有参数有格式someprogram.py --someparameter 23 -p 42 -anotherparam somevalue . 结果我正在寻找一个
这个问题在这里已经有了答案: Argparse optional positional arguments? (3 个回答) 1年前关闭。 我如何才能设置我的 argparser 以具有以下行为? b
这是我的简单 test.py 脚本: import argparse parser = argparse.ArgumentParser('A long string that goes on and
我是一名优秀的程序员,十分优秀!