最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python的argparse中传入type是bool的参数始终都是True

传入 crifan 712浏览 0评论
之前已经:
【已解决】Python文件支持运行时从命令行传入参数
但是此处使用期间,遇到问题:
argParser.add_argument("-u", "--update-existed-md", type=bool, default=True, help="update existed md file modification time”)

isUpdateMdWhenExist = args.update_existed_md
传入:
python common/tools/generate_md_from_summary.py -u false -f /Users/xxx/dev/crifan/gitbook/gitbook_template/books/linux_usage_dev_summary/src/SUMMARY.md

python common/tools/generate_md_from_summary.py -u False -f /Users/xxx/dev/crifan/gitbook/gitbook_template/books/linux_usage_dev_summary/src/SUMMARY.md
结果:
isUpdateMdWhenExist始终都是True:
isUpdateMdWhenExist=True
python argparse bool always true
python – Why is argparse not parsing my boolean flag correctly? – Stack Overflow
python – Why in argparse, a ‘True’ is always ‘True’? – Stack Overflow
去看官网的store_true
argparse — Parser for command-line options, arguments and sub-commands — Python 3.9.5 documentation
“* ‘store_true’ and ‘store_false’ – These are special cases of ‘store_const’ used for storing the values True and False respectively. In addition, they create default values of False and True respectively. For example:
>>>
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument(‘–foo’, action=’store_true’)
>>> parser.add_argument(‘–bar’, action=’store_false’)
>>> parser.add_argument(‘–baz’, action=’store_false’)
>>> parser.parse_args(‘–foo –bar’.split())
Namespace(foo=True, bar=False, baz=True)”
基本搞懂了:
对于bool参数,应该用:
-x
或:
--xxx
--xxx-yyy
如果加了此参数,则
  • 代码中store_true -》 传入了True
  • 代码中store_false -》 传入了False
所以此处可以去写成:
argParser.add_argument("--disable-update-existed-md", action='store_false', help="disable update existed md file modification time. if not pass, default to True")
argParser.add_argument("--enable-use-random-time", action='store_true', help="enable use random time for update time. if not pass, default to False")
调用:
两个都不传
python common/tools/generate_md_from_summary.py -f /Users/xxx/dev/crifan/gitbook/gitbook_template/books/linux_usage_dev_summary/src/SUMMARY.md
输出:
args.disable_update_existed_md=True
isRandomUpdateTime=False
设置其中一个:
python generate_md_from_summary.py --disable-update-existed-md -f /Users/xxx/dev/crifan/gitbook/gitbook_template/books/linux_usage_dev_summary/src/SUMMARY.md
输出:
args.disable_update_existed_md=False
isRandomUpdateTime=False
2个都传入:
python generate_md_from_summary.py --disable-update-existed-md --enable-use-random-time -f /Users/xxx/dev/crifan/gitbook/gitbook_template/books/linux_usage_dev_summary/src/SUMMARY.md
输出:
args.disable_update_existed_md=False
isRandomUpdateTime=True
后来,改为:
argParser.add_argument("--disable-update-existed-md", action='store_true', help="disable update existed md file modification time")
argParser.add_argument("--enable-use-random-time", action='store_false', help="enable use random time for update time")
然后默认不传入这2个参数:
ython generate_md_from_summary.py -f /Users/xxx/dev/crifan/gitbook/gitbook_template/books/linux_usage_dev_summary/src/SUMMARY.md
就是我要的效果了。
附带help的信息:
 python generate_md_from_summary.py --help
usage: generate_md_from_summary.py [-h] -f MD_FILE [--disable-update-existed-md] [--enable-use-random-time] [-r RANDOM_RANGE]


optional arguments:
  -h, --help            show this help message and exit
  -f MD_FILE, --md-file MD_FILE
                        full path of entry SUMMARY.md file
  --disable-update-existed-md
                        disable update existed md file modification time. if not pass, default to True
  --enable-use-random-time
                        enable use random time for update time. if not pass, default to False
  -r RANDOM_RANGE, --random-range RANDOM_RANGE
                        for random time, the range. in seconds. default 600 = 10 minutes
供参考。
 

转载请注明:在路上 » 【已解决】Python的argparse中传入type是bool的参数始终都是True

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.132 seconds, using 20.53MB memory