Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

argsert.js 2.4KB

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