reargv 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +78 -0
- package/index.mjs +73 -0
- package/jest.config.mjs +4 -0
- package/package.json +15 -0
- package/tests/test.js +76 -0
- package/types/index.d.ts +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jean Luc Emmanuel VERHANNEMAN
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# reargv
|
|
2
|
+
|
|
3
|
+
Reads command-line arguments and returns a structured, representative object.
|
|
4
|
+
|
|
5
|
+
> **Note:** This module is intended to provide a lightweight alternative to full-featured argument management libraries such as [commander.js](https://www.npmjs.com/package/commander).
|
|
6
|
+
> If you need a more fine-tuned or advanced CLI utility, consider using those instead of `reargv`.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install reargv
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import { reargv } from "reargv";
|
|
18
|
+
|
|
19
|
+
// Example:
|
|
20
|
+
process.argv = [
|
|
21
|
+
null,
|
|
22
|
+
null,
|
|
23
|
+
...[
|
|
24
|
+
"hello",
|
|
25
|
+
"--world",
|
|
26
|
+
"reargv",
|
|
27
|
+
"on.npm",
|
|
28
|
+
'--and="this is great!"',
|
|
29
|
+
"help",
|
|
30
|
+
"VERSION",
|
|
31
|
+
"-k8",
|
|
32
|
+
"-x",
|
|
33
|
+
],
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const argv = reargv();
|
|
37
|
+
|
|
38
|
+
console.log(argv);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Output
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
{
|
|
45
|
+
options: {
|
|
46
|
+
world: 'reargv',
|
|
47
|
+
and: '"this is great!"',
|
|
48
|
+
help: true,
|
|
49
|
+
version: true,
|
|
50
|
+
k: '8',
|
|
51
|
+
x: true
|
|
52
|
+
},
|
|
53
|
+
files: [ 'on.npm' ],
|
|
54
|
+
misc: [ 'hello' ]
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## API
|
|
61
|
+
|
|
62
|
+
### `reargv(): ReArgVObject`
|
|
63
|
+
|
|
64
|
+
| Parameters | Returns |
|
|
65
|
+
|-----------|---------|
|
|
66
|
+
| / | A structured description of the command-line arguments |
|
|
67
|
+
|
|
68
|
+
### Returned object properties
|
|
69
|
+
|
|
70
|
+
- **options** — An object containing arguments introduced by dashes (`-` or `--`) or matched special arguments.
|
|
71
|
+
- **files** — An array of arguments recognized as file names.
|
|
72
|
+
- **misc** — An array of remaining arguments that are neither options nor files.
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
[MIT © 2025](https://github.com/ManuUseGitHub/reargv?tab=MIT-1-ov-file#readme)
|
package/index.mjs
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export const hasNextOptions = (args, i) => {
|
|
2
|
+
return args[i + 1] && !args[i + 1].startsWith("-");
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
const longOptions = (args, arg, out, i) => {
|
|
6
|
+
const [key, value] = arg.slice(2).split("=", 2);
|
|
7
|
+
|
|
8
|
+
if (value !== undefined) {
|
|
9
|
+
out.options[key] = value;
|
|
10
|
+
} else if (hasNextOptions(args, i)) {
|
|
11
|
+
out.options[key] = args[++i];
|
|
12
|
+
return 1;
|
|
13
|
+
} else {
|
|
14
|
+
out.options[key] = true;
|
|
15
|
+
}
|
|
16
|
+
return 0;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const shortOptions = (args, arg, out, i) => {
|
|
20
|
+
const key = arg[1];
|
|
21
|
+
const value = arg.slice(2);
|
|
22
|
+
|
|
23
|
+
if (value) {
|
|
24
|
+
out.options[key] = value;
|
|
25
|
+
} else if (hasNextOptions(args, i)) {
|
|
26
|
+
out.options[key] = args[++i];
|
|
27
|
+
return 1;
|
|
28
|
+
} else {
|
|
29
|
+
out.options[key] = true;
|
|
30
|
+
}
|
|
31
|
+
return 0;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const reargv = () => {
|
|
35
|
+
const args = process.argv.slice(2);
|
|
36
|
+
const out = { options: {}, files: [], misc: [] };
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < args.length; i++) {
|
|
39
|
+
const arg = args[i];
|
|
40
|
+
|
|
41
|
+
// --version or --VERSION -V or misc 'version' or 'VERSION'
|
|
42
|
+
if (/^--version|-V|version$/i.test(arg)) {
|
|
43
|
+
out.options["version"] = true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// --help or --HELP or misc 'help' or 'HELP'
|
|
47
|
+
else if (/^--help|help/i.test(arg)) {
|
|
48
|
+
out.options["help"] = true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// --long or --long=value
|
|
52
|
+
else if (/^--/.test(arg)) {
|
|
53
|
+
i += longOptions(args, arg, out, i);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// -s or -sVALUE
|
|
57
|
+
else if (/^-[a-zA-Z]/.test(arg)) {
|
|
58
|
+
i += shortOptions(args, arg, out, i);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// file
|
|
62
|
+
else if (/\.[a-zA-Z0-9]+$/.test(arg)) {
|
|
63
|
+
out.files.push(arg);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// misc
|
|
67
|
+
else {
|
|
68
|
+
out.misc.push(arg);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return out;
|
|
73
|
+
};
|
package/jest.config.mjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "reargv",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Reads command arguments and gives a representative object of them",
|
|
5
|
+
"main": "index.mjs",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
8
|
+
},
|
|
9
|
+
"author": "Jean Luc Emmanuel VERHANNEMAN",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"jest": "^30.2.0"
|
|
14
|
+
}
|
|
15
|
+
}
|
package/tests/test.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { reargv } from "../index.mjs";
|
|
2
|
+
|
|
3
|
+
const setArgvs = (commandString) => {
|
|
4
|
+
const newArgv = commandString.split(" ");
|
|
5
|
+
return newArgv;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
test("adds 1 + 2 to equal 3", () => {
|
|
9
|
+
process.argv =
|
|
10
|
+
"-o -t -k3 -f -p2 hello mop text.ts next nuxt nest --arg='Hello test' --help --version".split(
|
|
11
|
+
" "
|
|
12
|
+
);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it.each(setArgvs("-a -b -c -d -e -f -g -h -i"))(
|
|
16
|
+
"%s should be present",
|
|
17
|
+
(arg) => {
|
|
18
|
+
const option = arg.replace(/^-/g, "");
|
|
19
|
+
process.argv = [null, null, arg];
|
|
20
|
+
expect(reargv().options[option]).toBe(true);
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
it("arguments should be placed in good places miscs and files and options", () => {
|
|
25
|
+
process.argv = [
|
|
26
|
+
null,
|
|
27
|
+
null,
|
|
28
|
+
..."hello mop -f -k text.ts -k2 -b crypt -m -p oui -nn --non -j k script.js --files next nuxt nest --equal=5".split(
|
|
29
|
+
" "
|
|
30
|
+
),
|
|
31
|
+
];
|
|
32
|
+
const argvs = reargv();
|
|
33
|
+
|
|
34
|
+
expect(argvs.misc.length).toBe(4);
|
|
35
|
+
expect(argvs.files.length).toBe(1);
|
|
36
|
+
expect(argvs.options.files).toBe("next");
|
|
37
|
+
expect(argvs.misc.includes("next")).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it.each([
|
|
41
|
+
"--version --help text.ts",
|
|
42
|
+
"-V help text.ts",
|
|
43
|
+
"--VERSION HELP text.ts",
|
|
44
|
+
"VERSION --HELP text.ts",
|
|
45
|
+
"-V --help text.ts",
|
|
46
|
+
])(
|
|
47
|
+
"'version' and 'help' argument are alwais flags (args : [%s])",
|
|
48
|
+
(_arguments) => {
|
|
49
|
+
process.argv = [null, null, ..._arguments.split(" ")];
|
|
50
|
+
const argvs = reargv();
|
|
51
|
+
|
|
52
|
+
expect(argvs.options.version).toBe(true);
|
|
53
|
+
expect(argvs.options.help).toBe(true);
|
|
54
|
+
}
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
// Example :
|
|
58
|
+
process.argv = [
|
|
59
|
+
null,
|
|
60
|
+
null,
|
|
61
|
+
...[
|
|
62
|
+
"hello",
|
|
63
|
+
"--world",
|
|
64
|
+
"argv",
|
|
65
|
+
"on.npm",
|
|
66
|
+
'--and="this is great!"',
|
|
67
|
+
"help",
|
|
68
|
+
"VERSION",
|
|
69
|
+
"-k8",
|
|
70
|
+
"-x",
|
|
71
|
+
],
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
const argv = reargv();
|
|
75
|
+
|
|
76
|
+
console.log(argv);
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface ReArgVObject {
|
|
2
|
+
/**
|
|
3
|
+
* An object containing arguments introduced by dashes (- or --) or matched special arguments.
|
|
4
|
+
*/
|
|
5
|
+
options: { [x: string]: any };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An array of arguments recognized as file names.
|
|
9
|
+
*/
|
|
10
|
+
files: string[];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* An array of remaining arguments that are neither options nor files.
|
|
14
|
+
*/
|
|
15
|
+
misc: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns an argv description.
|
|
20
|
+
*
|
|
21
|
+
* @returns A ReArgVObject object (never null)
|
|
22
|
+
*/
|
|
23
|
+
export function reargv(): ReArgVObject;
|