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

Conversation

tommy-mitchell
Copy link
Contributor

Closes #109.

flag.alias is now flag.shortFlag for users:

const cli = meow(`
	Usage
	  $ foo <input>

	Options
	  --rainbow, -r  Include a rainbow

	Examples
	  $ foo unicorns --rainbow
	  🌈 unicorns 🌈
`, {
	importMeta: import.meta,
	flags: {
		rainbow: {
			type: 'boolean',
			shortFlag: 'r'
		}
	}
});

An extra check was added to buildParserFlags() to ensure compatibility with minimist-options:

// index.js

const buildParserFlags = ({flags, booleanDefault}) => {
	const parserFlags = {};

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

		// ...
	}

	return parserFlags;
}

@tommy-mitchell
Copy link
Contributor Author

Should flag.aliases be a part of this PR as well, or a separate one?

@sindresorhus
Copy link
Owner

Should flag.aliases be a part of this PR as well, or a separate one?

Separate

@sindresorhus
Copy link
Owner

Can you make it throw a user-friendly error when alias option is used?

@tommy-mitchell
Copy link
Contributor Author

Can you make it throw a user-friendly error when alias option is used?

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

@sindresorhus sindresorhus merged commit 14e870d into sindresorhus:main Mar 19, 2023
@sindresorhus
Copy link
Owner

Perfect 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rename alias option to shortFlag
2 participants