75lb / command-line-args

A mature, feature-complete library to parse command-line options.
MIT License
689 stars 106 forks source link

Are required arguments not supported? #92

Closed drone1 closed 5 years ago

drone1 commented 5 years ago

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.

ymfHU commented 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.

drone1 commented 5 years ago

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
    },
];
drone1 commented 5 years ago

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);
}
75lb commented 5 years ago

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

drone1 commented 5 years ago

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.

JordanShurmer commented 5 years ago

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.

75lb commented 5 years ago

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