« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/process-argv.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/process-argv.js')
-rw-r--r--src/process-argv.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/process-argv.js b/src/process-argv.js
new file mode 100644
index 0000000..3193d98
--- /dev/null
+++ b/src/process-argv.js
@@ -0,0 +1,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)
+    }
+  }
+}