简体   繁体   English

argparse arguments 数量限制

[英]Argparse arguments number limits

I'm trying to convert a bash script into a python script, and I have to create a lot of argument with Argparser.我正在尝试将 bash 脚本转换为 python 脚本,我必须使用 Argparser 创建很多参数。 When I run the code with this arguments I obtain the error当我用这个 arguments 运行代码时,我得到了错误

'
  File "/home/lmonari/anaconda3/envs/ChemFlowOfficial/lib/python3.9/argparse.py", line 338, in _format_usage
    assert ' '.join(opt_parts) == opt_usage

AssertionError'

If I randomly comment some arguments, the code works fine.如果我随机评论一些 arguments,代码工作正常。 Have I reached the maximum number of arguments for Argparse?我是否达到了 Argparse 的最大数量 arguments? I don't find anywhere a documented limit我在任何地方都找不到记录的限制

The code:代码:

create a parser for command line为命令行创建解析器

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()

If I uncomment all your arguments, and use a parser.print_usage() on a wide enough screen I get如果我取消注释所有 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]

On a narrower screen it tries to split usage into several lines, and hits在较窄的屏幕上,它会尝试将用法分成几行,然后点击

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

The usage formatter is brittle.使用格式化程序很脆弱。 If the usage as displayed above is too long, tries to split it.如果上面显示的用法太长,尝试拆分它。 The assert is supposed to check that the split was done right. assert应该检查拆分是否正确。 The metavars are messing that up.元变量把它搞砸了。 This problem is well known.这个问题是众所周知的。

If I comment out all the metavar , the working multiline usage is:如果我注释掉所有的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]

and full help和全力帮助

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

Shorter metavar without special characters or blank should also work.没有特殊字符或空白的更短metavar也应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM