Skip to content

Commit

Permalink
that's the easy parsing done
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Blair committed Mar 12, 2021
1 parent 34681e1 commit d4d3b5c
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
coverage
tests
.gitignore
**.code-workspace
Empty file added coverage/.gitkeep
Empty file.
66 changes: 41 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
/** @typedef {Object.<Number, String[]>} Args */
/** @typedef {Object.<Number, String[]>} TokenList */

export const global_flag_index = 0

/**
* Accepts an array of options and returns a map of their keys and values
* @param {string[]} args
* @param {TokenList} token_list
*
* @param {String[]} options
*/
export function parse_options(options) {
/** @type {Map<String, any>} */
const parsed_options = new Map()

for (const each_option of options.entries()) {
const index = each_option[0]
const next_index = index + 1

const key = each_option[1]
let key_is_flag = false

let next_parameter
let value
if (options.length >= next_index) {
next_parameter = options[next_index]
if (next_parameter.startsWith("-")) {
key_is_flag = true
} else {
value = next_parameter
}
* @returns {Promise<Args>}
* */
export async function parse(args, token_list) {
const number_of_tokens = Object.keys(token_list).length
let parse = {}
let current_section = 0
for (let current_index = 0; current_index < args.length; current_index++) {
const current_value = args[current_index]

const next_index = current_index + 1
const next_value = args[next_index]

const next_value_is_token =
token_list[current_section]?.indexOf(next_value) >= 0

const current_value_is_flag = current_value[0] === "-"
const next_value_is_flag = args.length > next_index && next_value[0] === "-"

if (parse[current_section] === undefined) parse[current_section] = {}

if (current_value_is_flag) {
const flag_name = current_value.replace(/^-{1,2}/, "")

const value =
next_value_is_flag || next_value_is_token || next_value === undefined
? true
: next_value

parse[current_section] = { [flag_name]: value }
} else if (next_value_is_token) {
current_section += 1
}

parsed_options.set(key, key_is_flag ? true : value)
// FIXME If next index is not a flag then its either a value or a token
// If its not a token then we need to skip current_index beyond it
}

return parsed_options
return parse
}
22 changes: 0 additions & 22 deletions jsconfig.json

This file was deleted.

12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"author": "Nathan Blair",
"license": "0BSD",
"devDependencies": {
"@sonicoriginalsoftware/jester": "file:../jester"
"@sonicoriginalsoftware/jester": "^0.2.6",
"@types/node": "^14.14.34"
}
}
96 changes: 96 additions & 0 deletions tests/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { strict as assert } from "assert"

import { parse, global_flag_index } from "../index.js"

export const id = "Argument Parsing"

export const assertions = {
"Should be able to toggle a flag with the '-$flag' syntax when no value is passed": {
function: async () => {
const flag_name = "f"
const parsed = await parse([`-${flag_name}`], {})
assert.deepStrictEqual(parsed[global_flag_index][flag_name], true)
},
// skip: true,
},
"Should be able to toggle a flag with the '--$flag' syntax when no value is passed": {
function: async () => {
const flag_name = "flag"
const parsed = await parse([`--${flag_name}`], {})
assert.deepStrictEqual(parsed[global_flag_index][flag_name], true)
},
// skip: true,
},
"Should be able to set a flag value passed with '-$flag' syntax": {
function: async () => {
const flag_name = "f"
const value = "value"
const parsed = await parse([`-${flag_name}`, value], {})
assert.deepStrictEqual(parsed[global_flag_index][flag_name], value)
},
// skip: true,
},
"Should be able to set a flag value passed with '--$flag' syntax": {
function: async () => {
const flag_name = "flag"
const value = "value"
const parsed = await parse([`--${flag_name}`, value], {})
assert.deepStrictEqual(parsed[global_flag_index][flag_name], value)
},
// skip: true,
},
"Should be able to set a flag value passed with '--$flag=' syntax": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
"Should be able to set multiple flag toggles": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
"Should be able to set multiple flags with values": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
"Should be able to set a mix of flag toggles and values": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
"Should be able to set a mix of flag toggles and values with one token": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
"Global flags with one token and flag toggles for that token": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
"Global flags with one token and flag values for that token": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
"Global flags with one token and mix of flag toggles and values for that token": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
"Global flags with two tokens and mix of flag toggles and values for each token": {
function: async () => {
assert.fail("Not yet implemented!")
},
skip: true,
},
}
File renamed without changes.

0 comments on commit d4d3b5c

Please sign in to comment.