sindresorhus / meow

🐈 CLI app helper
MIT License
3.53k stars 150 forks source link

Rename `alias` to `shortFlag` #225

Closed tommy-mitchell closed 1 year ago

tommy-mitchell commented 1 year ago

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 commented 1 year ago

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

sindresorhus commented 1 year ago

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

Separate

sindresorhus commented 1 year ago

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

tommy-mitchell commented 1 year ago

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 commented 1 year ago

Perfect 👍