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

Use parsing position (integer) as argument #40

Open
ORESoftware opened this issue Oct 8, 2017 · 2 comments
Open

Use parsing position (integer) as argument #40

ORESoftware opened this issue Oct 8, 2017 · 2 comments

Comments

@ORESoftware
Copy link
Contributor

ORESoftware commented Oct 8, 2017

dashdash might already have this feature.

normally process.argv looks like:

['node','index.js','--foo','--bar','--baz']

but what if I have an array like so representing process.argv?

['--foo','--bar','--baz']

how can I tell dashdash to start parsing from position 0?

thanks

@karfau
Copy link

karfau commented Jan 31, 2020

From looking into the code

Parser.prototype.parse = function parse(inputs) {
var self = this;
// Old API was `parse([argv, [slice]])`
if (Array.isArray(arguments[0])) {
inputs = {argv: arguments[0], slice: arguments[1]};
}
assert.optionalObject(inputs, 'inputs');
if (!inputs) {
inputs = {};
}
assert.optionalArrayOfString(inputs.argv, 'inputs.argv');
//assert.optionalNumber(slice, 'slice');
var argv = inputs.argv || process.argv;
var slice = inputs.slice !== undefined ? inputs.slice : 2;
var args = argv.slice(slice);

you can use the following:

dashdash.parse({
  argv: ['--foo','--bar','--baz'],
  slice: 0
});

Which is also part of the JsDoc comment for that method. But it could of course be added to the longer example

(Or even dashdash.parse(['--foo','--bar','--baz'], 0) but this seems to be the "old way". Passing processs.argv as the first argument is part of the longer example.)

@softmarshmallow
Copy link

softmarshmallow commented Oct 6, 2021

Slice is not working. can anyone confirm this?

  const res = parse({
    argv: ["--a=a", "--b=b", "--c=c"],
    // the dashdash parser is designed for cli use, the argv takes [0] path [1] file [2...] as args.
    slice: 0,
    options: [
      { name: "a", type: "string" },
      { name: "b", type: "string" },
      { name: "c", type: "string" },
    ],
  });
  console.log(res);

output.

Object {
   "_args": Array [],
   "_order": Array [
     Object {
       "from": "argv",
       "key": "c",
       "value": "c",
     },
   ],
   "c": "c",
 }

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

No branches or pull requests

3 participants