Repository: pallets/click

Issue:
Environment variable name generation from command name is broken
When command names contain a `-` character, automatic environment variable name generation is broken

```
#!/usr/bin/env python3

import click

@click.group()
@click.option('--debug/--no-debug')
def cli(debug):
    click.echo('Debug mode is %s' % ('on' if debug else 'off'))

@click.command(name="greet-me")
@click.option('--username', show_envvar=True)
def greet(username):
    click.echo('Hello %s!' % username)

if __name__ == '__main__':
    cli.add_command(greet)
    cli(auto_envvar_prefix='GREETER')
```

```
$ pipenv run python ./test.py greet-me  --help
Loading .env environment variables…
Debug mode is off
Usage: test.py greet-me [OPTIONS]

Options:
  --username TEXT  [env var: GREETER_GREET-ME_USERNAME]
  --help           Show this message and exit.
```

This seems to be solved by changing `core.py:329` to
```
       # If there is no envvar prefix yet, but the parent has one and
        # the command on this level has a name, we can expand the envvar
        # prefix automatically.
        if auto_envvar_prefix is None:
            if parent is not None \
               and parent.auto_envvar_prefix is not None and \
               self.info_name is not None:
                auto_envvar_prefix = '%s_%s' % (parent.auto_envvar_prefix,
                                           self.info_name.upper().replace("-", "_"))
```

I'm not opening a pull request though as I'm not familiar with `click`'s codebase and
  - I'm not sure the modification above is general enough as there are other instances of `<something>.name.upper()` found in `core.py`
  - I'm not sure replacing `-` with `_` covers all the tricky cases


Discussion:
In fact, it also happens when not setting the command name explicitly but letting `click` renaming function names with underscore to command names.

```
#!/usr/bin/env python3

import click

@click.group()
@click.option('--debug/--no-debug')
def cli(debug):
    click.echo('Debug mode is %s' % ('on' if debug else 'off'))

@cli.command()
@click.option('--username', show_envvar=True)
def greet_me(username, foo):
    click.echo('Hello %s!' % username)

if __name__ == '__main__':
    cli(auto_envvar_prefix='GREETER')
```
```
$ pipenv run python ./test.py greet-me --help
Loading .env environment variables…
Debug mode is off
Usage: test.py greet-me [OPTIONS]

Options:
  --username TEXT  [env var: GREETER_GREET-ME_USERNAME]
  --help           Show this message and exit.
```

Resolving patch:
diff --git a/click/core.py b/click/core.py
--- a/click/core.py
+++ b/click/core.py
@@ -335,6 +335,8 @@ def __init__(self, command, parent=None, info_name=None, obj=None,
                                            self.info_name.upper())
         else:
             auto_envvar_prefix = auto_envvar_prefix.upper()
+        if auto_envvar_prefix is not None:
+            auto_envvar_prefix = auto_envvar_prefix.replace("-", "_")
         self.auto_envvar_prefix = auto_envvar_prefix
 
         if color is None and parent is not None:


Test patch:
diff --git a/tests/test_options.py b/tests/test_options.py
--- a/tests/test_options.py
+++ b/tests/test_options.py
@@ -273,6 +273,22 @@ def cmd(arg):
     assert 'TEST_ARG1' in result.output
 
 
+def test_show_envvar_auto_prefix_dash_in_command(runner):
+    @click.group()
+    def cli():
+        pass
+
+    @cli.command()
+    @click.option('--baz', show_envvar=True)
+    def foo_bar(baz):
+        pass
+
+    result = runner.invoke(cli, ['foo-bar', '--help'],
+                           auto_envvar_prefix='TEST')
+    assert not result.exception
+    assert 'TEST_FOO_BAR_BAZ' in result.output
+
+
 def test_custom_validation(runner):
     def validate_pos_int(ctx, param, value):
         if value < 0: