tap.utils.get_argument_name()
should choose a canonical argument name like ArgumentParser.add_argument()
does
#59
Labels
bug
Something isn't working
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")
.The text was updated successfully, but these errors were encountered: