« 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: a459dfe0083dd270898cb09225730261a75882db (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict'

module.exports = async function processArgv(argv, handlers) {
  // Basic command line argument list processor. Takes a list of arguments and
  // an object, which is used as a mapping of option strings to behavior
  // functions.

  let i = 0

  async function handleOpt(opt) {
    // Handles a single option. May be recursive, depending on the user-defined
    // handler given to processArgv. If there is no such handler for the given
    // option, a warning message is displayed and the option is ignored.

    if (opt in handlers) {
      await handlers[opt]({
        // Util object; stores useful information and methods that the handler
        // can access.

        argv, index: i,

        nextArg: function() {
          // Returns the next argument in the argument list, and increments
          // the parse index by one.

          i++
          return argv[i]
        },

        alias: function(optionToRun) {
          // Runs the given option's handler.

          return 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)
    }
  }
}