admin管理员组

文章数量:1291307

I'm using mander.js to parse the mand line args and I'm trying to collect an optional param that can appear multiple times and it always returns the options I set plus the default one.

function collect (val, memo) {
    memo.push(val);
    return memo;
}

program
    mand('run <param>')
    .action(function run(param, options) {
        console.log(param);
        console.log(options.parent.config);
    });

program
    .option('-c, --config <path>', 'Config', collect, ["/path/to/default"])
    .parse(process.argv);

When I call the script like this:

index.js run some -c "/some/path" -c "/other/path"

It prints:

[ '/path/to/default', '/some/path', '/other/path' ]

But it should only print:

['/some/path', '/other/path' ]`

When I call it without the -c param it works correctly, printing the array with the default value. How can I fix this?

I'm using mander.js to parse the mand line args and I'm trying to collect an optional param that can appear multiple times and it always returns the options I set plus the default one.

function collect (val, memo) {
    memo.push(val);
    return memo;
}

program
    .mand('run <param>')
    .action(function run(param, options) {
        console.log(param);
        console.log(options.parent.config);
    });

program
    .option('-c, --config <path>', 'Config', collect, ["/path/to/default"])
    .parse(process.argv);

When I call the script like this:

index.js run some -c "/some/path" -c "/other/path"

It prints:

[ '/path/to/default', '/some/path', '/other/path' ]

But it should only print:

['/some/path', '/other/path' ]`

When I call it without the -c param it works correctly, printing the array with the default value. How can I fix this?

Share Improve this question edited Jul 14, 2020 at 22:15 KyleMit 30.3k72 gold badges510 silver badges700 bronze badges asked May 14, 2015 at 13:29 borgespiresborgespires 2101 gold badge3 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The mander"Repeatable value" option doesn't support a default value, at least in a way that prevents you from having to write your own logic to handle the scenario where the user pass one or more values. The way you wrote your code, you're gonna have to check the size of the program.config attribute:

  • If the user pass one or more -c option value, the size is > 1;
  • Otherwise, it's === 1.

IMO, this scenario calls for the "A list" option, which supports the default value, and saves you some extra work. Like:

program
  .option('-l, --list <items>', 'A list', list, [ "/path/to/default" ])
  .parse(process.argv);

To have access to the values passed, just call program.list, and in the mand line, call it with values:

$ index.js run some -l "/some/path","/other/path"
// where console.log(program.list) prints [ "/some/path", "/other/path" ]

Or, without values:

$ index.js run some
// where console.log(program.list) prints [ "/path/to/default" ]

You could mark the array you pass as default option, then when collecting look for it.

function collectRepeatable(value, previous) {
    if (previous._isDefault) {
        return [value];
    }
    previous.push(value);
    return previous;
}

function defaultRepeatable(array) {
    array._isDefault = true;
    return array;
}

//...
program
  //...
    .option('--exclude <file>', 'excludes files in input directory by file or pattern', 
        collectRepeatable, defaultRepeatable(['.gitignore', 'codeswing.json']))
  //...

I created Replit space that includes full example: https://replit./@AgainPsychoX/testing-mander-repeatable-options

本文标签: javascriptCommanderjs collect multiple options always include defaultStack Overflow