@bemoje/cli 0.0.5 → 0.0.6
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/dist/cjs/_example/bFindIn.js +189 -0
- package/dist/cjs/_example/bFindIn.js.map +1 -0
- package/dist/cjs/cmd/CommandBuilder.js +34 -52
- package/dist/cjs/cmd/CommandBuilder.js.map +1 -1
- package/dist/cjs/cmd/CommandBuilderMetaData.js +1 -0
- package/dist/cjs/cmd/CommandBuilderMetaData.js.map +1 -1
- package/dist/cjs/index.js +1 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/_example/bFindIn.js +187 -0
- package/dist/esm/_example/bFindIn.js.map +1 -0
- package/dist/esm/cmd/CommandBuilder.js +32 -50
- package/dist/esm/cmd/CommandBuilder.js.map +1 -1
- package/dist/esm/cmd/CommandBuilderMetaData.js +1 -0
- package/dist/esm/cmd/CommandBuilderMetaData.js.map +1 -1
- package/dist/esm/index.js +1 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/types/_example/bFindIn.d.ts +1 -0
- package/dist/types/cmd/CommandBuilder.d.ts +6 -0
- package/dist/types/cmd/CommandBuilderMetaData.d.ts +1 -0
- package/dist/types/index.d.ts +1 -2
- package/package.json +1 -1
- package/project.json +1 -0
- package/{scripts → src}/_example/CommandBuilder.example.ts +5 -9
- package/{scripts → src}/_example/CommandBuilder.example2.ts +3 -4
- package/{scripts → src}/_example/bFindIn.ts +5 -12
- package/src/arg/ArgumentParserSelector.test.ts +9 -9
- package/src/arg/ArgumentValidatorSelector.test.ts +17 -17
- package/src/cmd/CommandBuilder.test.ts +19 -19
- package/src/cmd/CommandBuilder.ts +42 -53
- package/src/cmd/CommandBuilderMetaData.ts +1 -0
- package/src/index.ts +2 -3
- package/src/opt/OptionArgumentParserSelector.test.ts +16 -15
- package/src/opt/OptionArgumentValidatorSelector.test.ts +16 -17
- package/src/util/array/arrSome.test.ts +3 -3
- package/dist/cjs/cmd/CLI.js +0 -16
- package/dist/cjs/cmd/CLI.js.map +0 -1
- package/dist/cjs/proto/overrideCommanderPrototype.js +0 -37
- package/dist/cjs/proto/overrideCommanderPrototype.js.map +0 -1
- package/dist/esm/cmd/CLI.js +0 -12
- package/dist/esm/cmd/CLI.js.map +0 -1
- package/dist/esm/proto/overrideCommanderPrototype.js +0 -34
- package/dist/esm/proto/overrideCommanderPrototype.js.map +0 -1
- package/dist/types/cmd/CLI.d.ts +0 -2
- package/dist/types/proto/overrideCommanderPrototype.d.ts +0 -8
- package/scripts/_example/CommandBuilder.example3.ts +0 -20
- package/src/cmd/CLI.test.ts +0 -14
- package/src/cmd/CLI.ts +0 -13
- package/src/proto/overrideCommanderPrototype.ts +0 -42
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CommandBuilder } from '../cmd/CommandBuilder'
|
|
2
2
|
|
|
3
3
|
console.time('load')
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const cli = new CommandBuilder('demo', (c) => {
|
|
6
6
|
c.version('0.0.1')
|
|
7
7
|
c.description('A CLI example')
|
|
8
8
|
c.setRecommended()
|
|
@@ -23,9 +23,8 @@ const init = CLI('demo', (c) => {
|
|
|
23
23
|
console.timeEnd('load')
|
|
24
24
|
|
|
25
25
|
console.time('init')
|
|
26
|
-
const cli = init()
|
|
27
26
|
console.timeEnd('init')
|
|
28
27
|
|
|
29
28
|
console.time('exec')
|
|
30
|
-
cli
|
|
29
|
+
cli.$.parse()
|
|
31
30
|
console.timeEnd('exec')
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { execInherit } from '
|
|
3
|
-
import {
|
|
4
|
-
import { strSplitCamelCase } from '../../src/util/string/strSplitCamelCase'
|
|
1
|
+
import { CommandBuilder } from '../cmd/CommandBuilder'
|
|
2
|
+
import { execInherit } from '../util/node/execInherit'
|
|
3
|
+
import { strSplitCamelCase } from '../util/string/strSplitCamelCase'
|
|
5
4
|
|
|
6
5
|
console.time('load')
|
|
7
6
|
|
|
8
|
-
const
|
|
7
|
+
const cli = new CommandBuilder('bFindIn', (b) => {
|
|
9
8
|
b.setRecommended()
|
|
10
9
|
b.version('0.0.1')
|
|
11
10
|
b.description(
|
|
@@ -196,12 +195,6 @@ const init = CLI('bFindIn', (b) => {
|
|
|
196
195
|
})
|
|
197
196
|
console.timeEnd('load')
|
|
198
197
|
|
|
199
|
-
console.time('init')
|
|
200
|
-
const cli = init()
|
|
201
|
-
console.timeEnd('init')
|
|
202
|
-
|
|
203
198
|
console.time('exec')
|
|
204
|
-
cli
|
|
199
|
+
cli.$.parse()
|
|
205
200
|
console.timeEnd('exec')
|
|
206
|
-
|
|
207
|
-
printCounts()
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ArgumentParserSelector } from './ArgumentParserSelector'
|
|
2
|
-
import {
|
|
2
|
+
import { CommandBuilder } from '../cmd/CommandBuilder'
|
|
3
3
|
|
|
4
4
|
describe(ArgumentParserSelector.name, () => {
|
|
5
5
|
it('should parse string, number, integer', () => {
|
|
6
|
-
const run =
|
|
6
|
+
const run = new CommandBuilder('t', (c) => {
|
|
7
7
|
c.argument('<str>', (a) => a.parser.string())
|
|
8
8
|
c.argument('<num>', (a) => a.parser.number())
|
|
9
9
|
c.argument('<int>', (a) => a.parser.integer())
|
|
@@ -13,22 +13,22 @@ describe(ArgumentParserSelector.name, () => {
|
|
|
13
13
|
expect(int).toBe(3)
|
|
14
14
|
})
|
|
15
15
|
})
|
|
16
|
-
run
|
|
16
|
+
run.$.parse(['str', '3.2', '3'], { from: 'user' })
|
|
17
17
|
})
|
|
18
18
|
|
|
19
19
|
it('should parse variadic argument', () => {
|
|
20
|
-
const
|
|
20
|
+
const run = new CommandBuilder('t', (c) => {
|
|
21
21
|
c.argument('<str>', (o) => o.parser.string())
|
|
22
22
|
c.argument('<ints...>', (o) => o.parser.integer())
|
|
23
23
|
c.action((str, ints) => {
|
|
24
24
|
expect(ints).toEqual([3, 4, 5])
|
|
25
25
|
})
|
|
26
26
|
})
|
|
27
|
-
|
|
27
|
+
run.$.parse('abc 3 4 5'.split(' '), { from: 'user' })
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
it('should parse delimited string, number, integer', () => {
|
|
31
|
-
const run =
|
|
31
|
+
const run = new CommandBuilder('t', (c) => {
|
|
32
32
|
c.argument('<strs>', (a) => a.parser.delimitedStrings())
|
|
33
33
|
c.argument('<nums>', (a) => a.parser.delimitedNumbers())
|
|
34
34
|
c.argument('<ints>', (a) => a.parser.delimitedIntegers())
|
|
@@ -38,16 +38,16 @@ describe(ArgumentParserSelector.name, () => {
|
|
|
38
38
|
expect(ints).toEqual([3, 4, 5])
|
|
39
39
|
})
|
|
40
40
|
})
|
|
41
|
-
run
|
|
41
|
+
run.$.parse(['a,b', '3.1,4.1', '3,4,5'], { from: 'user' })
|
|
42
42
|
})
|
|
43
43
|
|
|
44
44
|
it('should parse with custom parser', () => {
|
|
45
|
-
const run =
|
|
45
|
+
const run = new CommandBuilder('t', (c) => {
|
|
46
46
|
c.argument('<arg>', (a) => a.parser.custom((s) => s.split('')))
|
|
47
47
|
c.action((arg) => {
|
|
48
48
|
expect(arg).toEqual(['a', 'b', 'c'])
|
|
49
49
|
})
|
|
50
50
|
})
|
|
51
|
-
run
|
|
51
|
+
run.$.parse(['abc'], { from: 'user' })
|
|
52
52
|
})
|
|
53
53
|
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
3
|
import { ArgumentValidatorSelector } from './ArgumentValidatorSelector'
|
|
4
|
-
import {
|
|
4
|
+
import { CommandBuilder } from '../cmd/CommandBuilder'
|
|
5
5
|
|
|
6
6
|
describe(ArgumentValidatorSelector.name, () => {
|
|
7
7
|
jest.spyOn(process, 'exit').mockImplementation((code?: number) => {
|
|
@@ -9,63 +9,63 @@ describe(ArgumentValidatorSelector.name, () => {
|
|
|
9
9
|
})
|
|
10
10
|
|
|
11
11
|
it('should validate string, number, integer', () => {
|
|
12
|
-
const run =
|
|
12
|
+
const run = new CommandBuilder('t', (c) => {
|
|
13
13
|
c.argument('<str>', (a) => a.parser.string().validator.isString())
|
|
14
14
|
c.argument('<num>', (a) => a.parser.number().validator.isNumber())
|
|
15
15
|
c.argument('<int>', (a) => a.parser.integer().validator.isInteger())
|
|
16
16
|
c.action(() => {})
|
|
17
17
|
})
|
|
18
|
-
expect(() => run
|
|
18
|
+
expect(() => run.$.parse(['str', '3.2', '3'], { from: 'user' })).not.toThrow()
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
it('should throw if invalid', () => {
|
|
22
|
-
const run =
|
|
22
|
+
const run = new CommandBuilder('t', (c) => {
|
|
23
23
|
c.argument('<int>', (a) => a.parser.number().validator.isInteger())
|
|
24
24
|
c.action(() => {})
|
|
25
25
|
c.throwInsteadOfProcessExit()
|
|
26
26
|
})
|
|
27
|
-
expect(() => run
|
|
27
|
+
expect(() => run.$.parse(['3.2'], { from: 'user' })).toThrow()
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
it('should validate delimited string, number, integer', () => {
|
|
31
|
-
const run =
|
|
31
|
+
const run = new CommandBuilder('t', (c) => {
|
|
32
32
|
c.argument('<strs>', (a) => a.parser.delimitedStrings().validator.isStringArray())
|
|
33
33
|
c.argument('<nums>', (a) => a.parser.delimitedNumbers().validator.isNumberArray())
|
|
34
34
|
c.argument('<ints>', (a) => a.parser.delimitedIntegers().validator.isIntegerArray())
|
|
35
35
|
c.action(() => {})
|
|
36
36
|
})
|
|
37
|
-
expect(() => run
|
|
37
|
+
expect(() => run.$.parse(['a,b', '3.1,4.1', '3,4,5'], { from: 'user' })).not.toThrow()
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
it('should validate variadic argument', () => {
|
|
41
|
-
const
|
|
41
|
+
const run = new CommandBuilder('t', (c) => {
|
|
42
42
|
c.argument('<str>', (o) => o.validator.isString())
|
|
43
43
|
c.argument('<ints...>', (o) => o.parser.integer().validator.isIntegerArray())
|
|
44
44
|
c.action(() => {})
|
|
45
45
|
})
|
|
46
|
-
expect(() =>
|
|
46
|
+
expect(() => run.$.parse('abc 3 4 5'.split(' '), { from: 'user' })).not.toThrow()
|
|
47
47
|
})
|
|
48
48
|
|
|
49
49
|
it('should throw if invalid', () => {
|
|
50
|
-
const run =
|
|
50
|
+
const run = new CommandBuilder('t', (c) => {
|
|
51
51
|
c.argument('<ints>', (a) => a.parser.delimitedIntegers().validator.isIntegerArray())
|
|
52
52
|
c.action(() => {})
|
|
53
53
|
c.throwInsteadOfProcessExit()
|
|
54
54
|
})
|
|
55
|
-
expect(() => run
|
|
55
|
+
expect(() => run.$.parse(['3,q,7'], { from: 'user' })).toThrow()
|
|
56
56
|
})
|
|
57
57
|
|
|
58
58
|
it('should validate with custom validator', () => {
|
|
59
|
-
const run =
|
|
59
|
+
const run = new CommandBuilder('t', (c) => {
|
|
60
60
|
c.argument('<arg>', (a) => a.validator.custom((arg) => arg === 'abc'))
|
|
61
61
|
c.action(() => {})
|
|
62
62
|
c.throwInsteadOfProcessExit()
|
|
63
63
|
})
|
|
64
|
-
expect(() => run
|
|
64
|
+
expect(() => run.$.parse(['ab'], { from: 'user' })).toThrow()
|
|
65
65
|
})
|
|
66
66
|
|
|
67
67
|
it('should validate with multiple validators', () => {
|
|
68
|
-
const run =
|
|
68
|
+
const run = new CommandBuilder('t', (c) => {
|
|
69
69
|
c.argument('<arg>', (a) => {
|
|
70
70
|
a.validator.custom((arg: string) => arg !== 'abc')
|
|
71
71
|
a.validator.custom((arg: string) => arg.length >= 3)
|
|
@@ -73,8 +73,8 @@ describe(ArgumentValidatorSelector.name, () => {
|
|
|
73
73
|
c.action(() => {})
|
|
74
74
|
c.throwInsteadOfProcessExit()
|
|
75
75
|
})
|
|
76
|
-
expect(() => run
|
|
77
|
-
expect(() => run
|
|
78
|
-
expect(() => run
|
|
76
|
+
expect(() => run.$.parse(['ab'], { from: 'user' })).toThrow()
|
|
77
|
+
expect(() => run.$.parse(['abc'], { from: 'user' })).toThrow()
|
|
78
|
+
expect(() => run.$.parse(['abcd'], { from: 'user' })).not.toThrow()
|
|
79
79
|
})
|
|
80
80
|
})
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
2
|
import colors from 'ansi-colors'
|
|
3
|
-
import { CLI } from './CLI'
|
|
4
3
|
import { CommandBuilder } from './CommandBuilder'
|
|
5
4
|
import { OutputManager } from '../core/OutputManager'
|
|
6
5
|
|
|
@@ -573,37 +572,38 @@ describe(CommandBuilder.name, () => {
|
|
|
573
572
|
})
|
|
574
573
|
|
|
575
574
|
let result = ''
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
575
|
+
const t = () =>
|
|
576
|
+
new CommandBuilder('t', (c) => {
|
|
577
|
+
c.argument('[name]')
|
|
578
|
+
c.action((name: string) => {
|
|
579
|
+
result = name
|
|
580
|
+
})
|
|
581
|
+
c.preset('john', {
|
|
582
|
+
description: '',
|
|
583
|
+
args: ['john'],
|
|
584
|
+
})
|
|
585
|
+
c.preset('anna', {
|
|
586
|
+
description: '',
|
|
587
|
+
args: ['anna'],
|
|
588
|
+
})
|
|
588
589
|
})
|
|
589
|
-
})
|
|
590
590
|
|
|
591
591
|
it('should parse presets on action', () => {
|
|
592
|
-
|
|
592
|
+
t().$.parse(['--john'], { from: 'user' })
|
|
593
593
|
expect(result).toBe('john')
|
|
594
|
-
|
|
594
|
+
t().$.parse(['--anna'], { from: 'user' })
|
|
595
595
|
expect(result).toBe('anna')
|
|
596
596
|
})
|
|
597
597
|
|
|
598
598
|
it('should let stacked presets override each other', () => {
|
|
599
|
-
|
|
599
|
+
t().$.parse('--anna --john'.split(' '), { from: 'user' })
|
|
600
600
|
expect(result).toBe('john')
|
|
601
|
-
|
|
601
|
+
t().$.parse('--john --anna'.split(' '), { from: 'user' })
|
|
602
602
|
expect(result).toBe('anna')
|
|
603
603
|
})
|
|
604
604
|
|
|
605
605
|
it('should always have arguments override preset values', () => {
|
|
606
|
-
|
|
606
|
+
t().$.parse('mia --john --anna'.split(' '), { from: 'user' })
|
|
607
607
|
expect(result).toBe('mia')
|
|
608
608
|
})
|
|
609
609
|
})
|
|
@@ -10,7 +10,6 @@ import { arrAssign } from '../util/object/arrAssign'
|
|
|
10
10
|
import { arrLast } from '../util/array/arrLast'
|
|
11
11
|
import { arrSome } from '../util/array/arrSome'
|
|
12
12
|
import { CommandBuilderMetaData } from './CommandBuilderMetaData'
|
|
13
|
-
import { commanderBackRefs } from '../proto/overrideCommanderPrototype'
|
|
14
13
|
import { CommandFeatureSelector } from './CommandFeatureSelector'
|
|
15
14
|
import { DefaultHelpConfig } from './DefaultHelpConfig'
|
|
16
15
|
import { ensureThat } from '../util/validation/ensureThat'
|
|
@@ -30,10 +29,8 @@ import { OutputManager } from '../core/OutputManager'
|
|
|
30
29
|
import { PresetsSection } from '../db/PresetsSection'
|
|
31
30
|
import { realizeLazyProperty } from '../util/object/realizeLazyProperty'
|
|
32
31
|
import { removeFile } from '../util/fs/removeFile/removeFile'
|
|
33
|
-
import type { ReturnTypesOfFunctionArray, ReturnTypesOfFunctionMap } from './asdas'
|
|
34
32
|
import { setNonEnumerable } from '../util/object/setNonEnumerable'
|
|
35
33
|
import { splitCombinedArgvShorts } from '../core/splitCombinedArgvShorts'
|
|
36
|
-
import type { TStringParser } from '../util/types/TStringParser'
|
|
37
34
|
import {
|
|
38
35
|
type AddHelpTextPosition,
|
|
39
36
|
Argument,
|
|
@@ -47,6 +44,7 @@ import {
|
|
|
47
44
|
Option,
|
|
48
45
|
type OptionValues,
|
|
49
46
|
type OptionValueSource,
|
|
47
|
+
ParseOptions,
|
|
50
48
|
} from '@commander-js/extra-typings'
|
|
51
49
|
import { type JsonValue } from '../util/types/JsonValue'
|
|
52
50
|
|
|
@@ -103,7 +101,6 @@ export class CommandBuilder<Args extends Any[] = unknown[], Opts extends OptionV
|
|
|
103
101
|
child.assertNoDuplicateCommandNames()
|
|
104
102
|
}
|
|
105
103
|
}
|
|
106
|
-
|
|
107
104
|
if (child.features.isAutoAssignMissingOptionFlagsEnabled) {
|
|
108
105
|
child.assignMissingOptionFlags()
|
|
109
106
|
if (!child.meta.isNative) {
|
|
@@ -220,6 +217,7 @@ export class CommandBuilder<Args extends Any[] = unknown[], Opts extends OptionV
|
|
|
220
217
|
}
|
|
221
218
|
action(fn: (...args: any[]) => void | Promise<void>): this {
|
|
222
219
|
const isAsync = isAsyncFunction(fn) || /^\(.+\) ?=> ?tslib_1\.__awaiter\(/.test(fn.toString().trim())
|
|
220
|
+
this.meta.isActionAsync = isAsync
|
|
223
221
|
Object.defineProperty(this.meta, 'actionHandler', { value: fn, configurable: false, writable: false })
|
|
224
222
|
this.initializeActionWrapper(isAsync)
|
|
225
223
|
return this
|
|
@@ -917,55 +915,6 @@ export class CommandBuilder<Args extends Any[] = unknown[], Opts extends OptionV
|
|
|
917
915
|
console.info(db.getAll())
|
|
918
916
|
})
|
|
919
917
|
})
|
|
920
|
-
/*
|
|
921
|
-
config.option('--editor [cmd]', (o) => {
|
|
922
|
-
o.description('The command to launch your preferred text editor.')
|
|
923
|
-
})
|
|
924
|
-
config.argument('[action]', (a) => {
|
|
925
|
-
a.description('The action to perform.')
|
|
926
|
-
a.choices(['edit', 'list', 'get', 'set', 'reset'])
|
|
927
|
-
a.default('edit')
|
|
928
|
-
})
|
|
929
|
-
config.argument('[key]', (a) => {
|
|
930
|
-
a.description('Property key (if applicable)')
|
|
931
|
-
})
|
|
932
|
-
config.argument('[value]', (a) => {
|
|
933
|
-
a.description('Value to set (if applicable)')
|
|
934
|
-
})
|
|
935
|
-
config.action(
|
|
936
|
-
async (action: string, key: string, value: string, opts: { editor: string }, config: CommandBuilder) => {
|
|
937
|
-
const cmd = config.getClosestNonNativeParent()
|
|
938
|
-
const cfg = cmd.db.config
|
|
939
|
-
if (!action || action === 'edit') {
|
|
940
|
-
cfg.edit(opts.editor)
|
|
941
|
-
return console.info(cfg.getAll())
|
|
942
|
-
} else if (action === 'list') {
|
|
943
|
-
return console.dir(
|
|
944
|
-
cfg.keys.map((key: string) => {
|
|
945
|
-
return {
|
|
946
|
-
key,
|
|
947
|
-
description: cfg.descriptions[key],
|
|
948
|
-
value: cfg.get(key),
|
|
949
|
-
defaultValue: cfg.defaultValues,
|
|
950
|
-
}
|
|
951
|
-
})
|
|
952
|
-
)
|
|
953
|
-
} else if (action === 'get') {
|
|
954
|
-
if (key) return console.log(cfg.get(key))
|
|
955
|
-
else return console.log(cfg.getAll())
|
|
956
|
-
} else if (action === 'set') {
|
|
957
|
-
const from = cfg.get(key)
|
|
958
|
-
const parse = cfg.parsers[key]
|
|
959
|
-
const to = typeof parse === 'function' ? cfg.parsers[key](value) : value
|
|
960
|
-
cfg.set(key, to)
|
|
961
|
-
return console.info({ changed: key, from, to })
|
|
962
|
-
} else if (action === 'reset') {
|
|
963
|
-
if (key) cfg.reset(key)
|
|
964
|
-
else cfg.resetAll()
|
|
965
|
-
return console.info(cfg.getAll())
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
)*/
|
|
969
918
|
}
|
|
970
919
|
})
|
|
971
920
|
}
|
|
@@ -1173,3 +1122,43 @@ export class CommandBuilder<Args extends Any[] = unknown[], Opts extends OptionV
|
|
|
1173
1122
|
}
|
|
1174
1123
|
|
|
1175
1124
|
process.argv = splitCombinedArgvShorts(process.argv.slice())
|
|
1125
|
+
|
|
1126
|
+
export const commanderBackRefs = new WeakMap<Command<Any, Any>, CommandBuilder<Any, Any>>()
|
|
1127
|
+
const oldParse = Command.prototype.parse
|
|
1128
|
+
Command.prototype.parse = function parse(this: Command, argv?: readonly string[], options?: ParseOptions) {
|
|
1129
|
+
if (argv) {
|
|
1130
|
+
argv = splitCombinedArgvShorts(argv.slice())
|
|
1131
|
+
this.builder.meta.rawArgs = argv.slice(options?.from === 'user' ? 0 : 2)
|
|
1132
|
+
} else {
|
|
1133
|
+
this.builder.meta.rawArgs = process.argv.slice(2)
|
|
1134
|
+
}
|
|
1135
|
+
return oldParse.call(this, argv, options)
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
const oldParseAsync = Command.prototype.parseAsync
|
|
1139
|
+
Command.prototype.parseAsync = async function (this: Command, argv?: readonly string[], options?: ParseOptions) {
|
|
1140
|
+
if (argv) {
|
|
1141
|
+
argv = splitCombinedArgvShorts(argv.slice())
|
|
1142
|
+
this.builder.meta.rawArgs = argv.slice(options?.from === 'user' ? 0 : 2)
|
|
1143
|
+
} else {
|
|
1144
|
+
this.builder.meta.rawArgs = process.argv.slice(2)
|
|
1145
|
+
}
|
|
1146
|
+
return await oldParseAsync.call(this, argv, options)
|
|
1147
|
+
}
|
|
1148
|
+
Object.defineProperty(Command.prototype, 'builder', {
|
|
1149
|
+
get(this: Command) {
|
|
1150
|
+
const ins = commanderBackRefs.get(this)
|
|
1151
|
+
if (!ins) throw new Error(`CommandBuilder not found for command ${this.name()}`)
|
|
1152
|
+
return ins
|
|
1153
|
+
},
|
|
1154
|
+
})
|
|
1155
|
+
|
|
1156
|
+
declare module '@commander-js/extra-typings' {
|
|
1157
|
+
interface Command {
|
|
1158
|
+
get builder(): CommandBuilder
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
export function CLI(name: string, cb: (cmd: CommandBuilder) => void) {
|
|
1163
|
+
return new CommandBuilder(name, cb).commander
|
|
1164
|
+
}
|
|
@@ -18,6 +18,7 @@ export class CommandBuilderMetaData<Args extends Any[] = unknown[]> {
|
|
|
18
18
|
optValidators: Record<string, TValidator<Any>[]> = {}
|
|
19
19
|
rawArgs: string[] = []
|
|
20
20
|
isNative = false
|
|
21
|
+
isActionAsync = false
|
|
21
22
|
|
|
22
23
|
get actionHandler(): (this: CommandBuilder, ...args: any[]) => void | Promise<void> {
|
|
23
24
|
return async function defaultActionHandler(this: CommandBuilder) {
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,6 @@ export * from './arg/ArgumentParserSelector'
|
|
|
3
3
|
export * from './arg/ArgumentReader'
|
|
4
4
|
export * from './arg/ArgumentValidatorSelector'
|
|
5
5
|
export * from './cmd/asdas'
|
|
6
|
-
export * from './cmd/CLI'
|
|
7
6
|
export * from './cmd/CommandBuilder'
|
|
8
7
|
export * from './cmd/CommandBuilderMetaData'
|
|
9
8
|
export * from './cmd/CommandFeatureSelector'
|
|
@@ -22,7 +21,6 @@ export * from './opt/OptionArgumentValidatorSelector'
|
|
|
22
21
|
export * from './opt/OptionBuilder'
|
|
23
22
|
export * from './opt/OptionHelpers'
|
|
24
23
|
export * from './opt/OptionReader'
|
|
25
|
-
export * from './proto/overrideCommanderPrototype'
|
|
26
24
|
export * from './types/IConfig'
|
|
27
25
|
export * from './types/IPreset'
|
|
28
26
|
export * from './util/array/arrLast'
|
|
@@ -100,4 +98,5 @@ export * from './util/validation/isString'
|
|
|
100
98
|
export * from './util/validation/isStringArray'
|
|
101
99
|
export * from './util/validation/isStringWithNoSpacesOrDashes'
|
|
102
100
|
export * from './util/validation/numbers/isInteger'
|
|
103
|
-
export * from './util/validation/numbers/isValidNumber'
|
|
101
|
+
export * from './util/validation/numbers/isValidNumber'
|
|
102
|
+
export * from './_example/bFindIn'
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CommandBuilder } from '../cmd/CommandBuilder'
|
|
2
2
|
import { OptionArgumentParserSelector } from './OptionArgumentParserSelector'
|
|
3
3
|
|
|
4
4
|
describe(OptionArgumentParserSelector.name, () => {
|
|
5
5
|
it('should parse string, number, integer', () => {
|
|
6
|
-
const init =
|
|
6
|
+
const init = new CommandBuilder('t', (c) => {
|
|
7
7
|
c.option('--str <val>', (o) => o.parser.string())
|
|
8
8
|
c.option('--num <val>', (o) => o.parser.number())
|
|
9
9
|
c.option('--int <val>', (o) => o.parser.integer())
|
|
@@ -13,23 +13,24 @@ describe(OptionArgumentParserSelector.name, () => {
|
|
|
13
13
|
expect(opts.int).toBe(3)
|
|
14
14
|
})
|
|
15
15
|
})
|
|
16
|
-
init
|
|
16
|
+
init.$.parse('--str str --num 3.2 --int 3'.split(' '), { from: 'user' })
|
|
17
17
|
})
|
|
18
18
|
|
|
19
19
|
it('should parse variadic option', () => {
|
|
20
|
-
const init =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
const init = () =>
|
|
21
|
+
new CommandBuilder('t', (c) => {
|
|
22
|
+
c.option('--ints <val...>', (o) => o.parser.integer())
|
|
23
|
+
c.option('--str <val>', (o) => o.parser.string())
|
|
24
|
+
c.action((opts) => {
|
|
25
|
+
expect(opts.ints).toEqual([3, 4, 5])
|
|
26
|
+
})
|
|
25
27
|
})
|
|
26
|
-
})
|
|
27
|
-
init()
|
|
28
|
-
init().parse('--ints 3 4 5 --str abc'.split(' '), { from: 'user' })
|
|
28
|
+
init().$.parse('--ints 3 4 5'.split(' '), { from: 'user' })
|
|
29
|
+
init().$.parse('--ints 3 4 5 --str abc'.split(' '), { from: 'user' })
|
|
29
30
|
})
|
|
30
31
|
|
|
31
32
|
it('should parse delimited string, number, integer', () => {
|
|
32
|
-
const init =
|
|
33
|
+
const init = new CommandBuilder('t', (c) => {
|
|
33
34
|
c.option('--strs <val>', (o) => o.parser.delimitedStrings())
|
|
34
35
|
c.option('--nums <val>', (o) => o.parser.delimitedNumbers())
|
|
35
36
|
c.option('--ints <val>', (o) => o.parser.delimitedIntegers())
|
|
@@ -39,16 +40,16 @@ describe(OptionArgumentParserSelector.name, () => {
|
|
|
39
40
|
expect(opts.ints).toEqual([3, 4, 5])
|
|
40
41
|
})
|
|
41
42
|
})
|
|
42
|
-
init
|
|
43
|
+
init.$.parse('--strs a,b --nums 3.1,4.1 --ints 3,4,5'.split(' '), { from: 'user' })
|
|
43
44
|
})
|
|
44
45
|
|
|
45
46
|
it('should parse with custom parser', () => {
|
|
46
|
-
const init =
|
|
47
|
+
const init = new CommandBuilder('t', (c) => {
|
|
47
48
|
c.option('--chars <val>', (o) => o.parser.custom((s) => s.split('')))
|
|
48
49
|
c.action((opts) => {
|
|
49
50
|
expect(opts.chars).toEqual(['a', 'b', 'c'])
|
|
50
51
|
})
|
|
51
52
|
})
|
|
52
|
-
init
|
|
53
|
+
init.$.parse('--chars abc'.split(' '), { from: 'user' })
|
|
53
54
|
})
|
|
54
55
|
})
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
-
import { CLI } from '../cmd/CLI'
|
|
4
3
|
import { CommandBuilder } from '../cmd/CommandBuilder'
|
|
5
4
|
import { OptionArgumentValidatorSelector } from './OptionArgumentValidatorSelector'
|
|
6
5
|
import { OptionBuilder } from './OptionBuilder'
|
|
@@ -17,65 +16,65 @@ describe(OptionArgumentValidatorSelector.name, () => {
|
|
|
17
16
|
})
|
|
18
17
|
|
|
19
18
|
it('should validate string, number, integer', () => {
|
|
20
|
-
const run =
|
|
19
|
+
const run = new CommandBuilder('t', (c) => {
|
|
21
20
|
c.option('--s <str>', (o) => o.parser.string().validator.isString())
|
|
22
21
|
c.option('--n <num>', (o) => o.parser.number().validator.isNumber())
|
|
23
22
|
c.option('--i <int>', (o) => o.parser.integer().validator.isInteger())
|
|
24
23
|
c.action(() => {})
|
|
25
24
|
})
|
|
26
|
-
expect(() => run
|
|
25
|
+
expect(() => run.$.parse('--s str --n 3.2 --i 3'.split(' '), { from: 'user' })).not.toThrow()
|
|
27
26
|
})
|
|
28
27
|
|
|
29
28
|
it('should throw if invalid', () => {
|
|
30
|
-
const run =
|
|
29
|
+
const run = new CommandBuilder('t', (c) => {
|
|
31
30
|
c.throwInsteadOfProcessExit()
|
|
32
31
|
c.option('--i <int>', (a) => a.parser.number().validator.isInteger())
|
|
33
32
|
c.action(() => {})
|
|
34
33
|
c.throwInsteadOfProcessExit()
|
|
35
34
|
})
|
|
36
|
-
expect(() => run
|
|
35
|
+
expect(() => run.$.parse('--i 3.2'.split(' '), { from: 'user' })).toThrow()
|
|
37
36
|
})
|
|
38
37
|
|
|
39
38
|
it('should validate delimited string, number, integer', () => {
|
|
40
|
-
const run =
|
|
39
|
+
const run = new CommandBuilder('t', (c) => {
|
|
41
40
|
c.option('--s <strs>', (o) => o.parser.delimitedStrings().validator.isStringArray())
|
|
42
41
|
c.option('--n <nums>', (o) => o.parser.delimitedNumbers().validator.isNumberArray())
|
|
43
42
|
c.option('--i <ints>', (o) => o.parser.delimitedIntegers().validator.isIntegerArray())
|
|
44
43
|
c.action(() => {})
|
|
45
44
|
})
|
|
46
|
-
run
|
|
45
|
+
run.$.parse('--s a,b --n 3.1,4.1 --i 3,4,5'.split(' '), { from: 'user' })
|
|
47
46
|
})
|
|
48
47
|
|
|
49
48
|
it('should validate variadic argument', () => {
|
|
50
|
-
const
|
|
49
|
+
const run = new CommandBuilder('t', (c) => {
|
|
51
50
|
c.option('--i <ints...>', (o) => o.parser.integer().validator.isIntegerArray())
|
|
52
51
|
c.option('--s <str>', (o) => o.validator.isString())
|
|
53
52
|
c.action(() => {})
|
|
54
53
|
})
|
|
55
|
-
expect(() =>
|
|
54
|
+
expect(() => run.$.parse('--i 3 4 5 --s abc'.split(' '), { from: 'user' })).not.toThrow()
|
|
56
55
|
})
|
|
57
56
|
|
|
58
57
|
it('should throw if invalid', () => {
|
|
59
|
-
const run =
|
|
58
|
+
const run = new CommandBuilder('t', (c) => {
|
|
60
59
|
c.option('--i <ints>', (a) => a.parser.delimitedIntegers().validator.isIntegerArray())
|
|
61
60
|
c.action(() => {})
|
|
62
61
|
c.throwInsteadOfProcessExit()
|
|
63
62
|
OutputManager.getInstance().stderr.disable()
|
|
64
63
|
})
|
|
65
|
-
expect(() => run
|
|
64
|
+
expect(() => run.$.parse(['--i 3,q,7'], { from: 'user' })).toThrow()
|
|
66
65
|
})
|
|
67
66
|
|
|
68
67
|
it('should validate with custom validator', () => {
|
|
69
|
-
const run =
|
|
68
|
+
const run = new CommandBuilder('t', (c) => {
|
|
70
69
|
c.option('--a <arg>', (a) => a.validator.custom((arg) => arg === 'abc'))
|
|
71
70
|
c.action(() => {})
|
|
72
71
|
c.throwInsteadOfProcessExit()
|
|
73
72
|
})
|
|
74
|
-
expect(() => run
|
|
73
|
+
expect(() => run.$.parse('--a ab'.split(' '), { from: 'user' })).toThrow()
|
|
75
74
|
})
|
|
76
75
|
|
|
77
76
|
it('should validate with multiple validators', () => {
|
|
78
|
-
const run =
|
|
77
|
+
const run = new CommandBuilder('t', (c) => {
|
|
79
78
|
c.option('--a <arg>', (a) => {
|
|
80
79
|
a.validator.custom((arg: string) => arg !== 'abc')
|
|
81
80
|
a.validator.custom((arg: string) => arg.length >= 3)
|
|
@@ -83,8 +82,8 @@ describe(OptionArgumentValidatorSelector.name, () => {
|
|
|
83
82
|
c.action(() => {})
|
|
84
83
|
c.throwInsteadOfProcessExit()
|
|
85
84
|
})
|
|
86
|
-
expect(() => run
|
|
87
|
-
expect(() => run
|
|
88
|
-
expect(() => run
|
|
85
|
+
expect(() => run.$.parse('--a ab'.split(' '), { from: 'user' })).toThrow()
|
|
86
|
+
expect(() => run.$.parse('--a abc'.split(' '), { from: 'user' })).toThrow()
|
|
87
|
+
expect(() => run.$.parse('--a abcd'.split(' '), { from: 'user' })).not.toThrow()
|
|
89
88
|
})
|
|
90
89
|
})
|
|
@@ -6,17 +6,17 @@ describe('arrSome', () => {
|
|
|
6
6
|
expect(
|
|
7
7
|
arrSome(arr, (element: any) => {
|
|
8
8
|
return element.length > 5
|
|
9
|
-
})
|
|
9
|
+
})
|
|
10
10
|
).toBe(false)
|
|
11
11
|
expect(
|
|
12
12
|
arrSome(arr, (element: any) => {
|
|
13
13
|
return element.length === 5
|
|
14
|
-
})
|
|
14
|
+
})
|
|
15
15
|
).toBe(true)
|
|
16
16
|
expect(
|
|
17
17
|
arrSome([], (element: any) => {
|
|
18
18
|
return element.length === 5
|
|
19
|
-
})
|
|
19
|
+
})
|
|
20
20
|
).toBe(false)
|
|
21
21
|
})
|
|
22
22
|
|
package/dist/cjs/cmd/CLI.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CLI = void 0;
|
|
4
|
-
const CommandBuilder_1 = require("./CommandBuilder");
|
|
5
|
-
function CLI(name, callback) {
|
|
6
|
-
return () => new CommandBuilder_1.CommandBuilder(name, callback).commander;
|
|
7
|
-
}
|
|
8
|
-
exports.CLI = CLI;
|
|
9
|
-
/*
|
|
10
|
-
|
|
11
|
-
TODO: should return a class that offers:
|
|
12
|
-
- command
|
|
13
|
-
- main
|
|
14
|
-
- action
|
|
15
|
-
*/
|
|
16
|
-
//# sourceMappingURL=CLI.js.map
|
package/dist/cjs/cmd/CLI.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CLI.js","sourceRoot":"","sources":["../../../src/cmd/CLI.ts"],"names":[],"mappings":";;;AAAA,qDAAiD;AAEjD,SAAgB,GAAG,CAAC,IAAY,EAAE,QAA6D;IAC7F,OAAO,GAAG,EAAE,CAAC,IAAI,+BAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,SAAS,CAAA;AAC3D,CAAC;AAFD,kBAEC;AAED;;;;;;EAME"}
|