-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python API: inconsistent handling of metavar [question] #2709
Comments
I've known of issues with
Given the efforts on #2678 the full overhaul is probably unlikely, even if it would be my long-term preference.
Agreed; that'll just be me overlooking things. I will have written enough code to make exemplar help pages etc. look the way I think they should look, but not guaranteed to have tested every scenario. Particularly positionals with
Also need to check whether I myself have misunderstood something in
I probably avoided that from the outset because that parsing mechanism is highly incompatible with the MRtrix3 capability of arbitrary movement of command-line options. But if there's a case for it we can try to make it work. |
Yes I saw this when snooping around for similar examples, it's pretty close to my use case. I agree having all but the last parsed to a single argument, with an additional one for the final template is a bit awkward, but amounts to the same thing as a single
Yep, for example
It really seems to me like the only difference between using
Certainly something like I'm of the mind that given *Here are the various behaviours when metavar is a tuple in unnecessary detail
i.e.
|
Prior post I thought this may have been an error on my part, but it is actually consistent with the
But as per your extensive reporting, this only makes sense if
Where such detail would actually be of greater function would be to produce an exemplar command that constructs positionals & optionals of all configurations of interest, and then show which bits of which outputs (not just inline help page, also eg. RST / MarkDown) we believe to be showing the wrong behaviour. Then we can tweak the API code and test the same command. Best way to be confident that all bases are covering and a change to fix one thing isn't breaking something else. |
I've been using the python api in an external module and have run into some inconsistencies in the use of the argument/option
metavar
feature. This is more of a discussion / clarification issue before I go ahead opening any PRs or suchlike. Also this may all be moot since the api may or may not be due an overhaul.(For context, I'm using
metavar="input1 output1 [input2 output2]"
andnargs=+
for a positional argument to get a variable number of input/output pairs, similar to other commands e.g.dwi2fod
. There may be issues in other places I haven't come across e.g. in subparsers, which I'm not using)1. Mostly
metavar
is checked for, sometimes it isn't.This gives inconsistent usage output in particular if a metavar is used for a positional. I figure this is just an omission, since I can't think of a scenario where you'd not want to support metavar.
proposed change: always check; only place I think this is missing is in printing the usage line at the start of the full help text here, but there may be others I've not come across.
2. There are different ways for formatting metavar
if arg.metavar: name = arg.metavar
)name = " ".join(arg.metavar)
)This one confused me until I realised argparse allows using tuples to specify multiple metavars when
nargs>1
. So this would make the intended usage in my case look like this:nargs='+', metavar=('input1 output1', '[input2 output2 ...]')
to get the displayusage: prog [options] input1 output1 [input2 output2 ...]
. Unfortunately, bugs in argparse means this is only stable for optionals, not for positionals. If I just use a single string (metavar='input1 output1 [input2 output2 ...]'
) the help gets formatted asUsage: prog i n p u t 1 o u t p u t 1 [ i n p u t 2 o u t p u t 2 . . . ]
😃The
-continue
option makes use of the tupled metavar, but I don't see the benefit over just using a multi-word string, given that the destination is still a single variable and accessed via indexing.*proposed change: not sure. Easiest option seems to be not supporting tuples for positionals, since internal argparse bug means this doesn't work anyway... This would mean not branching on tuples (and not doing
' '.join(metavar)
) when printing positionals, leaving everything else as is. I don't think a blanket ban on tuple metavars makes sense since users familiar with argparse would expect to be able to use them. But then again, given that a tupled metavar only affects the print behaviour (which is overridden in mrtrix anyway), maybe that would be easier and more consistent?Happy to keep changes to a bare minimum, or just deal with it on my end, since this is a pretty niche problem.
* in untampered argparse there is some benefit, because for
nargs='*'
you get nice optional formatting like this:'-opt', metavar=('in1', 'in2')
-->usage: prog [-h] [-opt in1 [in2 ...]]
, but this is overridden in MRtrixThe text was updated successfully, but these errors were encountered: