Skip to content

Commit

Permalink
fix: check required command options
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 26, 2018
1 parent 50395ec commit 93e2bae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ export default class Command {
}
}
}

/**
* Check if the required string-type options exist
* @param values
* @param globalCommand
*/
checkRequiredOptions(values: { [k: string]: any }, globalCommand: Command) {
const requiredOptions = [...globalCommand.options, ...this.options].filter(
option => option.required
)
for (const option of requiredOptions) {
const value = values[option.names[0]]
if (value === undefined || typeof value === 'boolean') {
console.error(`error: option \`${option.rawName}\` argument is missing`)
process.exit(1)
}
}
}
}

export { HelpCallback, CommandExample, CommandConfig }
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ class CAC extends EventEmitter {

command.checkUnknownOptions(originalOptions, globalCommand)

command.checkRequiredOptions(originalOptions, globalCommand)

const minimalArgsCount = command.args.filter(arg => arg.required).length

if (args.length < minimalArgsCount) {
Expand Down

0 comments on commit 93e2bae

Please sign in to comment.