本文翻译自:Parsing boolean values with argparse

I would like to use argparse to parse boolean command-line arguments written as "--foo True" or "--foo False". 我想使用argparse解析布尔命令行参数,写为“ --foo True”或“ --foo False”。 For example: 例如:

my_program --my_boolean_flag False

However, the following test code does not do what I would like: 但是,以下测试代码无法满足我的要求:

import argparse
parser = argparse.ArgumentParser(description="My parser")
parser.add_argument("--my_bool", type=bool)
cmd_line = ["--my_bool", "False"]
parsed_args = parser.parse(cmd_line)

Sadly, parsed_args.my_bool evaluates to True . 可悲的是, parsed_args.my_bool计算结果为True This is the case even when I change cmd_line to be ["--my_bool", ""] , which is surprising, since bool("") evalutates to False . 即使当我将cmd_line更改为["--my_bool", ""] ,也是如此,这很令人惊讶,因为bool("")评估为False

How can I get argparse to parse "False" , "F" , and their lower-case variants to be False ? 如何获取argparse来解析"False""F"及其小写变体为False


#1楼

参考:https://stackoom.com/question/10ySk/用argparse解析布尔值


#2楼

I think a more canonical way to do this is via: 我认为更规范的方法是通过:

command --feature

and

command --no-feature

argparse supports this version nicely: argparse很好地支持此版本:

parser.add_argument('--feature', dest='feature', action='store_true')
parser.add_argument('--no-feature', dest='feature', action='store_false')
parser.set_defaults(feature=True)

Of course, if you really want the --arg <True|False> version, you could pass ast.literal_eval as the "type", or a user defined function ... 当然,如果您真的想要--arg <True|False>版本,则可以将ast.literal_eval传递为“类型”,或者传递用户定义的函数...

def t_or_f(arg):ua = str(arg).upper()if 'TRUE'.startswith(ua):return Trueelif 'FALSE'.startswith(ua):return Falseelse:pass  #error condition maybe?

#3楼

I was looking for the same issue, and imho the pretty solution is : 我一直在寻找相同的问题,恕我直言,漂亮的解决方案是:

def str2bool(v):return v.lower() in ("yes", "true", "t", "1")

and using that to parse the string to boolean as suggested above. 并按照上面的建议使用它来将字符串解析为布尔值。


#4楼

There seems to be some confusion as to what type=bool and type='bool' might mean. 关于type=booltype='bool'含义似乎有些困惑。 Should one (or both) mean 'run the function bool() , or 'return a boolean'? 一个(或两个)是否意味着“运行功能bool()或”返回布尔值”? As it stands type='bool' means nothing. 就目前而言, type='bool'毫无意义。 add_argument gives a 'bool' is not callable error, same as if you used type='foobar' , or type='int' . add_argument给出一个'bool' is not callable错误,就像您使用type='foobar'type='int'

But argparse does have registry that lets you define keywords like this. 但是argparse确实具有注册表,可让您定义这样的关键字。 It is mostly used for action , eg `action='store_true'. 它主要用于action ,例如`action ='store_true'。 You can see the registered keywords with: 您可以通过以下方式查看已注册的关键字:

parser._registries

which displays a dictionary 显示字典

