@noresources/command 0.1.0 → 0.1.1
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/package.json +5 -3
- package/src/Command.mjs +159 -171
- package/src/Options.mjs +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noresources/command",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A lightweight command line interface definition package focusing on reusability, extensibility and a clean API",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
"lint": "eslint --fix",
|
|
9
9
|
"doc": "jsdoc --recurse -c resources/jsdoc/configuration.json --readme README.md --tutorials resources/jsdoc/tutorials --destination doc src/"
|
|
10
10
|
},
|
|
11
|
-
"keywords": [
|
|
11
|
+
"keywords": [
|
|
12
|
+
|
|
13
|
+
],
|
|
12
14
|
"author": "",
|
|
13
15
|
"license": "ISC",
|
|
14
16
|
"repository": {
|
|
@@ -41,4 +43,4 @@
|
|
|
41
43
|
"./src/lib/options/index.mjs"
|
|
42
44
|
]
|
|
43
45
|
}
|
|
44
|
-
}
|
|
46
|
+
}
|
package/src/Command.mjs
CHANGED
|
@@ -1,171 +1,159 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file Defines the core structural components for building command-line interface (CLI) applications.
|
|
5
|
-
* This module provides the foundational classes (CommandBase, Command, Program) necessary to manage
|
|
6
|
-
* command hierarchy, configure metadata, and handle command execution logic within a robust JS application.
|
|
7
|
-
* It centralizes the management of command names, aliases, and execution handlers.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
options = {};
|
|
161
|
-
}
|
|
162
|
-
super (options);
|
|
163
|
-
this.name = (typeof options.name === 'string') ? options.name : 'undefined';
|
|
164
|
-
this.version = (typeof options.version === 'string') ? options.version : '1.0.0';
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export default {
|
|
169
|
-
Command,
|
|
170
|
-
Program
|
|
171
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Defines the core structural components for building command-line interface (CLI) applications.
|
|
5
|
+
* This module provides the foundational classes (CommandBase, Command, Program) necessary to manage
|
|
6
|
+
* command hierarchy, configure metadata, and handle command execution logic within a robust JS application.
|
|
7
|
+
* It centralizes the management of command names, aliases, and execution handlers.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createDocumentation } from './Documentation.mjs';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @class CommandBase
|
|
14
|
+
* @description Provides a foundational base class for defining command structures within the CLI.
|
|
15
|
+
* This base class handles common utilities such as documentation caching, managing command arguments,
|
|
16
|
+
* and attaching specific command execution handlers.
|
|
17
|
+
*/
|
|
18
|
+
class CommandBase {
|
|
19
|
+
/**
|
|
20
|
+
* Creates an instance of CommandBase.
|
|
21
|
+
* @param {object} [options] - Initialization options provided during construction.
|
|
22
|
+
* @param {object} [options.documentation] - Optional initial, raw documentation string. This string is processed to create the internal documentation object.
|
|
23
|
+
* @param {function} [options.handler] - The primary function object responsible for handling command execution. This function contains the actual business logic.
|
|
24
|
+
*/
|
|
25
|
+
constructor (options) {
|
|
26
|
+
if (!options) {
|
|
27
|
+
options = {};
|
|
28
|
+
}
|
|
29
|
+
this.documentation = options.documentation;
|
|
30
|
+
this.options = [];
|
|
31
|
+
this.commands = [];
|
|
32
|
+
this.positionals = [];
|
|
33
|
+
|
|
34
|
+
if (typeof options.handler === 'function') {
|
|
35
|
+
this.handler = options.handler;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Gets the command documentation.
|
|
41
|
+
*
|
|
42
|
+
* @returns {Documentation}
|
|
43
|
+
*/
|
|
44
|
+
get documentation () {
|
|
45
|
+
return this._documentation;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Sets the command documentation.
|
|
50
|
+
*
|
|
51
|
+
* The input value is normalized to a {Documentation} object.
|
|
52
|
+
*
|
|
53
|
+
* @param {Documentation|string|undefined value - User-defined documentation.
|
|
54
|
+
* If undefined, the documentation object is cleared.
|
|
55
|
+
*/
|
|
56
|
+
set documentation (value) {
|
|
57
|
+
if (!value) {
|
|
58
|
+
this._documentation = undefined;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
this._documentation = createDocumentation(value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get handler () {
|
|
66
|
+
return this._handler;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Attaches the primary handler function responsible for executing the command logic.
|
|
72
|
+
* This handler is the core logic execution entry point that dictates the command's behavior when invoked.
|
|
73
|
+
* @param {function} handler - The callable function object that encapsulates the complete command execution logic.
|
|
74
|
+
* @returns {void}
|
|
75
|
+
* @throws {Error} Throws an Error if the provided 'handler' argument is not a valid function type.
|
|
76
|
+
*/
|
|
77
|
+
set handler (handler) {
|
|
78
|
+
if (typeof handler !== 'function') {
|
|
79
|
+
throw new Error ('Invalid handler argument. A function object is expected.');
|
|
80
|
+
}
|
|
81
|
+
this._handler = handler;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @class Command
|
|
87
|
+
* @extends {CommandBase}
|
|
88
|
+
* @description Represents a single, executable command unit within the CLI program structure.
|
|
89
|
+
* This class is responsible for defining the command's unique identifier(s), managing aliases,
|
|
90
|
+
* and providing a specialized interface on top of CommandBase. larger application ecosystem.
|
|
91
|
+
*/
|
|
92
|
+
export class Command extends CommandBase {
|
|
93
|
+
/**
|
|
94
|
+
* Creates an instance of Command, setting up its formal primary name and registering callable aliases.
|
|
95
|
+
* @param {object} options - Command initialization options.
|
|
96
|
+
* @param {Array<string>} [options.names] - An optional array of names. The first element MUST be the primary command name, while subsequent elements serve as secondary aliases for execution convenience.
|
|
97
|
+
*/
|
|
98
|
+
constructor (options) {
|
|
99
|
+
if (!options) {
|
|
100
|
+
options = {};
|
|
101
|
+
}
|
|
102
|
+
super (options);
|
|
103
|
+
this.names = [];
|
|
104
|
+
if (Array.isArray (options.names)) {
|
|
105
|
+
options.names.forEach(n => {
|
|
106
|
+
this.names.push (n);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Retrieves the primary, canonical name of the command.
|
|
113
|
+
* This name is defined by the first element provided during construction and is the primary identifier used for invocation.
|
|
114
|
+
* @returns {string} The main name used to execute the command (e.g., 'user'). This string cannot be empty.
|
|
115
|
+
*/
|
|
116
|
+
get name () {
|
|
117
|
+
return this.names[0];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Retrieves an array of recognized aliases for this command.
|
|
122
|
+
* These are supplementary names that can be used interchangeably with the main command name.
|
|
123
|
+
* @returns {Array<string>} An array containing all secondary aliases registered for this command. This array may be empty.
|
|
124
|
+
*/
|
|
125
|
+
get aliases () {
|
|
126
|
+
return this.names.slice (1);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @class Program
|
|
132
|
+
*
|
|
133
|
+
* @extends {CommandBase}
|
|
134
|
+
* @description Represents the root executable container for the entire CLI application.
|
|
135
|
+
* This class establishes the global context of the application, holding critical metadata
|
|
136
|
+
* such as the application name and version. It functions as the primary organizational entry point
|
|
137
|
+
* from which all sub-commands are registered, managed, and ultimately executed.
|
|
138
|
+
*/
|
|
139
|
+
export class Program extends CommandBase {
|
|
140
|
+
/**
|
|
141
|
+
* Creates an instance of Program, setting up the global application metadata that defines the CLI interface.
|
|
142
|
+
* @param {object} [options] - Program configuration options.
|
|
143
|
+
* @param {string} [options.name] - The official, user-facing name of the entire program (e.g., 'mytool'). This is used in help output.
|
|
144
|
+
* @param {string} [options.version] - The current, semantic version string of the application (e.g., '1.0.0').
|
|
145
|
+
*/
|
|
146
|
+
constructor (options) {
|
|
147
|
+
if (!options) {
|
|
148
|
+
options = {};
|
|
149
|
+
}
|
|
150
|
+
super (options);
|
|
151
|
+
this.name = (typeof options.name === 'string') ? options.name : 'undefined';
|
|
152
|
+
this.version = (typeof options.version === 'string') ? options.version : '1.0.0';
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export default {
|
|
157
|
+
Command,
|
|
158
|
+
Program
|
|
159
|
+
};
|
package/src/Options.mjs
CHANGED
|
@@ -445,8 +445,8 @@ function autodetectType (options) {
|
|
|
445
445
|
for (const m of mapping) {
|
|
446
446
|
const [name, type] = m;
|
|
447
447
|
if (typeof options[name] !== 'undefined') {
|
|
448
|
-
if (typeof t === 'string') {
|
|
449
|
-
throw new Error ('Ambiguous option descriptor');
|
|
448
|
+
if (typeof t === 'string' && (t != type)) {
|
|
449
|
+
throw new Error ('Ambiguous option descriptor. Could be ' + t + ' or ' + type);
|
|
450
450
|
}
|
|
451
451
|
t = type;
|
|
452
452
|
}
|