Last active
August 31, 2023 17:27
-
-
Save jrobinsonc/d90262c5fad0ad2a253ba46d33cf13b9 to your computer and use it in GitHub Desktop.
Parse CLI arguments
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
{ | |
"name": "@jrobinsonc/parse-cli-arguments", | |
"description": "Parse CLI arguments.", | |
"version": "1.0.0", | |
"main": "parse-cli-arguments.js", | |
"license": "ISC", | |
"author": "Jose Robinson <[email protected]> (https://joserobinson.com/)", | |
"homepage": "https://gist.github.com/jrobinsonc/d90262c5fad0ad2a253ba46d33cf13b9", | |
"repository": { | |
"type": "git", | |
"url": "https://gist.github.com/d90262c5fad0ad2a253ba46d33cf13b9.git" | |
}, | |
"dependencies": { | |
"minimist": "^1.2.8" | |
} | |
} |
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
import minimist from 'minimist'; | |
/** | |
* Parses the CLI arguments into a Map object. | |
* | |
* @param {object} cliArguments - An object containing the CLI arguments. | |
* @returns {[string, Map]} A tuple with the command and a Map with all the arguments. | |
*/ | |
export default function parseCliArguments(cliArguments) { | |
const { | |
_: [cmd, ...booleanArguments], | |
...namedArguments | |
} = minimist(cliArguments); | |
const map = new Map(); | |
for (const [key, value] of Object.entries(namedArguments)) { | |
map.set(key, value); | |
} | |
for (const [index, value] of booleanArguments.entries()) { | |
map.set(index, value); | |
} | |
return [cmd, map]; | |
} |
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
import parseCliArguments from './parse-cli-arguments.js'; | |
describe('parseCliArguments', () => { | |
test('it should parse named arguments and boolean flags into a map object', () => { | |
const cliArguments = [ | |
'create', | |
'db_name', | |
'-ab', | |
'-c', | |
'--disable-keys', | |
'--tables=users,posts', | |
]; | |
const expectedMap = new Map([ | |
['a', true], | |
['b', true], | |
['c', true], | |
['disable-keys', true], | |
['tables', 'users,posts'], | |
[0, 'db_name'], | |
]); | |
const [cmd, cmdArguments] = parseCliArguments(cliArguments); | |
expect(cmd).toEqual('create'); | |
expect(cmdArguments).toEqual(expectedMap); | |
}); | |
test('it should return an empty map if no arguments provided', () => { | |
const cliArguments = ['create']; | |
const [cmd, cmdArguments] = parseCliArguments(cliArguments); | |
expect(cmd).toEqual('create'); | |
expect(cmdArguments).toEqual(new Map()); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment