trentm / node-dashdash

A light, featureful and explicit option parsing library for node.js.
Other
175 stars 21 forks source link

Use parsing position (integer) as argument #40

Open ORESoftware opened 7 years ago

ORESoftware commented 7 years ago

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 commented 4 years ago

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.)

softmarshmallow commented 3 years ago

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