typed-argument-parser icon indicating copy to clipboard operation
typed-argument-parser copied to clipboard

Feature request: 'inverse boolean' combined with different dest

Open ds-steventondeur opened this issue 3 years ago • 0 comments

I sometimes use the following pattern to enable some functonality by default but still allow disabling it:

parser = ArgumentParser()
parser.add_argument('--skip-thingy', action='store_false', dest='do_thingy')

args = parser.parse_args(...)

if args.do_thingy:   # Easier to understand than `if not args.skip_thingy:`
   # ....

parser.parse_args('')   # Namespace(do_thingy=True)
parser.parse_args(['--skip-thingy'])  # Namespace(do_thingy=False)

Is it possible to do this with Tap? I have the sense Tap tries to accommodate this by allowing a default True value for a boolean argument but the way it's implemented right now doesn't make a lot of sense to me since the name of the argument cannot be updated to the inverse meaning, e.g.:

class Args(Tap):
     do_thingy: bool = True   # Would default in a 'do_thingy' behavior

args = Args().parse_args('')   # Correct: do_thingy = true
args = Args().parse_args(['--do_thingy'])  # Correct according to the docs: do_thingy = false, but conceptually this doesn't make a lot of sense, I'd like to do something like '--skip-thingy' to make 'do_thingy' False

Any idea how I should go about doing that?

ds-steventondeur avatar Apr 21 '22 09:04 ds-steventondeur