User Tools

Site Tools


argparse_notes

This is an old revision of the document!


--feature and --no-feature

To do

command --feature

and

command --no-feature

For python >= 3.9:

parser.add_argument('--feature', action=argparse.BooleanOptionalAction)

For python < 3.9:

parser.add_argument('--feature', action='store_true')
parser.add_argument('--no-feature', dest='feature', action='store_false')
parser.set_defaults(feature=True)

In practice:

parser.add_argument(
    "--header", action='store_true',
    dest='header',
    help='use 1st line as header')
parser.add_argument(
    "--no-header", action='store_false',
    dest='header',
    help='assumes that there is no header')
parser.set_defaults(header=True)

Ref:- https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse

argparse_notes.1707775832.txt.gz · Last modified: 2024/02/12 22:10 by raju