相关疑难解决方法(0)

Argparse需要subsparser

我正在使用Python 3.4,我正在尝试使用argparsesubparsers,我希望在Python 2.x中有一个类似的行为,如果我不提供位置参数(表示subparser/subprogram)我会收到一条有用的错误消息.即,python2我将收到以下错误消息:

$ python2 subparser_test.py    
usage: subparser_test.py [-h] {foo} ...
subparser_test.py: error: too few arguments
Run Code Online (Sandbox Code Playgroud)

required按照/sf/answers/1609615031/中的建议设置了属性,但是这给我带来了Python 3.4.0的错误: TypeError: sequence item 0: expected str instance, NoneType found- 完全回溯:

$ python3 subparser_test.py    
Traceback (most recent call last):
  File "subparser_test.py", line 17, in <module>
    args = parser.parse_args()
  File "/usr/local/Cellar/python3/3.4.0/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1717, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/local/Cellar/python3/3.4.0/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1749, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/local/Cellar/python3/3.4.0/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1984, …
Run Code Online (Sandbox Code Playgroud)

python python-2.7 argparse python-3.x

55
推荐指数
1
解决办法
2万
查看次数

是否可以在django管理命令中创建子分析器?

标题真的说了一切,但我现在有这个,但它不起作用:

class Command(BaseCommand):
    help = ("Functions related to downloading, parsing, and indexing the  "
            "content")

    def add_arguments(self, parser):
        subparsers = parser.add_subparsers()

        download_parser = subparsers.add_parser(
            'download',
            help='Using a local CSV, download the XML data for content. '
                 'Output is sent to the log.'
        )
        download_parser.add_argument(
            '--start_line',
            type=int,
            default=0,
            help='The line in the file where you wish to start processing.'
        )

        # Add an argparse parser for parsing the content. Yes, this is
        # a bit confusing.
        content_parser_parser = subparsers.add_parser(
            'parse',
            help="Look …
Run Code Online (Sandbox Code Playgroud)

django django-manage.py argparse subparsers

9
推荐指数
1
解决办法
1278
查看次数