mutually exclusive groups + arguments

118 views
Skip to first unread message

Eduardo Bacchi Kienetz

unread,
Jun 28, 2011, 1:50:30 PM6/28/11
to argparse-users
Guys, after looking for this a lot, I had to come here and ask. Even
checked examples at http://code.google.com/p/argparse/source/browse/test/test_argparse.py

I want this behaviour:

( -f FILENAME | -i IP -u USER -p PASSWORD )

You see? If it's -f, then just a filename, otherwise if -i, need -u
and -p (for example).
I believe there is currently no way of getting this behaviour unless I
use subcommands like "prog file file.txt" or "prog ip -u USER -p
pass", is that right?

If I want to have ( -f FILENAME | -i IP ), I already know how, so
would there be a way to hack the help output to fake the rest of the
text ("-u USER -p PASSWORD")? It would be nice if we could change the
final text before it gets outputted, that way we can fake this kind of
stuff in the meantime (until it gets implemented).

Thank you in advance.

Mihai Rusu

unread,
Jun 28, 2011, 8:03:53 PM6/28/11
to argpars...@googlegroups.com
On Tue, Jun 28, 2011 at 10:50 AM, Eduardo Bacchi Kienetz
<edua...@gmail.com> wrote:
> Guys, after looking for this a lot, I had to come here and ask. Even
> checked examples at http://code.google.com/p/argparse/source/browse/test/test_argparse.py
>
> I want this behaviour:
>
> ( -f FILENAME | -i IP -u USER -p PASSWORD )
>
> You see? If it's -f, then just a filename, otherwise if -i, need -u
> and -p (for example).
> I believe there is currently no way of getting this behaviour unless I
> use subcommands like "prog file file.txt" or "prog ip -u USER -p
> pass", is that right?

I think so. I do encounter the same problem pretty often and I end up
deciding which arguments should I use to "select" the exclusive group
and which to just make normal options (they get ignored by the code).
That's not perfect as the --help output won't show the correct syntax.

--
Mihai Rusu

Steven Bethard

unread,
Jun 29, 2011, 1:53:53 PM6/29/11
to argpars...@googlegroups.com
On Tue, Jun 28, 2011 at 5:50 PM, Eduardo Bacchi Kienetz
<edua...@gmail.com> wrote:
> I want this behaviour:
>
> ( -f FILENAME | -i IP -u USER -p PASSWORD )
>
> You see? If it's -f, then just a filename, otherwise if -i, need -u
> and -p (for example).
> I believe there is currently no way of getting this behaviour unless I
> use subcommands like "prog file file.txt" or "prog ip -u USER -p
> pass", is that right?

Yeah, I don't see any obvious way to do this right now (other than the
post-hoc stuff you're probably already doing).

> If I want to have ( -f FILENAME | -i IP ), I already know how, so
> would there be a way to hack the help output to fake the rest of the
> text ("-u USER -p PASSWORD")? It would be nice if we could change the
> final text before it gets outputted, that way we can fake this kind of
> stuff in the meantime (until it gets implemented).

Probably the simplest thing is to override format_usage and/or
format_help, and munge the output to your desired form. If you're
feeling more ambitious, look at the definitions of these two methods,
and you can try modifying the calls to the formatter object directly.

And, of course, if you feel like implementing this properly in
argparse, patches are always welcome on bugs.python.org. ;-)

Steve
--
Where did you get that preposterous hypothesis?
Did Steve tell you that?
        --- The Hiphopopotamus

Reply all
Reply to author
Forward
0 new messages