75lb / command-line-args

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

Allow configuring arguments to be required #110

Closed codefoster closed 4 years ago

codefoster commented 4 years ago

I'd love to be able to add required: true to an argument option to have command-line-args validate that the user passed it in.

75lb commented 4 years ago

Sorry, no plans for validation. https://github.com/75lb/command-line-args/wiki/Validation#why-is-validation-not-built-into-the-module

codefoster commented 4 years ago

Thanks. I like the single purpose principle, so this is good. It leaves room for a compatible (uses the same schema for command options) package that does do validation. In the mean time, I baked it into my app. I add required and validationPattern (a regex) to each of the options and then run this...

// check required arguments
options.filter(o => o.required && !values[o.name])
    .forEach(o => validationMessages.push(`Missing ${o.name} argument`));

// check validation patterns
options.filter(o => o.validationPattern && !o.validationPattern.test(values[o.name]))
    .forEach(o => validationMessages.push(`Value for ${o.name} argument is not valid`));
75lb commented 4 years ago

Looks good to me!