reargv
v1.0.2
Published
Reads command arguments and gives a representative object of them
Readme
reargv
Reads command-line arguments and returns a structured, representative object.
Note: This module is intended to provide a lightweight alternative to full-featured argument management libraries such as commander.js.
If you need a more fine-tuned or advanced CLI utility, consider using those instead ofreargv.
Installation
$ npm install reargvUsage
Script pipeline
First, create a script file somewhere
//argv.mjs
import { reargv } from "reargv";
console.log(JSON.stringify(reargv(), null, 2))Then, run it and pass it commands
$ node ./argv.mjs hello --world reargv on.npm --and='this is great!' help VERSION -k8 -x > output.jsonOutput
((...) > output.json to output in output.json)
{
options: {
world: 'reargv',
and: '"this is great!"',
help: true,
version: true,
k: '8',
x: true
},
files: [ 'on.npm' ],
misc: [ 'hello' ]
}Mocking process.argv
import { reargv } from "reargv";
// Example:
process.argv = [
null,
null,
...[
"hello",
"--world",
"reargv",
"on.npm",
'--and="this is great!"',
"help",
"VERSION",
"-k8",
"-x",
],
];
const argv = reargv();
console.log(argv);Output
{
options: {
world: 'reargv',
and: '"this is great!"',
help: true,
version: true,
k: '8',
x: true
},
files: [ 'on.npm' ],
misc: [ 'hello' ]
}API
reargv(): ReArgVObject
| Parameters | Returns | |-----------|---------| | / | A structured description of the command-line arguments |
Returned object properties
- options — An object containing arguments introduced by dashes (
-or--) or matched special arguments. - files — An array of arguments recognized as file names.
- misc — An array of remaining arguments that are neither options nor files.
