Skip to content
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

Argument.consume_value returns None when ngargs is -1 #26

Closed
papagr opened this issue Apr 25, 2014 · 2 comments
Closed

Argument.consume_value returns None when ngargs is -1 #26

papagr opened this issue Apr 25, 2014 · 2 comments

Comments

@papagr
Copy link

papagr commented Apr 25, 2014

I was testing click and created a subcommand that expects a list of hostnames as arguments. See:

import click

@click.command()
@click.argument('hostnames', nargs=-1)
def subcommand(hostnames):
    print('Executing subcommand')
    print(hostnames)

@click.group()
def main():
    pass

main.add_command(subcommand)

if __name__ == '__main__':
    main()

Calling python3 clicktst.py subcommand host1 host2 returns:
(None,)
instead of the list of hostnames.

To correct this you need to change Argument.consume_value
from

    def consume_value(self, ctx, opts, args):
        found = True
        if self.nargs == 1:
            try:
                value = args.pop(0)
            except IndexError:
                found = False
        elif self.nargs < 0:
            value = tuple(args)
            found = not value
            args = []
        else:
            values = args[:self.nargs]
            values += [None] * (self.nargs - len(values))
            args = args[self.nargs:]
            value = tuple(values)
            found = not value
        if not found:
            value = self.value_from_envvar(ctx)
            if self.nargs != 1:
                value = (value,)
        return value, args

to

    def consume_value(self, ctx, opts, args):
        found = True
        if self.nargs == 1:
            try:
                value = args.pop(0)
            except IndexError:
                found = False
        elif self.nargs < 0:
            value = tuple(args)
            found = bool(value)
            args = []
        else:
            values = args[:self.nargs]
            values += [None] * (self.nargs - len(values))
            args = args[self.nargs:]
            value = tuple(values)
            found = not value
        if not found:
            value = self.value_from_envvar(ctx)
            if self.nargs != 1:
                value = (value,)
        return value, args

The difference is on the line 1217: found = not value becomes found = bool(value)

@douglarek
Copy link
Contributor

#20

@mitsuhiko
Copy link
Contributor

That bug was already fixed. Sorry for that.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants