sindresorhus / meow

🐈 CLI app helper
MIT License
3.54k stars 151 forks source link

feat: introduce `preferNestedFlags` option #209

Closed antongolub closed 2 years ago

antongolub commented 2 years ago

Real-world issue: I got stuck in a corner case when I couldn't parametrize CLI inside another CLI:

npx -p foo -p bar bar-cli -- --opts-for='bar-cli'

Too many non-option arguments: got 2, maximum of 0
Error: Process completed with exit code 1.

I suggest introducing preferNestedFlags option to workaround the limitation:

const argvStart = options.preferNestedFlags && process.argv.includes('--') ? process.argv.indexOf('--') + 1 : 2;

options = {
    pkg: foundPackage ? foundPackage.packageJson : {},
    argv: process.argv.slice(argvStart),
    ...
};
antongolub commented 2 years ago

@sindresorhus, what do you think of that?

sindresorhus commented 2 years ago

I would recommend trying to find a way to solve this without introducing a new flag. Maybe open an issue on https://github.com/yargs/yargs-parser (the parser meow uses) with your problem statement.

antongolub commented 2 years ago

There's no need for yargs-* patching. I just thought a flag would be a bit more convenient than:

const cli = meow({
    importMeta: import.meta,
    description: 'Custom description',
    help: `
        Usage
          foo <input>
  `,
    // preferNestedFlags: true,
    flags: {
        foo: {
            type: 'string',
        },
    },
    get argv() {
        const argvStart = process.argv.includes('--') ? process.argv.indexOf('--') + 1 : 2
        return process.argv.slice(argvStart)
    }
});
sindresorhus commented 2 years ago

If a flag actually makes sense for this, it makes more sense to add it to the actual argument parser.

antongolub commented 2 years ago
  1. everything is fine with npx. Side effect was produced by our own cli composer
  2. -- --foo='bar' is more of an aesthetic wish. npx -p pkg1 -p pkg2 some-cli --foo='bar' works fine
  3. get argv() { const argvStart = process.argv.includes('--') ? process.argv.indexOf('--') + 1 : 2 return process.argv.slice(argvStart) } completely covers my issue