天天看点

python argparse bool_python argparse传入布尔参数不生效解决

前言

在一个需要用到flag作为信号控制代码中一些代码片段是否运行的,比如"--flag True"或者"--flag False"。

但是古怪的是无法传入False,无论传入True还是False,程序里面都是True的参数,所以这个flag并没有生效,也就失去了意义。

参考代码:

# test.py

import argparse

def test_bool():

parser = argparse.ArgumentParser(description="This code is used to test bool value.")

parser.add_argument("--flag",

type=bool,

default=True)

args = parser.parse_args()

print("# The type of flag: ", type(args.flag))

print(args.flag is False)

if args.flag:

print(f"# Flag: {True}")

else:

print(f"# Flag: {False}")

if __name__ == '__main__':

test_bool()

执行上面的代码:python3 test.py --flag False

按照执行的期望是输出# Flag: False,但是实际输出的结果是True:

(data_parse) ~/Desktop/code/cython&numba  python3 test.py --flag False

# The type of flag:

False

# Flag: True

为什么会出现这种情况呢?因为,在命令行的输入会被python认为是字符串,并不是布尔类型。

解决方式一【推荐】

更改代码为:

# ***

parser.add_argument("--flag",

action="store_true",

help="Run or not.")

# ***

这样的话,当你不输入--flag的时候,默认为False;输入--flag的时候,才会触发True值。

看执行结果:

(data_parse) ~/Desktop/code/cython&numba  python3 test.py

# The type of flag:

True

# Flag: False

(data_parse) ~/Desktop/code/cython&numba  python3 test.py --flag

# The type of flag:

False

# Flag: True

当然这种方式,你也可以指定action为store_false,不输入的时候--flag默认True,输入--flag触发False。

解决方式二

这种方式稍微复杂一些,就是需要自己去写一个函数对输入的字符串值进行一个转换,由字符串转换为布尔值。

代码:

import argparse

def str2bool(v):

if isinstance(v, bool):

return v

if v.lower() in ('yes', 'true', 't', 'y', '1'):

return True

elif v.lower() in ('no', 'false', 'f', 'n', '0'):

return False

else:

raise argparse.ArgumentTypeError('Boolean value expected.')

def test_bool():

parser = argparse.ArgumentParser(description="This code is used to test bool value.")

parser.add_argument("--flag",

type=str2bool,

default=True,

help="Run or not.")

args = parser.parse_args()

print("# The type of flag: ", type(args.flag))

print(args.flag is False)

if args.flag:

print(f"# Flag: {True}")

else:

print(f"# Flag: {False}")

if __name__ == '__main__':

test_bool()

这样的话,当我们只要输入自定义函数中的那些对应TrueorFalse的选项,就能能触发对应的布尔类型。

看执行情况:

(data_parse) ~/Desktop/code/cython&numba  python3 test.py

# The type of flag:

False

# Flag: True

(data_parse) ~/Desktop/code/cython&numba  python3 test.py --flag True

# The type of flag:

False

# Flag: True

(data_parse) ~/Desktop/code/cython&numba  python3 test.py --flag False

# The type of flag:

True

# Flag: False

(data_parse) ~/Desktop/code/cython&numba  python3 test.py --flag 0

# The type of flag:

True

# Flag: False

(data_parse) ~/Desktop/code/cython&numba  python3 test.py --flag 1

# The type of flag:

False

# Flag: True

由于默认了default的值为True,所以不输入--flag的时候就默认为True,然后输入--flag对应自定义func: str2bool()中的选项时,就能达到想要的开关效果。