Closed drone1 closed 5 years ago
@drone1 I found this in the test directory: https://github.com/75lb/command-line-args/blob/master/test/stop-at-first-unknown.js
const optionDefinitions = [
{ name: 'one', type: Boolean },
{ name: 'two', type: Boolean }
]
const argv = [ '--one', 'a', '--two' ]
const result = commandLineArgs(optionDefinitions, { argv, partial: true })
Use the { partial: true }, parameter on the init, then the problem is solved.
Hi @ymfHU – looks like this code is to stop at the first unknown argument. This is not what I'm looking for.
I was hoping for something that would stop if a given option is not specified. So you'd need to be able to flag on a per-option level, like I've marked with * below:
const OPTION_DEFINITIONS = [
{
name: 'file',
type: String,
multiple: false,
description: `Input file`,
typeLabel: '<file>',
optional: true, // * – not required
},
{
name: 'mongo_db_name',
type: String,
multiple: false,
description: `Mongo database name`,
typeLabel: '<file>',
optional: false, // * – required – commandLineArgs() should fail is this is missing
},
];
It'd be nice to have this feature but I did implement it myself in my app like this:
['file', 'mongo_db_name']
.forEach(argName => {
const v = options[argName];
if (!v || !v.length) {
err(`Missing --${argName} argument.`);
}
})
function quit(code) {
process.exit(code || 0);
}
function err(s) {
console.error(`\x1b[31mError: ${s}\x1b[37m`);
quit(1);
}
sorry for the slow reply.. there's a simple example how to do validation and an explanation why it's no longer built into the library in the wiki
sorry for the slow reply.. there's a simple example how to do validation and an explanation why it's no longer built into the library in the wiki
Aha! No worries, thanks a lot.
I agree with the principle of something doing a single task and doing it well. Is there a plan to write an additional package to handle validation? If not I will create one.
@JordanShurmer the reason validation was taken out of the library was because people had varying opinions of how it should be done.. so yeah, i recommend writing your own library.. I'll be happy to review it if that helps.
Seems like a boilerplate feature to throw an exception if a required argument isn't present, but I can't find anything on the wiki suggesting this is currently implemented. Did I miss it? Thanks.