neithere / argh

An argparse wrapper that doesn't make you say "argh" each time you deal with it.
http://argh.rtfd.org
GNU Lesser General Public License v3.0
369 stars 56 forks source link

Allow multiple values for an option #100

Closed gforcada closed 8 years ago

gforcada commented 8 years ago

I'm building a command line interface and would like to be able to repeat the same command and get that collected as a list, i.e.:

myapp --filter some-value --filter some-other-value

By default if the filter parameter is a string you only get the last value (in the example above some-other-value), if it is defined as a list then you get again the same value but as a list :-/

gforcada commented 8 years ago

Looking at argparse I see that this could be achieved with nargs='*' i.e.:

myapp --filter some-value some-other-value

neithere commented 8 years ago

In fact, you can do it with @arg decorator:

@argh.arg('--patterns', nargs='*')
def cmd(patterns=None):
    distros = ('abc', 'xyz')
    return [d for d in distros if not patterns
                                  or any(p in d for p in patterns)]

I don't see how it can be made easier. OTOH, I do recognize the lack of documentation.

Please reopen the issue if you have any better idea. Thanks!