mirror of
https://github.com/laurent22/joplin.git
synced 2025-03-23 21:09:30 +02:00
CLI: Validate required flags
This commit is contained in:
parent
b832930512
commit
42cef1e918
@ -82,6 +82,7 @@ cliUtils.makeCommandArgs = function(cmd, argv) {
|
|||||||
const options = cmd.options();
|
const options = cmd.options();
|
||||||
const booleanFlags = [];
|
const booleanFlags = [];
|
||||||
const aliases = {};
|
const aliases = {};
|
||||||
|
const flagSpecs = [];
|
||||||
for (let i = 0; i < options.length; i++) {
|
for (let i = 0; i < options.length; i++) {
|
||||||
if (options[i].length !== 2) throw new Error(`Invalid options: ${options[i]}`);
|
if (options[i].length !== 2) throw new Error(`Invalid options: ${options[i]}`);
|
||||||
let flags = options[i][0];
|
let flags = options[i][0];
|
||||||
@ -96,6 +97,8 @@ cliUtils.makeCommandArgs = function(cmd, argv) {
|
|||||||
if (flags.short && flags.long) {
|
if (flags.short && flags.long) {
|
||||||
aliases[flags.long] = [flags.short];
|
aliases[flags.long] = [flags.short];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flagSpecs.push(flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = yargParser(argv, {
|
const args = yargParser(argv, {
|
||||||
@ -121,6 +124,19 @@ cliUtils.makeCommandArgs = function(cmd, argv) {
|
|||||||
argOptions[key] = args[key];
|
argOptions[key] = args[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(argOptions)) {
|
||||||
|
const flagSpec = flagSpecs.find(s => {
|
||||||
|
return s.short === key || s.long === key;
|
||||||
|
});
|
||||||
|
if (flagSpec?.arg?.required) {
|
||||||
|
// If a flag is required, and no value is provided for it, Yargs
|
||||||
|
// sets the value to `true`.
|
||||||
|
if (value === true) {
|
||||||
|
throw new Error(_('Missing required flag value: %s', `-${flagSpec.short} <${flagSpec.arg.name}>`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
output.options = argOptions;
|
output.options = argOptions;
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user