pkgjs / parseargs

Polyfill of `util.parseArgs()`
Apache License 2.0
121 stars 9 forks source link

Single dash options #147

Open vjpr opened 1 year ago

vjpr commented 1 year ago

I'm trying to parse a clang string with -DMI_SKIP_COLLECT_ON_EXIT=1 -DMI_STATIC_LIB.

But I don't want to define a schema. I just want to get -DMI_SKIP_COLLECT_ON_EXIT=1 as a token, then I will process it myself.

Would it be possible to add an option that says: use single dash instead of double-dash?

shadowspawn commented 1 year ago

[EDIT: I mistakenly answered this question as though it was about commander rather than parseArgs!]

If you are taking actions in your code based on the values, it might be a good match to have an option -D which takes a value, and add a custom processor to put them into an array for processing yourself. See collect() in the README.

program.option('-D, --define <value>', 'macro definition', collect);

For different ways of changing how unknown options get treated so you see whole token like '-DMI_STATIC_LIB', see .passThroughOptions() and .allowUnknownOption().

(If none of these seem appropriate, give us an example of what sort of command line you are trying to parse.)

bakkot commented 1 year ago

@shadowspawn I think you're thinking of commander - this is an issue on parseArgs.

shadowspawn commented 1 year ago

Oh crud, yes, sorry, didn't pay attention to the repo the question was in! Serves me right for trying to do a quick answer. I'll lay out the equivalent parseArgs approaches later, if no one beats me to it.

shadowspawn commented 1 year ago

I am not sure I understand your use case, but I'll give a simple example of how you could parse the clang macro definitions. You can explain further if this does not help.

const { parseArgs } = require('util');

const result = parseArgs({ options: {
    D: { type: 'string', multiple: true }
}});

console.log(result.values);
$ node index.js -DMI_SKIP_COLLECT_ON_EXIT=1 -DMI_STATIC_LIB
[Object: null prototype] {
  D: [ 'MI_SKIP_COLLECT_ON_EXIT=1', 'MI_STATIC_LIB' ]
}