sindresorhus / meow

🐈 CLI app helper
MIT License
3.53k stars 150 forks source link

allowUnknownFlags option and camel case #178

Open ozum opened 3 years ago

ozum commented 3 years ago

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,

sindresorhus commented 3 years ago

// @weareoutman

weareoutman commented 3 years ago

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 commented 3 years ago

@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.

ozum commented 3 years ago

@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" } };
tommy-mitchell commented 1 year ago

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" } };