yargs / yargs/yargs

Specifying environment variable names

Open
#1,655 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
JavaScript
Stars
11.5k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

The only feature I'm missing from this great library is the possibility to specify environment variable names for options. Something like this:

.options({
    option-1: {
        type: 'string',
        description: 'description',
        env: 'OTHER_THAN_OPTION_1'                
    }
})

It is "2. The Explicit Approach" mentioned here.

Saying default: process.env.OTHER_THAN_OPTION_1 is not a good option, because the precedence is:

  1. Command line args
  2. Env vars
  3. Config file/objects
  4. Configured defaults

so a config file could override the configured default, which is an environment variable in this case.

An example: an option's name can be generic (e.g. account), because the command (e.g. aws) where the option is used gives some context. But an environment variable named ACCOUNT or MY_PREFIX_ACCOUNT has no context. I don't want to prefix my env var with AWS (e.g. AWS_ACCOUNT), because in the same CLI I have many commands with many options that have nothing to do with AWS.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named in the issue. Start by tracing the .options(...) entry point and read the referenced yargs issue #305, then identify how command-line, environment, config, and default values are ordered. Done means an option can declare a distinct environment variable name without losing the stated precedence.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.