【发布时间】:2016-08-10 21:55:30
【问题描述】:
标题确实说明了一切,但我目前有这个,但它不起作用:
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 at the file system and parse everything you see so that "
"we have content in the databse."
)
content_parser_parser.add_argument(
'--start_item',
type=int,
default=0,
help="Assuming the content is sorted by file name, this item is "
"the one to start on."
)
我的具体想法是创建一个包含子命令的命令,用于下载 XML 内容或将其解析到数据库中。
【问题讨论】:
-
不知道
parser中已经包含了什么,或者django以后用它做什么,我不能说。您的子解析器定义看起来不错。但是正如您从其他 SO 问题中看到的那样,使子解析器与其他参数、位置和/或可选参数一起工作可能会很棘手。就像诊断一样,在函数的开头添加print parser._actions。 -
stackoverflow.com/questions/31919101/…,是涉及 argparse 和 django 的先前问题。看起来 django 曾经使用 optparse,但最近添加了 argparse 替代方案。
标签: django argparse django-manage.py subparsers