Skip to content

Commit f36715c

Browse files
PicchiKevinsindresorhus
authored andcommitted
Remove flag's aliases from the flags property (#108)
Fixes #102
1 parent 646f30b commit f36715c

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

index.js

+8
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,18 @@ module.exports = (helpText, options) => {
108108
delete argv._;
109109

110110
const flags = camelcaseKeys(argv, {exclude: ['--', /^\w$/]});
111+
const unnormalizedFlags = {...flags};
112+
113+
if (options.flags !== undefined) {
114+
for (const flagValue of Object.values(options.flags)) {
115+
delete flags[flagValue.alias];
116+
}
117+
}
111118

112119
return {
113120
input,
114121
flags,
122+
unnormalizedFlags,
115123
pkg,
116124
help,
117125
showHelp,

readme.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ foo(cli.input[0], cli.flags);
7373
Returns an `Object` with:
7474

7575
- `input` *(Array)* - Non-flag arguments
76-
- `flags` *(Object)* - Flags converted to camelCase
76+
- `flags` *(Object)* - Flags converted to camelCase excluding aliases
77+
- `unnormalizedFlags` *(Object)* - Flags converted camelCase including aliases
7778
- `pkg` *(Object)* - The `package.json` object
7879
- `help` *(string)* - The help text used with `--help`
7980
- `showHelp([code=2])` *(Function)* - Show the help text and exit with `code`
@@ -229,6 +230,11 @@ const cli = meow(`
229230
/*
230231
{
231232
flags: {
233+
rainbow: true,
234+
unicorn: false,
235+
sparkles: true
236+
},
237+
unnormalizedFlags: {
232238
rainbow: true,
233239
r: true,
234240
unicorn: false,

test.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test('spawn cli and not show help screen', async t => {
5757

5858
test('spawn cli and test input', async t => {
5959
const {stdout} = await execa('./fixture.js', ['-u', 'cat']);
60-
t.is(stdout, 'u\nunicorn\nmeow\ncamelCaseOption');
60+
t.is(stdout, 'unicorn\nmeow\ncamelCaseOption');
6161
});
6262

6363
test('spawn cli and test input flag', async t => {
@@ -172,8 +172,7 @@ test('accept help and options', t => {
172172
}
173173
}
174174
}).flags, {
175-
foo: true,
176-
f: true
175+
foo: true
177176
});
178177
});
179178

@@ -192,9 +191,31 @@ test('grouped short-flags work', t => {
192191
}
193192
});
194193

194+
const {unnormalizedFlags} = cli;
195+
t.true(unnormalizedFlags.coco);
196+
t.true(unnormalizedFlags.loco);
197+
t.true(unnormalizedFlags.c);
198+
t.true(unnormalizedFlags.l);
199+
});
200+
201+
test('grouped flags work', t => {
202+
const cli = meow({
203+
argv: ['-cl'],
204+
flags: {
205+
coco: {
206+
type: 'boolean',
207+
alias: 'c'
208+
},
209+
loco: {
210+
type: 'boolean',
211+
alias: 'l'
212+
}
213+
}
214+
});
215+
195216
const {flags} = cli;
196217
t.true(flags.coco);
197218
t.true(flags.loco);
198-
t.true(flags.c);
199-
t.true(flags.l);
219+
t.is(flags.c, undefined);
220+
t.is(flags.l, undefined);
200221
});

0 commit comments

Comments
 (0)