meow icon indicating copy to clipboard operation
meow copied to clipboard

allowUnknownFlags option and camel case

Open ozum opened this issue 4 years ago • 5 comments

Hi,

Thanks for the library and the great effort.

I need help with an issue:

When I set allowUnknownFlags to false, camel-case flags are reported as unknown flag. However, when I set allowUnknownFlags to true, both camel-cased and dash-cased flags populate the same camel case key (e.g. out-dir and outDir result with { flags: outDir: "xxx" }).

I expected when allowUnknownFlags is true, both types of flags are allowed, because dash cased flags are already converted to camel case in the result object.

Please see examples below:

allowUnknownFlags: false with camel-case flag

const flags = { outDir: { type: "string", desc: "Some desc." } };
const result = meow("some help",  { flags, allowUnknownFlags: false });
console.log(result.flags);

Command: $ cmd --outDir models Result: Unknown flag --outDir Expected: { outDir: "models" }

allowUnknownFlags: true

const flags = { outDir: { type: "string", desc: "Some desc." } };
const result = meow("some help",  { flags, allowUnknownFlags: true });
console.log(result.flags);

Command: $ cmd --outDir models Result: { outDir: "models" }

Command: $ cmd --out-dir models Result: { outDir: "models" }

Kind Regards,

ozum avatar Feb 25 '21 13:02 ozum

// @weareoutman

sindresorhus avatar Feb 26 '21 14:02 sindresorhus

I'll look at this later. And I also noticed there is a problem when passing --help or --version (which are builtin flags) with allowUnknownFlags: true.

weareoutman avatar Feb 28 '21 03:02 weareoutman

@ozum There is currently a workaround to fix this, set an alias for flags.outDir, such as:

const flags = { outDir: { type: "string", alias: "o" } };

Or set the alias to "outDir" or "out-dir" instead of "o" if you think which one makes sense.

The related issue is https://github.com/yargs/yargs-parser/issues/359. I suggest to wait for responses of this issue from yargs-parser, before doing fixes in meow.

weareoutman avatar Mar 06 '21 14:03 weareoutman

@weareoutman thanks for the effort and for providing a workaround. It helps a lot.

Only a camel-case key with a kebab-key alias works for me.

const flags = { outDir: { type: "string", alias: "out-dir" } };

ozum avatar Mar 07 '21 12:03 ozum

Multiple aliases will be supported in the next release, so there will be a workaround with less tradeoffs:

const flags = { outDir: { type: "string", aliases: ["out-dir"], shortFlag: "o" } };

tommy-mitchell avatar Mar 24 '23 17:03 tommy-mitchell