-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nathan Blair
committed
Mar 12, 2021
1 parent
34681e1
commit d4d3b5c
Showing
8 changed files
with
154 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
coverage | ||
tests | ||
.gitignore | ||
**.code-workspace |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.