« get me outta code hell

process-argv.js « src - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/process-argv.js
blob: 3193d98ad14074e406a835281bcbae0e12cd962f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use strict'

module.exports = async function processArgv(argv, handlers) {
  let i = 0

  async function handleOpt(opt) {
    if (opt in handlers) {
      await handlers[opt]({
        argv, index: i,
        nextArg: function() {
          i++
          return argv[i]
        },
        alias: function(optionToRun) {
          handleOpt(optionToRun)
        }
      })
    } else {
      console.warn('Option not understood: ' + opt)
    }
  }

  for (; i < argv.length; i++) {
    const cur = argv[i]
    if (cur.startsWith('-')) {
      const opt = cur.slice(1)
      await handleOpt(opt)
    }
  }
}