Incorrect Usage
Below code looks natural implementation, but it is actually, does not work.
Even if you pass true from arguments ‘python script.py –boolean True’, the script won’t take so.
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='description should be written here'
)
parse.add_argument(
'--boolean', '-B', type=bool, default=True, help=''
)
args = parser.parse_args()
Example
Here is one of implementation, basically, it is better to pass a function to
import argparse
def str2bool(value):
if isinstance(value, bool):
return value
elif value.lower() in ('true', 't'):
return True
elif value.lower() in ('false', 'f'):
return False
else:
raise argparse.ArgumentTypeError('Boolean values is expeted')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='description should be written here'
)
parse.add_argument(
'--boolean', '-B', type=str2bool, default=True, help=''
)
args = parser.parse_args()
args_dict = vars(args)