{'action': {None: argparse._StoreAction,'append': argparse._AppendAction,'append_const': argparse._AppendConstAction,
...'type': {None: <function argparse.identity>}}

There are lots of actions defined, but only one type, the default one, argparse.identity . 定义了许多动作,但只有一种类型,默认类型为argparse.identity

This code defines a 'bool' keyword: 这段代码定义了一个'bool'关键字:

def str2bool(v):#susendberg's functionreturn v.lower() in ("yes", "true", "t", "1")
p = argparse.ArgumentParser()
p.register('type','bool',str2bool) # add type keyword to registries
p.add_argument('-b',type='bool')  # do not use 'type=bool'
# p.add_argument('-b',type=str2bool) # works just as well
p.parse_args('-b false'.split())
Namespace(b=False)

parser.register() is not documented, but also not hidden. parser.register()未记录,但也未隐藏。 For the most part the programmer does not need to know about it because type and action take function and class values. 在大多数情况下,程序员不需要了解它,因为typeaction采用函数和类值。 There are lots of stackoverflow examples of defining custom values for both. 有很多为这两者定义自定义值的stackoverflow示例。


In case it isn't obvious from the previous discussion, bool() does not mean 'parse a string'. 万一从前面的讨论bool()不出来, bool()并不意味着“解析字符串”。 From the Python documentation: 从Python文档中:

bool(x): Convert a value to a Boolean, using the standard truth testing procedure. bool(x):使用标准真值测试过程将值转换为布尔值。

Contrast this with 与之对比

int(x): Convert a number or string x to an integer. int(x):将数字或字符串x转换为整数。


#5楼

除了@mgilson所说的以外,还应该注意还有一个ArgumentParser.add_mutually_exclusive_group(required=False)方法,这使得琐碎地同时使用--flag--no-flag变得很简单。 。


#6楼

class FlagAction(argparse.Action):# From http://bugs.python.org/issue8538def __init__(self, option_strings, dest, default=None,required=False, help=None, metavar=None,positive_prefixes=['--'], negative_prefixes=['--no-']):self.positive_strings = set()self.negative_strings = set()for string in option_strings:assert re.match(r'--[A-z]+', string)suffix = string[2:]for positive_prefix in positive_prefixes:self.positive_strings.add(positive_prefix + suffix)for negative_prefix in negative_prefixes:self.negative_strings.add(negative_prefix + suffix)strings = list(self.positive_strings | self.negative_strings)super(FlagAction, self).__init__(option_strings=strings, dest=dest,nargs=0, const=None, default=default, type=bool, choices=None,required=required, help=help, metavar=metavar)def __call__(self, parser, namespace, values, option_string=None):if option_string in self.positive_strings:setattr(namespace, self.dest, True)else:setattr(namespace, self.dest, False)

用argparse解析布尔值相关推荐

  1. Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组

    一. Android资源文件简介 1. Android应用资源的作用 (1) Android项目中文件分类 在Android工程中, 文件主要分为下面几类 : 界面布局文件, Java src源文件, ...

  2. 如何在JavaScript中将字符串转换为布尔值?

    我可以将表示布尔值(例如" true"," false")的字符串转换为JavaScript中的固有类型吗? 我有一个隐藏的HTML表单,该表单会根据用户在列表 ...

  3. 【Android 应用开发】Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组

    . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19913755 . 一. Android资源文件简介 1 ...

  4. java将字符串逻辑表达式转成布尔值

    开发背景: 我们在开发过程中,可能需要将某数据动态从配置文件中读取.且需要根据用户的输入,来动态判断决定某布尔值. 例如: 我们有一个配置,称为配置一,需要在配置文件中写好,该配置有3个子配置(姑且这 ...

  5. java后台传一个对象到前台_前台判断对象中的一个布尔值_springMVC面试题

    1:springMVC工作原理 springMVC架构.png [用户发送请求到前端控制器dispatcherservlet,前端控制器接收到请求之后调用处理器映射器,根据请求url找到具体的处理器, ...

  6. 【Android 应用开发】Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组...

    . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19913755 . 一. Android资源文件简介 1 ...

  7. boolean linux shell_给PowerShell脚本传递一个布尔值

    谁写脚本的经验再丰富,可能也会有败走麦城的时候.比如写出这样一个test.ps1 param ( [bool]$Confirm, [bool]$Force ) if($Confirm){ 'Confi ...

  8. Python布尔值属于数字类型吗?

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 列郡讴歌惜,三朝出入荣. 大家好, ...

  9. Pandas映射(转化)dataframe中的布尔值True和False值到1和0数值、使用astype函数

    Pandas映射(转化)dataframe中的布尔值True和False值到1和0数值.使用astype函数 目录

最新文章

  1. response 中OutputStream和PrintWriter区别
  2. mysql模糊查询LIKE、REGEXP(正则)的详解(在可视化工具navicat下)
  3. OpenCV--罗德里格斯(Rodrigues)变换
  4. 电子商务数据运营的五大应用
  5. 服务器运维软硬件维护月报,运维月报ppt
  6. 通达信最新 行情服务器,【图】2021年通达信新的高级行情服务器IP_股票,炒股,炒股公式,股票指标,股票论坛_股票软件技术交流论坛_理想论坛 - 股票论坛...
  7. 详细图解,一眼就能看懂!卷帘快门(Rolling Shutter)与全局快门(Global Shutter)的区别
  8. 如何修改anaconda的文件目录_Anaconda安装的常见错误和python的基础知识
  9. Unity Loading转场学习笔记
  10. 【微机原理 实验】大小写字母的转换实验 (含汇编代码)
  11. 编程技巧│提高 Javascript 代码效率的技巧
  12. ActionContext.getContext()的几种用法
  13. java 去除警告_Java——警告消除
  14. 学习 Python 之 Pygame 开发魂斗罗(四)
  15. Android 实现QQ侧滑界面之实现
  16. EasyExcel的使用--填充excel
  17. 自动控制原理/现代控制原理专业词汇中英文对照
  18. GIS 网格索引算法
  19. adb client, adb server, adbd原理浅析(附带我的操作过程)
  20. 站群系统服务器搭建,站群服务器 搭建

热门文章

  1. 使用cronolog-1.6.2按日期截取Tomcat日志
  2. IUS database
  3. linux 访问ntfs分区
  4. Delphi TBitmap Scanline
  5. [JavaScript]高效 JavaScript
  6. 【收藏】ASP.NET英文技术文章推荐[11/4 - 11/11]
  7. MySQL8.0数据库配置注意事项
  8. Kafka学习(一)
  9. codeforces 906C
  10. 关于rabbitmq的介绍