( yargs: YargsInstance, usage: UsageInstance, shim: PlatformShim )
| 15 | // validation-type-stuff, missing params, |
| 16 | // bad implications: |
| 17 | export function validation( |
| 18 | yargs: YargsInstance, |
| 19 | usage: UsageInstance, |
| 20 | shim: PlatformShim |
| 21 | ) { |
| 22 | const __ = shim.y18n.__; |
| 23 | const __n = shim.y18n.__n; |
| 24 | const self = {} as ValidationInstance; |
| 25 | |
| 26 | // validate appropriate # of non-option |
| 27 | // arguments were provided, i.e., '_'. |
| 28 | self.nonOptionCount = function nonOptionCount(argv) { |
| 29 | const demandedCommands = yargs.getDemandedCommands(); |
| 30 | // don't count currently executing commands |
| 31 | const positionalCount = |
| 32 | argv._.length + (argv['--'] ? argv['--'].length : 0); |
| 33 | const _s = |
| 34 | positionalCount - yargs.getInternalMethods().getContext().commands.length; |
| 35 | |
| 36 | if ( |
| 37 | demandedCommands._ && |
| 38 | (_s < demandedCommands._.min || _s > demandedCommands._.max) |
| 39 | ) { |
| 40 | if (_s < demandedCommands._.min) { |
| 41 | if (demandedCommands._.minMsg !== undefined) { |
| 42 | usage.fail( |
| 43 | // replace $0 with observed, $1 with expected. |
| 44 | demandedCommands._.minMsg |
| 45 | ? demandedCommands._.minMsg |
| 46 | .replace(/\$0/g, _s.toString()) |
| 47 | .replace(/\$1/, demandedCommands._.min.toString()) |
| 48 | : null |
| 49 | ); |
| 50 | } else { |
| 51 | usage.fail( |
| 52 | __n( |
| 53 | 'Not enough non-option arguments: got %s, need at least %s', |
| 54 | 'Not enough non-option arguments: got %s, need at least %s', |
| 55 | _s, |
| 56 | _s.toString(), |
| 57 | demandedCommands._.min.toString() |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | } else if (_s > demandedCommands._.max) { |
| 62 | if (demandedCommands._.maxMsg !== undefined) { |
| 63 | usage.fail( |
| 64 | // replace $0 with observed, $1 with expected. |
| 65 | demandedCommands._.maxMsg |
| 66 | ? demandedCommands._.maxMsg |
| 67 | .replace(/\$0/g, _s.toString()) |
| 68 | .replace(/\$1/, demandedCommands._.max.toString()) |
| 69 | : null |
| 70 | ); |
| 71 | } else { |
| 72 | usage.fail( |
| 73 | __n( |
| 74 | 'Too many non-option arguments: got %s, maximum of %s', |
no test coverage detected
searching dependent graphs…