[英]Argparse arguments number limits
我正在嘗試將 bash 腳本轉換為 python 腳本,我必須使用 Argparser 創建很多參數。 當我用這個 arguments 運行代碼時,我得到了錯誤
'
File "/home/lmonari/anaconda3/envs/ChemFlowOfficial/lib/python3.9/argparse.py", line 338, in _format_usage
assert ' '.join(opt_parts) == opt_usage
AssertionError'
如果我隨機評論一些 arguments,代碼工作正常。 我是否達到了 Argparse 的最大數量 arguments? 我在任何地方都找不到記錄的限制
代碼:
parser = argparse.ArgumentParser(add_help=False)
# format the argument groups
parser._action_groups.pop()
# help arguments
help_args = parser.add_argument_group('[ Help ] ')
help_args.add_argument('-h', '--help', action='store_true',
help='Show this help message and exit.' )
help_args.add_argument("-H","--Help", action='help',
help="Detailed help.")
# checking the short help from the terminal_input
terminal_input=sys.argv
if '-h' in terminal_input or '--help' in terminal_input:
DockFlow_help_short()
parser.exit()
# required arguments
required = parser.add_argument_group('[ Required ] ')
required.add_argument('-p',"---project", metavar='',
help="STR : ChemFlow project.")
# required.add_argument("-r","--receptor", metavar='',
# help="Receptor's mol2 file.", required=True)
# required.add_argument("-l","--ligand", metavar='',
# help="FILE : Ligands MOL2 file.", required=True)
# required.add_argument("-dp","--program", metavar='',
# help="STR : plants, vina, qvina, smina.", required=True)
# post processing
post_pro = parser.add_argument_group('[ Post Processing ]')
post_pro.add_argument("--postprocess", metavar='',
help="Process DockFlow output for the specified project/protocol/receptor.")
post_pro.add_argument("--postprocess-all", metavar='',
help="Process DockFlow output in a ChemFlow project.")
post_pro.add_argument('-n',"--n_poses", metavar='',
help="INT : Number of docked poses to keep.")
# post_pro.add_argument("--archive", metavar='',
# help="Compress the docking folders for the specified project/protocol/receptor.")
# post_pro.add_argument("--archive_all", metavar='',
# help="Compress the docking folders in a ChemFLow project.")
# optional argument
optional = parser.add_argument_group('[ Optional ]')
# optional.add_argument("-t","--test", metavar='',
# help="STR : Name for this specific protocol [default].")
# optional.add_argument("-z",'--zeta', default="It's a trap", metavar='',
# help="testline", action=None)
optional.add_argument('--foo', help=argparse.SUPPRESS)
parser.parse_args()
如果我取消注釋所有 arguments,並在足夠寬的屏幕上使用parser.print_usage()
我得到
1155:~/mypy$ python3 stack71486789.py
usage: stack71486789.py [-h] [-H] [-p] -r -l -dp [--postprocess] [--postprocess-all] [-n] [--archive] [--archive_all] [-t] [-z]
在較窄的屏幕上,它會嘗試將用法分成幾行,然后點擊
1155:~/mypy$ python3 stack71486789.py
Traceback (most recent call last):
File "stack71486789.py", line 56, in <module>
parser.print_usage()
File "/usr/lib/python3.8/argparse.py", line 2501, in print_usage
self._print_message(self.format_usage(), file)
File "/usr/lib/python3.8/argparse.py", line 2467, in format_usage
return formatter.format_help()
File "/usr/lib/python3.8/argparse.py", line 294, in format_help
help = self._root_section.format_help()
File "/usr/lib/python3.8/argparse.py", line 225, in format_help
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.8/argparse.py", line 225, in <listcomp>
item_help = join([func(*args) for func, args in self.items])
File "/usr/lib/python3.8/argparse.py", line 349, in _format_usage
assert ' '.join(opt_parts) == opt_usage
使用格式化程序很脆弱。 如果上面顯示的用法太長,嘗試拆分它。 assert
應該檢查拆分是否正確。 元變量把它搞砸了。 這個問題是眾所周知的。
如果我注釋掉所有的metavar
,工作的多行用法是:
1202:~/mypy$ python3 stack71486789.py
usage: stack71486789.py [-h] [-H] [-p PROJECT] -r RECEPTOR -l LIGAND -dp PROGRAM [--postprocess POSTPROCESS]
[--postprocess-all POSTPROCESS_ALL] [-n N_POSES] [--archive ARCHIVE] [--archive_all ARCHIVE_ALL]
[-t TEST] [-z ZETA]
和全力幫助
1203:~/mypy$ python3 stack71486789.py
usage: stack71486789.py [-h] [-H] [-p PROJECT] -r RECEPTOR -l LIGAND -dp PROGRAM [--postprocess POSTPROCESS]
[--postprocess-all POSTPROCESS_ALL] [-n N_POSES] [--archive ARCHIVE] [--archive_all ARCHIVE_ALL]
[-t TEST] [-z ZETA]
[ Help ] :
-h, --help Show this help message and exit.
-H, --Help Detailed help.
[ Required ] :
-p PROJECT, ---project PROJECT
STR : ChemFlow project.
-r RECEPTOR, --receptor RECEPTOR
Receptor's mol2 file.
-l LIGAND, --ligand LIGAND
FILE : Ligands MOL2 file.
-dp PROGRAM, --program PROGRAM
STR : plants, vina, qvina, smina.
[ Post Processing ]:
--postprocess POSTPROCESS
Process DockFlow output for the specified project/protocol/receptor.
--postprocess-all POSTPROCESS_ALL
Process DockFlow output in a ChemFlow project.
-n N_POSES, --n_poses N_POSES
INT : Number of docked poses to keep.
--archive ARCHIVE Compress the docking folders for the specified project/protocol/receptor.
--archive_all ARCHIVE_ALL
Compress the docking folders in a ChemFLow project.
[ Optional ]:
-t TEST, --test TEST STR : Name for this specific protocol [default].
-z ZETA, --zeta ZETA testline
沒有特殊字符或空白的更短metavar
也應該有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.