optionator
optionator copied to clipboard
named and required positional arguments?
Maybe I missed it but is it possible to define positional required arguments?
Let's say I wanted to make the cp
command in the format
cp [options] src dst
Is it possible to get optionator to fail if there are not exactly 2 positional arguments? And is it possible to get the first one added as src
and the 2nd as dst
.
I find I create lots of commands like this and I end up with lots of boilerplate like
const args = optionator.parse(process.argv);
if (args._.length < 1) {
console.log("missing src filename");
printHelp();
return;
}
if (args._.length < 2) {
console.log("missing dst filename");
printHelp();
return;
}
if (args._.length > 2) {
console.log("unknown 3rd argument");
printHelp();
return;
}
const src = args._[0];
const dst = args._[1];
that just seems like something optionator could handle saving me all the work. Does it already do that and I'm just missing it?
What I'd like to write is
const optionSpec = {
options: [
{ option: 'help', alias: 'h', type: 'Boolean', description: 'displays help' },
{ option: 'recursive, alias: 'r', type: 'Boolean', description: 'copy subfolders', },
{ option: 'src', type: 'String', description: 'source filespec', required: true, positional: true, },
{ option: 'dst', type: 'String', description: 'destination filespec', required: true, positional: true, },
],
prepend: 'cp [options] src dst',
};
where positional: true
means it comes from positional arguments and based on the order in the optins array one comes before the other. Then'd use it
const args = optionator.parse(process.argv);
const src = args.src;
const dst = args.dst;
And it would fail if src and dst were not provided
Interesting, no it doesn't support this type of thing, but would be nice to have