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

Rename alias to shortFlag #225

Merged
merged 4 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion estest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ meow(
flags: {
rainbow: {
type: 'boolean',
alias: 'r',
shortFlag: 'r',
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type IsRequiredPredicate = (flags: Readonly<AnyFlags>, input: readonly st

export type Flag<Type extends FlagType, Default, IsMultiple = false> = {
readonly type?: Type;
readonly alias?: string;
readonly shortFlag?: string;
readonly default?: Default;
readonly isRequired?: boolean | IsRequiredPredicate;
readonly isMultiple?: IsMultiple;
Expand All @@ -41,7 +41,7 @@ export type Options<Flags extends AnyFlags> = {
The key is the flag name in camel-case and the value is an object with any of:

- `type`: Type of value. (Possible values: `string` `boolean` `number`)
- `alias`: Usually used to define a short flag alias.
- `shortFlag`: A short flag alias.
- `default`: Default value when the flag is not specified.
- `isRequired`: Determine if the flag is required.
If it's only known at runtime whether the flag is required or not you can pass a Function instead of a boolean, which based on the given flags and other non-flag arguments should decide if the flag is required.
Expand All @@ -55,7 +55,7 @@ export type Options<Flags extends AnyFlags> = {
flags: {
unicorn: {
type: 'string',
alias: 'u',
shortFlag: 'u',
default: ['rainbow', 'cat'],
isMultiple: true,
isRequired: (flags, input) => {
Expand Down Expand Up @@ -165,16 +165,16 @@ export type Options<Flags extends AnyFlags> = {
rainbow: {
type: 'boolean',
default: true,
alias: 'r'
shortFlag: 'r'
},
unicorn: {
type: 'boolean',
default: false,
alias: 'u'
shortFlag: 'u'
},
cake: {
type: 'boolean',
alias: 'c'
shortFlag: 'c'
},
sparkles: {
type: 'boolean',
Expand Down Expand Up @@ -302,7 +302,7 @@ const cli = meow(`
flags: {
rainbow: {
type: 'boolean',
alias: 'r'
shortFlag: 'r'
}
}
});
Expand Down
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const getMissingRequiredFlags = (flags, receivedFlags, input) => {
const reportMissingRequiredFlags = missingRequiredFlags => {
console.error(`Missing required flag${missingRequiredFlags.length > 1 ? 's' : ''}`);
for (const flag of missingRequiredFlags) {
console.error(`\t--${decamelize(flag.key, {separator: '-'})}${flag.alias ? `, -${flag.alias}` : ''}`);
console.error(`\t--${decamelize(flag.key, {separator: '-'})}${flag.shortFlag ? `, -${flag.shortFlag}` : ''}`);
}
};

Expand All @@ -57,6 +57,11 @@ const validateOptions = ({flags}) => {
if (invalidFlags.length > 0) {
throw new Error(`Flag keys may not contain '-': ${invalidFlags.join(', ')}`);
}

const flagsWithAlias = Object.keys(flags).filter(flagKey => flags[flagKey].alias !== undefined);
if (flagsWithAlias.length > 0) {
throw new Error(`The option \`alias\` has been renamed to \`shortFlag\`. The following flags need to be updated: \`${flagsWithAlias.join('`, `')}\``);
}
};

const reportUnknownFlags = unknownFlags => {
Expand All @@ -72,6 +77,12 @@ const buildParserFlags = ({flags, booleanDefault}) => {
for (const [flagKey, flagValue] of Object.entries(flags)) {
const flag = {...flagValue};

// `buildParserOptions` expects `flag.alias`
if (flag.shortFlag) {
flag.alias = flag.shortFlag;
delete flag.shortFlag;
}

if (
typeof booleanDefault !== 'undefined'
&& flag.type === 'boolean'
Expand Down Expand Up @@ -224,7 +235,7 @@ const meow = (helpText, options = {}) => {
validateFlags(flags, options);

for (const flagValue of Object.values(options.flags)) {
delete flags[flagValue.alias];
delete flags[flagValue.shortFlag];
}

const missingRequiredFlags = getMissingRequiredFlags(options.flags, flags, input);
Expand Down
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ expectType<Result<never>>(meow({importMeta, hardRejection: false}));
const result = meow('Help text', {
importMeta,
flags: {
foo: {type: 'boolean', alias: 'f'},
foo: {type: 'boolean', shortFlag: 'f'},
'foo-bar': {type: 'number'},
bar: {type: 'string', default: ''},
abc: {type: 'string', isMultiple: true},
Expand Down Expand Up @@ -82,7 +82,7 @@ const options = {
flags: {
rainbow: {
type: 'boolean',
alias: 'r',
shortFlag: 'r',
},
},
} as const;
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const cli = meow(`
flags: {
rainbow: {
type: 'boolean',
alias: 'r'
shortFlag: 'r'
}
}
});
Expand Down Expand Up @@ -103,7 +103,7 @@ Define argument flags.
The key is the flag name in camel-case and the value is an object with any of:

- `type`: Type of value. (Possible values: `string` `boolean` `number`)
- `alias`: Usually used to define a short flag alias.
- `shortFlag`: A short flag alias.
- `default`: Default value when the flag is not specified.
- `isRequired`: Determine if the flag is required. (Default: false)
- If it's only known at runtime whether the flag is required or not, you can pass a `Function` instead of a `boolean`, which based on the given flags and other non-flag arguments, should decide if the flag is required. Two arguments are passed to the function:
Expand All @@ -121,7 +121,7 @@ Example:
flags: {
unicorn: {
type: 'string',
alias: 'u',
shortFlag: 'u',
default: ['rainbow', 'cat'],
isMultiple: true,
isRequired: (flags, input) => {
Expand Down Expand Up @@ -242,16 +242,16 @@ const cli = meow(`
rainbow: {
type: 'boolean',
default: true,
alias: 'r'
shortFlag: 'r'
},
unicorn: {
type: 'boolean',
default: false,
alias: 'u'
shortFlag: 'u'
},
cake: {
type: 'boolean',
alias: 'c'
shortFlag: 'c'
},
sparkles: {
type: 'boolean',
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/fixture-allow-unknown-flags-with-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const cli = meow({
allowUnknownFlags: false,
flags: {
help: {
alias: 'h',
shortFlag: 'h',
type: 'boolean',
},
version: {
alias: 'v',
shortFlag: 'v',
type: 'boolean',
},
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/fixture-conditional-required-multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const cli = meow({
flags: {
test: {
type: 'number',
alias: 't',
shortFlag: 't',
isRequired: () => false,
isMultiple: true,
},
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/fixture-required-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const cli = meow({
flags: {
trigger: {
type: 'boolean',
alias: 't',
shortFlag: 't',
},
withTrigger: {
type: 'string',
isRequired: (flags, _) => flags.trigger,
},
allowError: {
type: 'boolean',
alias: 'a',
shortFlag: 'a',
},
shouldError: {
type: 'boolean',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/fixture-required-multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const cli = meow({
flags: {
test: {
type: 'number',
alias: 't',
shortFlag: 't',
isRequired: true,
isMultiple: true,
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/fixture-required.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const cli = meow({
flags: {
test: {
type: 'string',
alias: 't',
shortFlag: 't',
isRequired: true,
},
number: {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const cli = meow({
autoVersion: !process.argv.includes('--no-auto-version'),
autoHelp: !process.argv.includes('--no-auto-help'),
flags: {
unicorn: {alias: 'u'},
unicorn: {shortFlag: 'u'},
meow: {default: 'dog'},
camelCaseOption: {default: 'foo'},
},
Expand Down
34 changes: 28 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test('return object', t => {
foo <input>
`,
flags: {
unicorn: {alias: 'u'},
unicorn: {shortFlag: 'u'},
meow: {default: 'dog'},
'--': true,
},
Expand Down Expand Up @@ -226,7 +226,7 @@ test('accept help and options', t => {
flags: {
foo: {
type: 'boolean',
alias: 'f',
shortFlag: 'f',
},
},
}).flags, {
Expand All @@ -241,11 +241,11 @@ test('grouped short-flags work', t => {
flags: {
coco: {
type: 'boolean',
alias: 'c',
shortFlag: 'c',
},
loco: {
type: 'boolean',
alias: 'l',
shortFlag: 'l',
},
},
});
Expand All @@ -264,11 +264,11 @@ test('grouped flags work', t => {
flags: {
coco: {
type: 'boolean',
alias: 'c',
shortFlag: 'c',
},
loco: {
type: 'boolean',
alias: 'l',
shortFlag: 'l',
},
},
});
Expand Down Expand Up @@ -580,6 +580,28 @@ test('isMultiple - handles multi-word flag name', t => {
});
});

test('suggests renaming alias to shortFlag', t => {
t.throws(() => {
meow({
importMeta,
flags: {
foo: {
type: 'string',
alias: 'f',
},
bar: {
type: 'string',
alias: 'b',
},
baz: {
type: 'string',
shortFlag: 'z',
},
},
});
}, {message: 'The option `alias` has been renamed to `shortFlag`. The following flags need to be updated: `foo`, `bar`'});
});

if (NODE_MAJOR_VERSION >= 14) {
test('supports es modules', async t => {
try {
Expand Down