Is there a straightforward way to use --toggle
and --no-toggle
flags with Python's argparse?
Right now I'm using something similar to the following:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--toggle',
action='store_true',
dest='toggle')
parser.add_argument('--no-toggle',
action='store_true',
default=True,
dest='notoggle')
options = parser.parse_args([])
I'm just manually parsing out the possibilities in a long if chain, but it would be nice if there was a way to tidy this up and have the state immediately stored in one destination by the parser, e.g. options.toggle
. Is this feasible and if so, how?
A somewhat related answer is Python argparse toggle flags however I'm interested in using --no-
as the longopts store_false
toggle prefix (similar to the -
shortopts toggle prefix outlined in the aforementioned link).