question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Environment variable name generation from command name is broken

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
kathychurchcommented, Apr 4, 2019

This issue is causing me problems as well. Thank you @gpakosz for submitting a fix!

1reaction
gpakoszcommented, May 15, 2019

@davidism can this be added to the next milestone by chance?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Renaming environment variables by changing variable name ...
The loop uses "${!proj_env_repo@}" to generate a list of variable names that share the name prefix proj_env_repo .
Read more >
Shell command "find -name $variable" not working as expected
What I am trying to do is grab the second field for every line in the file. txt file, pipe it to the...
Read more >
How to generate environment variable name dynamically and ...
I want to generate environment variable name dynamically and set the value to that variable. I wrote a shell script as below. In...
Read more >
How to fix a "Command not found" error in Linux - Red Hat
The env command prints out all global environment variables. ... distributions use uppercase for environment variable names by default.
Read more >
Environment variables - ArchWiki
An environment variable is a named object that contains data used by one or more applications. In simple terms, it is a variable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found