Skip to content

Instantly share code, notes, and snippets.

@dimitriacosta
Created January 9, 2021 22:00
Show Gist options
  • Save dimitriacosta/fb5f5569cf8b00d2ade21acf3b5ba96f to your computer and use it in GitHub Desktop.
Save dimitriacosta/fb5f5569cf8b00d2ade21acf3b5ba96f to your computer and use it in GitHub Desktop.
Convert string to boolean values
def str2bool(val):
"""Converts string to boolean value"""
if isinstance(val, bool):
return val
if val.lower() in ("yes", "y", "true", "t", "1"):
return True
if val.lower() in ("no", "n", "false", "f", "0"):
return False
raise argparse.ArgumentTypeError("Boolean value expected.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment