Skip to content

Commit

Permalink
Remove flag's aliases from the flags property (#108)
Browse files Browse the repository at this point in the history
Fixes #102
  • Loading branch information
PicchiKevin authored and sindresorhus committed Feb 20, 2019
1 parent 646f30b commit f36715c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
8 changes: 8 additions & 0 deletions index.js
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Expand Up @@ -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`
Expand Down Expand Up @@ -229,6 +230,11 @@ const cli = meow(`
/*
{
flags: {
rainbow: true,
unicorn: false,
sparkles: true
},
unnormalizedFlags: {
rainbow: true,
r: true,
unicorn: false,
Expand Down
31 changes: 26 additions & 5 deletions test.js
Expand Up @@ -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 => {
Expand Down Expand Up @@ -172,8 +172,7 @@ test('accept help and options', t => {
}
}
}).flags, {
foo: true,
f: true
foo: true
});
});

Expand All @@ -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);
});

0 comments on commit f36715c

Please sign in to comment.