Open Deltik opened 2 years ago
Great point. We'll work on this soon. Thank you so much for the thorough and extremely thoughtful write up.
I'm attempting to adapt an ArgumentParser
-based project to use typed-argument-parser
instead, but immediately ran into this same stopper.
Has any progress been made on this? Or is there current a workaround that would enable a construct like this to work?
self.add_argument('--dry', '--dryrun', '--dry-run', dest = 'dryrun', action = 'store_true'...
For the record, @Deltik's proposed solution above sounds right to me.
Motivation
tap.tap.Tap.add_argument()
restricts the*name_or_flags
vararg differently fromArgumentParser.add_argument()
.Consider this pure argparse example:
argparse will choose the value of
dest
as the argument name if available. Otherwise, it will choose the first long argument name as the argument name.But
Tap.add_argument()
will refuse to do the same:I expected Typed Argument Parser to associate the added argument to the attribute
ScratchParser.target_name
because the keyword argumentdest="target_name"
was specified.If
dest="target_name"
had not been specified, then I would have expected the canonical argument name to betarget
because that's whatArgumentParser.add_argument("-t", "--target", "--to", …)
would have done.And if it had been
ArgumentParser.add_argument("-t", "-n", …)
instead, the canonical argument name would have beent
because that is the first name specified and there was no option that began with two hyphens to take precedence.Proposed Solution
tap.utils.get_argument_name()
should be rewritten to determine the canonical argument name based on this order of preference:dest
keyword argument, if it is a string / notNone
.*name_or_flags
that begins with"--"
, if any.*name_or_flags
that begins with"-"
.Behaviors that should be unchanged but may be worth testing:
"-"
), it must be the only item in*name_or_flags
."target"
) and an option (e.g."--target"
).Alternatives
I have not been able to find a way to
Tap.add_argument()
two long-named option aliases. The alternative for this case would be to use plain argparse, but I'd lose the typing benefits offered by Typed Argument Parser.Without
ArgumentParser.add_argument(…, dest=…)
support, the first long-named option would have stand in fordest
. This is not ideal if I want to receive an primary option named with a reserved keyword likeArgumentParser.add_argument("--with", "--to", dest="target_name")
.