From f36715c61ccf7f3365aadef6c5987b050d117fbc Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 20 Feb 2019 06:26:10 +0100 Subject: [PATCH] Remove flag's aliases from the `flags` property (#108) Fixes #102 --- index.js | 8 ++++++++ readme.md | 8 +++++++- test.js | 31 ++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 42d32df..8db102e 100644 --- a/index.js +++ b/index.js @@ -108,10 +108,18 @@ module.exports = (helpText, options) => { delete argv._; const flags = camelcaseKeys(argv, {exclude: ['--', /^\w$/]}); + const unnormalizedFlags = {...flags}; + + if (options.flags !== undefined) { + for (const flagValue of Object.values(options.flags)) { + delete flags[flagValue.alias]; + } + } return { input, flags, + unnormalizedFlags, pkg, help, showHelp, diff --git a/readme.md b/readme.md index 6858d2a..a0725a5 100644 --- a/readme.md +++ b/readme.md @@ -73,7 +73,8 @@ foo(cli.input[0], cli.flags); Returns an `Object` with: - `input` *(Array)* - Non-flag arguments -- `flags` *(Object)* - Flags converted to camelCase +- `flags` *(Object)* - Flags converted to camelCase excluding aliases +- `unnormalizedFlags` *(Object)* - Flags converted camelCase including aliases - `pkg` *(Object)* - The `package.json` object - `help` *(string)* - The help text used with `--help` - `showHelp([code=2])` *(Function)* - Show the help text and exit with `code` @@ -229,6 +230,11 @@ const cli = meow(` /* { flags: { + rainbow: true, + unicorn: false, + sparkles: true + }, + unnormalizedFlags: { rainbow: true, r: true, unicorn: false, diff --git a/test.js b/test.js index 64c3ca3..38f2ef4 100644 --- a/test.js +++ b/test.js @@ -57,7 +57,7 @@ test('spawn cli and not show help screen', async t => { test('spawn cli and test input', async t => { const {stdout} = await execa('./fixture.js', ['-u', 'cat']); - t.is(stdout, 'u\nunicorn\nmeow\ncamelCaseOption'); + t.is(stdout, 'unicorn\nmeow\ncamelCaseOption'); }); test('spawn cli and test input flag', async t => { @@ -172,8 +172,7 @@ test('accept help and options', t => { } } }).flags, { - foo: true, - f: true + foo: true }); }); @@ -192,9 +191,31 @@ test('grouped short-flags work', t => { } }); + const {unnormalizedFlags} = cli; + t.true(unnormalizedFlags.coco); + t.true(unnormalizedFlags.loco); + t.true(unnormalizedFlags.c); + t.true(unnormalizedFlags.l); +}); + +test('grouped flags work', t => { + const cli = meow({ + argv: ['-cl'], + flags: { + coco: { + type: 'boolean', + alias: 'c' + }, + loco: { + type: 'boolean', + alias: 'l' + } + } + }); + const {flags} = cli; t.true(flags.coco); t.true(flags.loco); - t.true(flags.c); - t.true(flags.l); + t.is(flags.c, undefined); + t.is(flags.l, undefined); });