Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Allow for { default } values on boolean flags #34

Closed
wants to merge 2 commits into from

Conversation

indexzero
Copy link

@indexzero indexzero commented Aug 2, 2018

This is related to, but not blocked by #33

The current documentation does not reflect the actual implementation. Quoting from that documentation

static flags = {
  name: flags.string({
    char: 'n',                    // shorter flag version
    description: 'name to print', // help description for flag
    hidden: false,                // hide from help
    multiple: false,              // allow setting this flag multiple times
    env: 'MY_NAME',               // default to value of environment variable
    options: ['a', 'b'],          // only allow the value to be from a discrete set
    parse: input => 'output',     // instead of the user input, return a different value
    default: 'world',             // default value if flag not passed
    required: false,              // make flag required (this is not common and you should probably use an argument instead)
    dependsOn: ['extra-flag'],    // this flag requires another flag
    exclusive: ['extra-flag'],    // this flag cannot be specified alongside this other flag
  }),

  // flag with no value (-f, --force)
  force: flags.boolean({
    char: 'f',
    // by default boolean flags may also be reversed with `--no-` (in this case: `--no-force`)
    // the flag will be set to false if reversed
    // set this to false to disable this functionality
    // allowNo: false,
  }),
}

The above documentation implies that this should work:

static flags = {
  force: flags.boolean({
    char: 'f',
    default: true // <–– this does NOT work
  });
}

This PR attempts to add {default} value support. It is currently blocked on a failing test that I can't figure out how to unbreak. Specifically that somehow --bool is "unexpected" when --bool --no-yes are provided:

 1) parse
       defaults
         --bool --no-yes (default: true):
     Error: Unexpected argument: --bool 
See more help with --help
      at validateArgs (src/validate.ts:11:13)
      at Object.validate (src/validate.ts:39:3)
      at Object.parse (src/index.ts:37:5)
      at Context.it (test/parse.test.ts:329:19)

Remark: for anyone reading this "default" boolean values can be inferred by existential operators (i.e. not undefined or 'flag' in flags). It is plausible that this is the expected interaction with the framework, but if so, it should be documented.

@indexzero
Copy link
Author

@jdxcode any thoughts on why this test is failing? I feel like I am out of my depth since I am still new to the codebase.

... also default values for boolean flags is a welcome feature, yes?

@jdx
Copy link
Contributor

jdx commented Aug 3, 2018

I'm not sure default makes sense on boolean flags. If you default it to true then it's always true whether or not the flag is specified, no?

@jdx
Copy link
Contributor

jdx commented Oct 17, 2018

closing in favor of #41

@jdx jdx closed this Oct 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants