node-dashdash icon indicating copy to clipboard operation
node-dashdash copied to clipboard

Use parsing position (integer) as argument

Open ORESoftware opened this issue 8 years ago • 2 comments

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

ORESoftware avatar Oct 08 '17 06:10 ORESoftware

From looking into the code https://github.com/trentm/node-dashdash/blob/a47fa779f9d4ff02a31da890002332bf60cc07ec/lib/dashdash.js#L355-L371 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.)

karfau avatar Jan 31 '20 17:01 karfau

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",
 }

softmarshmallow avatar Oct 06 '21 18:10 softmarshmallow