« get me outta code hell

Modularize it all - 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:
authorLiam <towerofnix@gmail.com>2017-05-31 14:42:22 -0300
committerLiam <towerofnix@gmail.com>2017-05-31 14:42:22 -0300
commit42ec01bb91c517067a9eba901272c1248ed52261 (patch)
treed28bdaa85f9f02028de5f3aad76873a183e0c157 /src/process-argv.js
parent07626fe82986214d4ef56a5790d864c4bf19b27e (diff)
Modularize it all
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)
+    }
+  }
+}