<small id='yTHSA'></small><noframes id='yTHSA'>

    <tfoot id='yTHSA'></tfoot>
  • <legend id='yTHSA'><style id='yTHSA'><dir id='yTHSA'><q id='yTHSA'></q></dir></style></legend>

    1. <i id='yTHSA'><tr id='yTHSA'><dt id='yTHSA'><q id='yTHSA'><span id='yTHSA'><b id='yTHSA'><form id='yTHSA'><ins id='yTHSA'></ins><ul id='yTHSA'></ul><sub id='yTHSA'></sub></form><legend id='yTHSA'></legend><bdo id='yTHSA'><pre id='yTHSA'><center id='yTHSA'></center></pre></bdo></b><th id='yTHSA'></th></span></q></dt></tr></i><div id='yTHSA'><tfoot id='yTHSA'></tfoot><dl id='yTHSA'><fieldset id='yTHSA'></fieldset></dl></div>
          <bdo id='yTHSA'></bdo><ul id='yTHSA'></ul>

        Python:argparse subcommand 子命令?

        时间:2023-09-28
        • <legend id='MLGQM'><style id='MLGQM'><dir id='MLGQM'><q id='MLGQM'></q></dir></style></legend>
          <tfoot id='MLGQM'></tfoot>

          <small id='MLGQM'></small><noframes id='MLGQM'>

          • <i id='MLGQM'><tr id='MLGQM'><dt id='MLGQM'><q id='MLGQM'><span id='MLGQM'><b id='MLGQM'><form id='MLGQM'><ins id='MLGQM'></ins><ul id='MLGQM'></ul><sub id='MLGQM'></sub></form><legend id='MLGQM'></legend><bdo id='MLGQM'><pre id='MLGQM'><center id='MLGQM'></center></pre></bdo></b><th id='MLGQM'></th></span></q></dt></tr></i><div id='MLGQM'><tfoot id='MLGQM'></tfoot><dl id='MLGQM'><fieldset id='MLGQM'></fieldset></dl></div>
            • <bdo id='MLGQM'></bdo><ul id='MLGQM'></ul>

                    <tbody id='MLGQM'></tbody>
                  本文介绍了Python:argparse subcommand 子命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的程序有很多可用选项.例如用于更改设置的配置选项.

                  I have a program that has many available options. For example a configuration option to change settings.

                  ./app config -h
                  

                  使用普通的 argparse 子命令帮助我

                  gives me the help using normal argparse subcommands

                  现在我想在名为 list 的配置子命令中添加另一个子命令以列出配置值

                  now i would like to add another subcommand to the config subcommand called list to list config values

                  ./app config list
                  

                  此外,该命令应该接受另一个选项,以便我可以说

                  additionally that command should accept another option so that i could say

                  ./app config list CATEGORY
                  

                  只列出一个类别的配置

                  我现在的代码基本上就是这样,只是有更多的命令

                  my code right now is basically this just with more commands

                  >>> parser = argparse.ArgumentParser()
                  >>> subparsers = parser.add_subparsers(title='subcommands',
                  ...                                    description='valid subcommands',
                  ...                                    help='additional help')
                  >>> subparsers.add_parser('foo')
                  >>> subparsers.add_parser('bar')
                  >>> parser.parse_args(['-h'])
                  usage:  [-h] {foo,bar} ...
                  
                  optional arguments:
                    -h, --help  show this help message and exit
                  
                  subcommands:
                    valid subcommands
                  
                    {foo,bar}   additional help
                  

                  到目前为止,我找不到在子命令中使用子命令的任何方法.如果这是可能的,怎么做?如果没有,还有其他方法可以实现这个目标吗?

                  So far I could not find any way to use a subcommand in a subcommand. If this is possible, how? If not, is there any other way to accomplish this goal?

                  提前致谢

                  推荐答案

                  #file: argp.py
                  
                  import argparse
                  
                  parser = argparse.ArgumentParser(prog='PROG')
                  parser_subparsers = parser.add_subparsers()
                  sub = parser_subparsers.add_parser('sub')
                  sub_subparsers = sub.add_subparsers()
                  sub_sub = sub_subparsers.add_parser('sub_sub')                                                                       
                  sub_sub_subparsers = sub_sub.add_subparsers()
                  sub_sub_sub = sub_sub_subparsers.add_parser('sub_sub_sub')
                  

                  似乎有效.

                  In [392]: run argp.py
                  
                  In [393]: parser.parse_args('sub sub_sub sub_sub_sub'.split())
                  Out[393]: Namespace()
                  
                  In [400]: sys.version_info
                  Out[400]: sys.version_info(major=2, minor=7, micro=2, releaselevel='final', serial=0)
                  

                  这篇关于Python:argparse subcommand 子命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 中自省构造函数 __init__ 的参数 下一篇:python,argparse:在指定另一个输入参数时启用输入参数

                  相关文章

                  <tfoot id='NTS3w'></tfoot>
                  1. <small id='NTS3w'></small><noframes id='NTS3w'>

                  2. <legend id='NTS3w'><style id='NTS3w'><dir id='NTS3w'><q id='NTS3w'></q></dir></style></legend>

                      <i id='NTS3w'><tr id='NTS3w'><dt id='NTS3w'><q id='NTS3w'><span id='NTS3w'><b id='NTS3w'><form id='NTS3w'><ins id='NTS3w'></ins><ul id='NTS3w'></ul><sub id='NTS3w'></sub></form><legend id='NTS3w'></legend><bdo id='NTS3w'><pre id='NTS3w'><center id='NTS3w'></center></pre></bdo></b><th id='NTS3w'></th></span></q></dt></tr></i><div id='NTS3w'><tfoot id='NTS3w'></tfoot><dl id='NTS3w'><fieldset id='NTS3w'></fieldset></dl></div>
                        <bdo id='NTS3w'></bdo><ul id='NTS3w'></ul>