vercel / arg

Simple argument parsing
https://npmjs.com/arg
MIT License
1.23k stars 54 forks source link

Support for required arguments? #45

Closed rauschma closed 4 years ago

rauschma commented 5 years ago

Would it make sense to support required arguments? For example:

const args = arg({
    '--help':    Boolean,
    '--port':    arg.req(Number),
    '--name':    String,
    '--tag':     arg.req([String]),
});

Alternatively:

const args = arg({
    '--help': Boolean,
    '--path': String,
});

const {
    '--help': printHelp = false,
    '--path': thePath = arg.required('--path', String),
} = args;

arg.required() would print an error message and exit Node.js. Downsides of this approach: Doesn’t handle aliases, redundancy.

If this is out of scope, it would be a good FAQ, because many shell argument parsing libraries support this feature.

Qix- commented 5 years ago

Pretty much the existing scope of features is the intended scope of features, honestly. Any sort of conditional logic is considered out of scope. Usually "required" arguments are handled by if (!args['--some-arg']) throw new Error('--some-arg is required') - at least, that's how we intend.

I'm :+1: for an FAQ, however :)

Qix- commented 4 years ago

Finally got around to this, thanks for pointing it out @rauschma. Also super weird coincidence, I just came back to this repo to look for something right after finding your openurl module and decided to take a look at this issue. Had to go back and check who the author was!

Anyway, FAQ in the readme now. :)