Skip to content

Support defaults for boolean options (including --no-*), for env var set defaults #928

@michael-crawford

Description

@michael-crawford

It doesn't appear we can set boolean option default values, so that we can set a default value for such options via environment variables, then override these values from the command line. I can do this for options which take a value, and would like this sort of override behavior to be consistent for both option types.

We have two use cases. Default true vs default false. For the description below, assume:

Default true use-case: We want to have a method to monitor progress of the program, the options.monitor boolean should default to false. If the user has a MONITOR environment variable set to true (or 1), this boolean should instead default to true. The user can then, on a case-by-case basis, override the false default with --monitor, or override the true (set via env var) default with --no-monitor. I'd like an ability to just turn on monitoring system-wide via an env var, not have to specify --monitor each time I run a CLI command. But, having changed the default system-wide, I may want to do the opposite on a single command.

Default false use-case: We want to have a method to perform additional validation checks, the options.check boolean should default to true. If the user has a NOCHECK (or NO_CHECK) environment variable set to true (or 1), this should instead default to false. The user can then, on a case-by-case basis, override the true default with --no-check, or override the false (set by env var) default with --check. As above, I'd like to turn off these extra checks system-wide via an env var, not have to specify --no-check each time I run a CLI command.

I was asked to open this issue as a new ticket, based on a comment to a related issue (#736). I've provided a cleaner description of the two use-cases above, but below is the original description of my use-cases.

  • options.monitor should default to false or undefined, but have a way to be set to true
  • options.check should default to true, but have a way to be set to false.

Current design as I understand it seems to support what I want as explicit command line options:

  • Add "--monitor" to CLI to get options.monitor = true, otherwise it's undefined
  • Add "--no-check" to CLI to get options.check = false, otherwise it's true

But I can't see how to make this work with default values, allowing me to set the default behavior via environment variables, but still allow for a command-line override.

  • If MONITOR env var exists and is 1 or true, options.monitor should default to true, and "--monitor" should have no effect. Otherwise, options.monitor should default to undefined or false, and "--monitor" would then set it to true.
  • If NOCHECK env var exists and is 1 or true, options.check should default to false, and "--check" should override this default to set it to true. Otherwise, options.check should default to true, and "--no-check" should override this and set it to false.

I thought this should work, but it doesn't. If there's not a way to do this, there should be.

let MONITOR = (process.env.MONITOR && (process.env.MONITOR == 'true' || process.env.MONITOR == '1')) ? true : false;
let CHECK = (process.env.NOCHECK && (process.env.NOCHECK == 'true' || process.env.NOCHECK == '1')) ? false : true;

program.command('test')
  .option('-m, --monitor', 'Monitor build process', MONITOR)
  .option('-C, --no-check', 'Do not check prerequisites', CHECK)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions