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

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

Open
lynchbomb opened this issue Apr 9, 2019 · 1 comment
Open

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

lynchbomb opened this issue Apr 9, 2019 · 1 comment

Comments

@lynchbomb
Copy link

(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
    }
  }
});
@MunifTanjim
Copy link

MunifTanjim commented May 25, 2020

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:

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

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants