Skip to content

Conversation

@yo-main
Copy link
Contributor

@yo-main yo-main commented May 9, 2020

Hi,

I have encountered an issue when using a Command object without any options.
As I wanted to prevent the '[OPTION]' from appearing in the Usage line, I tried to set the options_metavar parameter to None but got an exception.

I also tried to set it as an empty string rather than None, there's no exception then but the behavior isn't perfect neither as it prints a double space in the Usage row (the double space is between the filename and the argument)

Here's a code snippet to reproduce the issue with None

@click.command("foo", options_metavar=None)
@click.argument("bar")
def cli():
    pass

Expected Behavior

Running the above, I expected to see

$ python foobar.py --help
Usage: foobar.py BAR

Options:
  --help  Show this message and exit.

Actual Behavior

An error is raised as the formatter was expecting a string instead of a None.

Traceback (most recent call last):
  File "foobar.py", line 8, in <module>
    cli()
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 781, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 700, in make_context
    self.parse_args(ctx, args)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 1048, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 1630, in handle_parse_result
    value = invoke_param_callback(self.callback, ctx, self, value)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 123, in invoke_param_callback
    return callback(ctx, param, value)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 950, in show_help
    echo(ctx.get_help(), color=ctx.color)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 570, in get_help
    return self.command.get_help(self)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 975, in get_help
    self.format_help(ctx, formatter)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 1001, in format_help
    self.format_usage(ctx, formatter)
  File "/home/romain/projects/apps/myprss/.venv/lib/python3.7/site-packages/click/core.py", line 923, in format_usage
    formatter.write_usage(ctx.command_path, " ".join(pieces))
TypeError: sequence item 0: expected str instance, NoneType found

Environment

  • Python version: 3.7.4
  • Click version: 7.1.2

@yo-main yo-main changed the title fix usage of empty options_metavar (issue #1550) Fix behavior when setting options_metavar parameter as empty May 9, 2020
@yo-main
Copy link
Contributor Author

yo-main commented Aug 25, 2020

Hi

This PR looks ok to you ?
Or is there anything missing ?

Let me know otherwise so I can close it

Thanks !

@yo-main yo-main closed this Sep 6, 2020
@davidism davidism reopened this Sep 6, 2020
@davidism davidism added this to the 8.0.0 milestone Oct 13, 2020
@davidism davidism changed the base branch from 7.x to master October 13, 2020 01:39
@davidism davidism merged commit 4aabded into pallets:master Oct 13, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
@yo-main yo-main deleted the 7.x branch November 25, 2020 20:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants