argparse_notes
--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://docs.python.org/3/library/argparse.html#action → search for
--no-foo
argparse_notes.txt · Last modified: 2024/02/12 22:18 by raju