Parsing boolean values with argparse

Here is an example of how to parse boolean values with argparse in Python:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--flag', type=bool, default=True, help='A boolean flag')
args = parser.parse_args()
print(args.flag)

In this example, we create an ArgumentParser object and add a boolean argument '--flag' to it using the add_argument() method. The type parameter is set to bool and the default parameter is set to True. The help parameter is used to provide a brief description of the argument. We then parse the command-line arguments using the parse_args() method and access the value of the '--flag' argument using the args.flag attribute.

Watch a course Python - The Practical Guide

You can run this script with different inputs to see how it works :

python script.py --flag value
True

python script.py --flag value
False

This is just a simple example, and argparse provides many more features for more complex command-line interfaces.