Skip to content

Commit 473c604

Browse files
committed
v3.3.0-beta :: The POWER of Trigrams!!!
1 parent 8ff2bcc commit 473c604

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ function main() {
6868

6969
// Check if a subcommand was encountered (returns object with next position)
7070
if (typeof ret === 'object') {
71-
// ret contains { i, key, opt }:
72-
// - ret.i is the index to continue parsing from (already incremented)
71+
// ret contains { avi, key, opt }:
72+
// - ret.avi is the argv index to continue parsing from
7373
// - ret.key is the variable name in gitReq that triggered the exit ('command')
7474
// - ret.opt is the option string that triggered the exit (subcommand name)
7575

7676
console.error(`Executing ${ret.opt} command...`);
7777

7878
switch (ret.opt) {
7979
case 'commit':
80-
// Parse commit-specific options starting from ret.i
80+
// Parse commit-specific options starting from ret.avi
8181
const commitRes = {};
82-
parse(process.argv, ret.i, commitReq, commitRes, console.error);
82+
parse(process.argv, ret.avi, commitReq, commitRes, console.error);
8383

8484
// Use the results
8585
console.error(
@@ -109,14 +109,14 @@ parse(argv, i, req, res, err)
109109
- **i**: Starting index for parsing (usually `2`)
110110
- **req**: Configuration object defining your command structure
111111
- **res**: Object that will be populated with parsed results
112-
- **err**: Error handler function, receives `{msg, i, opt, key, val}` and returns boolean
112+
- **err**: Error handler function, receives `{msg, avi, opt, key, val}` and returns boolean
113113

114114
### Return Value
115115

116116
The function returns either:
117117
- A number (the next index after parsing completed normally)
118-
- An object `{ i, key, opt }` when exiting early due to an exit option, where:
119-
- `i`: The next index to resume parsing from (already incremented past the exit option)
118+
- An object `{ avi, key, opt }` when exiting early due to an exit option, where:
119+
- `avi`: The next index to resume parsing from (**A**rg**V** **I**ndex)
120120
- `key`: The variable name in the req object that triggered the exit
121121
- `opt`: The option string that triggered the exit (e.g., the subcommand name)
122122

@@ -248,15 +248,15 @@ const ret = parse(process.argv, 2, mainReq, mainRes, console.error);
248248

249249
if (typeof ret === 'object') {
250250
// When a command is found via the exit mechanism:
251-
// - ret.i is already positioned after the subcommand
251+
// - ret.avi is already positioned after the subcommand
252252
// - ret.key contains the variable name in req ('command' in this case)
253253
// - ret.opt contains the matched option (the subcommand name)
254254

255255
switch(ret.opt) {
256256
case 'build':
257257
const buildReq = { /* build options */ };
258258
const buildRes = {};
259-
parse(process.argv, ret.i, buildReq, buildRes, console.error);
259+
parse(process.argv, ret.avi, buildReq, buildRes, console.error);
260260
break;
261261
}
262262
}

examples/cli-tool.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ function main() {
9999
if (typeof ret === 'object') {
100100
// When a command is found via the exit mechanism,
101101
// ret contains {i, key, opt} where:
102-
// - ret.i is the index to resume parsing from
102+
// - ret.avi is the index to resume parsing from
103103
// - ret.key is the variable name in req that triggered the exit (here: 'command')
104104
// - ret.opt is the value that matched (here: the command name itself)
105105

106106
switch (ret.opt) {
107107
case 'build':
108-
handleBuildCommand(ret.i);
108+
handleBuildCommand(ret.avi);
109109
break;
110110
case 'serve':
111-
handleServeCommand(ret.i);
111+
handleServeCommand(ret.avi);
112112
break;
113113
case 'test':
114114
console.log('Running tests...');

index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
'use strict';
22
const isA = Array.isArray;
33
/**
4-
* @typedef {string} KeyStr Variable name
5-
* @typedef {string | boolean | string[]} VarVal Variable value
4+
* @typedef {string} KeyStr Variable name, in most cases, or kit name
65
* @typedef {string} OptStr Option string, e.g. '--', '-o', '--option'
7-
* @typedef {OptStr | null} OptDef Option definitions, e.g. '--', '-o', '--option', or null to refer to the variable name
8-
* @typedef {OptDef | OptDef[]} OptKit one or more option definitions
6+
* @typedef {OptStr | null} OptDef Option definition, e.g. '--', '-o', '--option', or `null` to refer to the variable name
7+
* @typedef {OptDef | OptDef[]} OptKit one or more option definitions, for a shortest one
98
*
109
* @typedef {object} VarKit Variable configuration object
1110
* @property {VarVal} [def] Variable **def**inition & **def**ault value (pun intended)
1211
* @property {OptKit} [set] Array of options to set the variable value
1312
* @property {OptKit} [rst] Array of options to reset the variable value
13+
* @typedef {string | boolean | string[]} VarVal Variable value
1414
*
15-
* @typedef {OptDef | OptDef[]} ExitKit Exit options, identical to `OptKit` for now
16-
* @typedef {Record<KeyStr, VarKit | ExitKit>} KeyKitMap
17-
* @typedef {Record<KeyStr, VarVal>} KeyValMap
15+
* @typedef {OptDef | OptDef[]} ExitKit Exit options
16+
* @typedef {Record<KeyStr, VarKit | ExitKit>} KeyKitMap `req`
17+
* @typedef {Record<KeyStr, VarVal>} KeyValMap `res`
1818
*
1919
* @callback IsFatal
20-
* @param {{msg: string, i: number, opt: OptStr, key?: KeyStr, val?: VarVal }} err
20+
* @param {{msg: string, avi: number, opt: OptStr, key?: KeyStr, val?: VarVal }} err
2121
* @returns {boolean} Whether the parsing should continue (false) or quit (true)
2222
* @typedef {Record<OptStr, KeyStr>} OptKeyMap internal type
2323
*/
@@ -30,7 +30,7 @@ const god = ok => ok === undefined ? [] : isA(ok) ? ok : [ok];
3030
* @param {KeyKitMap} req Options structure definition
3131
* @param {KeyValMap} res Object to store parsed results
3232
* @param {IsFatal} err Error handler function, return true to quit parsing
33-
* @returns {number | { i: number, key: KeyStr, opt: OptStr }}
33+
* @returns {number | { avi: number, key: KeyStr, opt: OptStr }} `ret` is object when an exit option applied, or just `avi`
3434
* @example
3535
*/
3636
export default function parse(argv, i, req, res, err) {
@@ -52,8 +52,8 @@ export default function parse(argv, i, req, res, err) {
5252
return false;
5353
} return true;
5454
}, k = o => o == null ? key : o, // undefined is ok?
55-
ask = (msg, val) => err({msg, i, opt, key, val}),
56-
exit = c => ({ i: i + c, key, opt });
55+
ask = (msg, val) => err({msg, avi: i, opt, key, val}),
56+
exit = c => ({ avi: i+c, key, opt });
5757
// prepare
5858
/** @type {OptKeyMap} */
5959
const set_ = {}, rst_ = {}, exit_ = {};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pargv-lite",
3-
"version": "3.2.3-beta",
3+
"version": "3.3.0-beta",
44
"description": "A lightning-fast, single-pass command line argument parser with structure validation",
55
"main": "index.js",
66
"type": "module",

poke.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ poke('cannot exit within an argument && default named exit option', ['-abcd'], {
100100
🔍 { a: { def: false, set: '-a' }, b: '-b', '-c': undefined, '-d': null }
101101
💥 {
102102
msg: 'cannot exit within an argument',
103-
i: 0,
103+
avi: 0,
104104
opt: '-b',
105105
key: 'b',
106106
val: undefined
107107
}
108108
💥 {
109109
msg: 'invalid option',
110-
i: 0,
110+
avi: 0,
111111
opt: '-c',
112112
key: undefined,
113113
val: undefined
114114
}
115115
🎁 { a: true }
116-
😇 { i: 1, key: '-d', opt: '-d' }
116+
😇 { avi: 1, key: '-d', opt: '-d' }
117117
*/

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function runTest(name, fn) {
1919

2020
// Error handler for tests
2121
const errorHandler = (err) => {
22-
console.log(` Test error: ${err.msg} at position ${err.i} for option ${err.opt}`);
22+
console.log(` Test error: ${err.msg} at position ${err.avi} for option ${err.opt}`);
2323
return false; // Continue parsing
2424
};
2525

@@ -159,7 +159,7 @@ runTest('Exit options', () => {
159159
if (typeof ret !== 'object') throw ret;
160160
assert.strictEqual(ret.key, 'command');
161161
assert.strictEqual(ret.opt, 'build');
162-
assert.strictEqual(ret.i, 3);
162+
assert.strictEqual(ret.avi, 3);
163163
});
164164

165165
// TEST 6: Combined short options
@@ -314,7 +314,7 @@ runTest('Exit on anonymous arguments', () => {
314314
if (typeof ret !== 'object') throw ret;
315315
assert.strictEqual(ret.key, 'files');
316316
assert.strictEqual(ret.opt, '1.txt');
317-
assert.strictEqual(ret.i, 3);
317+
assert.strictEqual(ret.avi, 3);
318318
});
319319

320320
console.log('🎉 All tests completed!');

0 commit comments

Comments
 (0)