【发布时间】:2013-03-12 17:36:11
【问题描述】:
我写了一个文件爬虫,我正在尝试扩展它。我想使用 argparse 来处理脚本的设置,包括在命令行中传递起始目录。
例如:/var/some/directory/
我还有其他几个参数可以工作,但我无法正确传递这个目录。我不在乎它前面是否有标志(例如-d /path/to/start/),但我需要确保至少使用 this 参数,因为它将是脚本运行的唯一强制选项.
代码示例:
parser = argparse.ArgumentParser(description='py pub crawler...')
parser.add_argument('-v', '--verbose', help='verbose output from crawler', action="store_true")
parser.add_argument('-d', '--dump', help='dumps and replaces existing dictionaries', action="store_true")
parser.add_argument('-f', '--fake', help='crawl only, nothing stored to DB', action="store_true")
args = parser.parse_args()
if args.verbose:
verbose = True
if args.dump:
dump = True
if args.fake:
fake = True
【问题讨论】:
-
你能显示你的代码吗?这应该是一件很容易做到的事情......也许看到你的代码会帮助我们了解你遇到的困难。
-
我现在没有任何东西可以传递目录。我尝试了各种选择,但都没有值得保留的地方。
-
如果您确实需要类型检查,请参阅:stackoverflow.com/q/11415570/247696