Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove flag's aliases from the flags property #108

Merged
merged 10 commits into from Feb 20, 2019
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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not sold on this name. I think we should try to come up with something shorter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got unsortedFlags, what do you thing about it 🤷‍♂️ ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really better. Let's just go with unnormalizedFlags for now.

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