2 const command = require('./command')()
3 const YError = require('./yerror')
5 const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
7 module.exports = function argsert (expected, callerArguments, length) {
8 // TODO: should this eventually raise an exception.
10 // preface the argument description with "cmd", so
11 // that we can run it through yargs' command parser.
13 let parsed = {demanded: [], optional: []}
14 if (typeof expected === 'object') {
15 length = callerArguments
16 callerArguments = expected
18 parsed = command.parseCommand(`cmd ${expected}`)
20 const args = [].slice.call(callerArguments)
22 while (args.length && args[args.length - 1] === undefined) args.pop()
23 length = length || args.length
25 if (length < parsed.demanded.length) {
26 throw new YError(`Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`)
29 const totalCommands = parsed.demanded.length + parsed.optional.length
30 if (length > totalCommands) {
31 throw new YError(`Too many arguments provided. Expected max ${totalCommands} but received ${length}.`)
34 parsed.demanded.forEach((demanded) => {
35 const arg = args.shift()
36 const observedType = guessType(arg)
37 const matchingTypes = demanded.cmd.filter(type => type === observedType || type === '*')
38 if (matchingTypes.length === 0) argumentTypeError(observedType, demanded.cmd, position, false)
42 parsed.optional.forEach((optional) => {
43 if (args.length === 0) return
44 const arg = args.shift()
45 const observedType = guessType(arg)
46 const matchingTypes = optional.cmd.filter(type => type === observedType || type === '*')
47 if (matchingTypes.length === 0) argumentTypeError(observedType, optional.cmd, position, true)
51 console.warn(err.stack)
55 function guessType (arg) {
56 if (Array.isArray(arg)) {
58 } else if (arg === null) {
64 function argumentTypeError (observedType, allowedTypes, position, optional) {
65 throw new YError(`Invalid ${positionName[position] || 'manyith'} argument. Expected ${allowedTypes.join(' or ')} but received ${observedType}.`)