python argpaese库的使用


python argparse传入布尔参数false不生效

parser.add_argument("--preprocess", type=bool, default=True, help='run prepare_data or not')

上述代码,在运行是,无论脚本传入的参数是什么,最终得到的结果均是False

1、正确的打开方法:

parser.add_argument('--bool', action='store_false', help='Bool type')

`python run.py  `   此时args.bool的值为True

`python run.py  --bool`  此时args.bool的值为False

2、 其他方式

def str2bool(x):
    return x.lower() in ('true')
parser.add_argument('--resume', type=str2bool, default=False)