Skip to content

Commit afb87d4

Browse files
committed
added array option type
1 parent edf0465 commit afb87d4

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"type": "shell",
16-
"command": "yarn build && npm publish build",
16+
"command": "yarn build && npm publish build/",
1717
"problemMatcher": [
1818
"$tsc-watch"
1919
],

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "massarg",
3-
"version": "0.1.0-pre.8",
3+
"version": "0.1.0-pre.11",
44
"description": "Flexible, powerful, and simple command/argument parser for CLI applications",
55
"main": "index.js",
66
"repository": "https://github.com/chenasraf/massarg.git",
@@ -10,7 +10,7 @@
1010
"clean": "rm -rf build",
1111
"build": "yarn clean && tsc && cp package.json README.md src/*.d.ts build",
1212
"develop": "tsc --watch",
13-
"deploy": "yarn build && npm publish build"
13+
"deploy": "yarn build && npm publish build/"
1414
},
1515
"devDependencies": {
1616
"@types/chalk": "^2.2.0",

src/index.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
117117
if (option) {
118118
// detect boolean values
119119
option.boolean ??= option.parse === Boolean || [true, false].includes(option.defaultValue)
120+
option.array ??= Array.isArray(option.defaultValue)
120121

121122
let tempValue: any
122123
const hasNextToken = args.length > i + 1
@@ -180,8 +181,36 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
180181

181182
private _addOptionToData(option: OptionDef<Options, any>, value: any) {
182183
const _d: Record<string, any> = this.data
183-
_d[camelCase(option.name)] = value
184-
option.aliases?.forEach((a) => (_d[a] = value))
184+
const set = (value: any) => {
185+
_d[option.name] = value
186+
_d[camelCase(option.name)] = value
187+
option.aliases?.forEach((a) => (_d[a] = value))
188+
}
189+
const push = (value: any) => {
190+
const ccSame = camelCase(option.name) === option.name
191+
_d[option.name] ??= []
192+
_d[camelCase(option.name)] ??= []
193+
option.aliases?.forEach((a) => (_d[a] ??= []))
194+
195+
_d[option.name].push(value)
196+
if (!ccSame) {
197+
_d[camelCase(option.name)].push(value)
198+
}
199+
option.aliases?.forEach((a) => _d[a].push(value))
200+
}
201+
if (!option.array) {
202+
// single value
203+
set(value)
204+
} else {
205+
// multiple values
206+
if (Array.isArray(value) && value.length) {
207+
for (const el of value) {
208+
push(el)
209+
}
210+
} else if (!Array.isArray(value)) {
211+
push(value)
212+
}
213+
}
185214
}
186215

187216
private _getWrappedLines(list: Array<{ name: string; description?: string }>): string[] {

src/options.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export interface OptionDef<Options, Value> {
3030
*/
3131
boolean?: boolean
3232

33+
/**
34+
* An array field will collect any inputs to it into a list. Each item in the list will be parsed with `parse` before
35+
* being added.
36+
*/
37+
array?: boolean
38+
3339
/**
3440
* Commands this option is relevant for. You may use either name or alias of command, but in the help text, only the
3541
* name will be shown as the section title.

0 commit comments

Comments
 (0)