parser icon indicating copy to clipboard operation
parser copied to clipboard

`parse` does not execute with `flags.build#default`

Open lynchbomb opened this issue 5 years ago • 1 comments

(Please direct this question to a different repo if warranted).

Current implementation: https://github.com/TracerBench/tracerbench/blob/c9b4a5457cfcd21af93dc96f7cd1221eedf6b269/packages/cli/src/helpers/flags.ts#L7-L13

Use-case: We have certain instances where we want to type check and parse flag values differently depending on if a default config.json value is being consumed vs explicitly flagged within the CLI command directly. ie

CLI: COMMAND --markers foo,buzz CONFIG.json: "markers": ["foo", "buzz"] parse: m => { // return do something with m under both scenarios }

Is it possible to "force" parse to execute within the context of flags.build when a default value is set. Appears currently if a default flag value is set parse is never executed.

flags.build#parse will execute

export const foo = flags.build({
  description: `foo foo`,
  parse: f => {
    if (typeof f === 'string') {
       // do something
    }
  }
});

flags.build#parse will NOT execute

export const foo = flags.build({
  default: 'foo',
  description: `foo foo`,
  parse: f => {
    if (typeof f === 'string') {
         // do something
    }
  }
});

lynchbomb avatar Apr 09 '19 21:04 lynchbomb

Is it possible to "force" parse to execute ... when a default value is set.

No.

Appears currently if a default flag value is set parse is never executed.

Yes, that's correct.

We have certain instances where we want to type check and parse flag values differently depending on if a default config.json value is being consumed vs explicitly flagged within the CLI command directly.

How you're parsing the markers flag is the right way to do it:

  • https://github.com/TracerBench/tracerbench/blob/6adcf771ad382b03f5b4bf12253c7f2d4076367b/packages/cli/src/helpers/flags.ts#L143-L147
  • https://github.com/TracerBench/tracerbench/blob/6adcf771ad382b03f5b4bf12253c7f2d4076367b/packages/cli/src/helpers/utils.ts#L181-L194

But, the provided default value should be something that's already in the parsed format.

  • https://github.com/TracerBench/tracerbench/blob/6adcf771ad382b03f5b4bf12253c7f2d4076367b/packages/cli/src/command-config/default-flag-args.ts#L20

So, markers: "domComplete" should actually be markers: ["domComplete"].

In case, you want to treat flags with default value differently in the runtime, you can check if a flag is set from default like this:

const { flags, metadata } = this.parse(Command)

const markersArray = flags['markers']
const markersIsSetFromDefault = metadata.flags['markers'].setFromDefault

I hope that helps.

MunifTanjim avatar May 25 '20 21:05 MunifTanjim