cli-kiss 0.2.3 → 0.2.5

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.
@@ -0,0 +1,50 @@
1
+ import { expect, it } from "@jest/globals";
2
+ import { ReaderArgs } from "../src";
3
+
4
+ it("run", async function () {
5
+ const readerArgs1 = new ReaderArgs(["--val=1", "A", "B", "STOP", "C"]);
6
+ const kOptionVariadicStop = readerArgs1.registerOption({
7
+ longs: ["val"],
8
+ shorts: [],
9
+ parsing: {
10
+ consumeShortGroup: true,
11
+ consumeNextArg: (_inlined, separated, nextArg) =>
12
+ nextArg !== undefined && separated[separated.length - 1] !== "STOP",
13
+ },
14
+ });
15
+ expect(readerArgs1.consumePositional()).toStrictEqual("C");
16
+ expect(readerArgs1.consumePositional()).toStrictEqual(undefined);
17
+ expect(readerArgs1.getOptionValues(kOptionVariadicStop)).toStrictEqual([
18
+ { inlined: "1", separated: ["A", "B", "STOP"] },
19
+ ]);
20
+
21
+ const readerArgs2 = new ReaderArgs(["--val=1", "A", "B", "C"]);
22
+ const kOptionVariadicFull = readerArgs2.registerOption({
23
+ longs: ["val"],
24
+ shorts: [],
25
+ parsing: {
26
+ consumeShortGroup: true,
27
+ consumeNextArg: (_inlined, _separated, nextArg) => nextArg !== undefined,
28
+ },
29
+ });
30
+ expect(readerArgs2.consumePositional()).toStrictEqual(undefined);
31
+ expect(readerArgs2.getOptionValues(kOptionVariadicFull)).toStrictEqual([
32
+ { inlined: "1", separated: ["A", "B", "C"] },
33
+ ]);
34
+
35
+ const readerArgs3 = new ReaderArgs(["--val=2", "A", "B", "--val=1", "C"]);
36
+ const kOptionVariadicKeyed = readerArgs3.registerOption({
37
+ longs: ["val"],
38
+ shorts: [],
39
+ parsing: {
40
+ consumeShortGroup: true,
41
+ consumeNextArg: (inlined, separated) =>
42
+ separated.length < Number(inlined ?? "0"),
43
+ },
44
+ });
45
+ expect(readerArgs3.consumePositional()).toStrictEqual(undefined);
46
+ expect(readerArgs3.getOptionValues(kOptionVariadicKeyed)).toStrictEqual([
47
+ { inlined: "2", separated: ["A", "B"] },
48
+ { inlined: "1", separated: ["C"] },
49
+ ]);
50
+ });
@@ -1,7 +1,7 @@
1
1
  import { expect, it } from "@jest/globals";
2
- import { ReaderArgs } from "../src";
2
+ import { ReaderArgs, ReaderOptionParsing } from "../src";
3
3
 
4
- it("run", async () => {
4
+ it("run", async function () {
5
5
  const stream = new ReaderArgs([
6
6
  "positional-0",
7
7
  "-aasof-normal",
@@ -12,47 +12,48 @@ it("run", async () => {
12
12
  "1.1",
13
13
  "-eesov-split",
14
14
  "1.2",
15
- "-ffsov-join=2",
15
+ "-ffsov-join=2.1",
16
+ "-ggsov-join=2.2",
16
17
  "positional-2",
17
18
  ]);
18
19
 
19
20
  expect(stream.consumePositional()).toStrictEqual("positional-0");
20
21
 
22
+ const kSofUnset = stream.registerOption({
23
+ longs: [],
24
+ shorts: ["sof-unset"],
25
+ parsing: optionFlagParsing,
26
+ });
21
27
  const kSofNormal = stream.registerOption({
22
28
  shorts: ["sof-normal"],
23
29
  longs: [],
24
- valued: false,
30
+ parsing: optionFlagParsing,
25
31
  });
26
32
  const kSofPositive = stream.registerOption({
27
33
  longs: [],
28
34
  shorts: ["sof-positive"],
29
- valued: false,
35
+ parsing: optionFlagParsing,
30
36
  });
31
37
  const kSofNegative = stream.registerOption({
32
38
  longs: [],
33
39
  shorts: ["sof-negative"],
34
- valued: false,
35
- });
36
- const kSofUnset = stream.registerOption({
37
- longs: [],
38
- shorts: ["sof-unset"],
39
- valued: false,
40
+ parsing: optionFlagParsing,
40
41
  });
41
42
 
42
43
  const kAa = stream.registerOption({
43
44
  longs: [],
44
45
  shorts: ["aa"],
45
- valued: false,
46
+ parsing: optionFlagParsing,
46
47
  });
47
48
  const kBb = stream.registerOption({
48
49
  longs: [],
49
50
  shorts: ["bb"],
50
- valued: false,
51
+ parsing: optionFlagParsing,
51
52
  });
52
53
  const kCc = stream.registerOption({
53
54
  longs: [],
54
55
  shorts: ["cc"],
55
- valued: false,
56
+ parsing: optionFlagParsing,
56
57
  });
57
58
 
58
59
  expect(stream.consumePositional()).toStrictEqual("positional-1");
@@ -60,51 +61,94 @@ it("run", async () => {
60
61
  const kSovSplit = stream.registerOption({
61
62
  longs: [],
62
63
  shorts: ["sov-split"],
63
- valued: true,
64
+ parsing: optionValueFixedUniqueParsing,
64
65
  });
65
66
  const kSovJoin = stream.registerOption({
66
67
  longs: [],
67
68
  shorts: ["sov-join"],
68
- valued: true,
69
+ parsing: optionValueFixedUniqueParsing,
69
70
  });
70
71
  const kSovUnset = stream.registerOption({
71
72
  longs: [],
72
73
  shorts: ["sov-unset"],
73
- valued: true,
74
+ parsing: optionValueFixedUniqueParsing,
74
75
  });
75
76
 
76
77
  const kDd = stream.registerOption({
77
78
  longs: [],
78
79
  shorts: ["dd"],
79
- valued: false,
80
+ parsing: optionFlagParsing,
80
81
  });
81
82
  const kEe = stream.registerOption({
82
83
  longs: [],
83
84
  shorts: ["ee"],
84
- valued: false,
85
+ parsing: optionFlagParsing,
85
86
  });
86
87
  const kFf = stream.registerOption({
87
88
  longs: [],
88
89
  shorts: ["ff"],
89
- valued: false,
90
+ parsing: optionFlagParsing,
91
+ });
92
+ const kGg = stream.registerOption({
93
+ longs: [],
94
+ shorts: ["gg"],
95
+ parsing: optionFlagParsing,
90
96
  });
91
97
 
92
98
  expect(stream.consumePositional()).toStrictEqual("positional-2");
93
99
 
94
- expect(stream.getOptionValues(kSofNormal)).toStrictEqual(["true"]);
95
- expect(stream.getOptionValues(kSofPositive)).toStrictEqual(["true"]);
96
- expect(stream.getOptionValues(kSofNegative)).toStrictEqual(["false"]);
97
100
  expect(stream.getOptionValues(kSofUnset)).toStrictEqual([]);
101
+ expect(stream.getOptionValues(kSofNormal)).toStrictEqual([
102
+ { inlined: null, separated: [] },
103
+ ]);
104
+ expect(stream.getOptionValues(kSofPositive)).toStrictEqual([
105
+ { inlined: "true", separated: [] },
106
+ ]);
107
+ expect(stream.getOptionValues(kSofNegative)).toStrictEqual([
108
+ { inlined: "false", separated: [] },
109
+ ]);
98
110
 
99
- expect(stream.getOptionValues(kAa)).toStrictEqual(["true"]);
100
- expect(stream.getOptionValues(kBb)).toStrictEqual(["true"]);
101
- expect(stream.getOptionValues(kCc)).toStrictEqual(["true"]);
111
+ expect(stream.getOptionValues(kAa)).toStrictEqual([
112
+ { inlined: null, separated: [] },
113
+ ]);
114
+ expect(stream.getOptionValues(kBb)).toStrictEqual([
115
+ { inlined: null, separated: [] },
116
+ ]);
117
+ expect(stream.getOptionValues(kCc)).toStrictEqual([
118
+ { inlined: null, separated: [] },
119
+ ]);
102
120
 
103
121
  expect(stream.getOptionValues(kSovUnset)).toStrictEqual([]);
104
- expect(stream.getOptionValues(kSovSplit)).toStrictEqual(["1.1", "1.2"]);
105
- expect(stream.getOptionValues(kSovJoin)).toStrictEqual(["2"]);
122
+ expect(stream.getOptionValues(kSovSplit)).toStrictEqual([
123
+ { inlined: null, separated: ["1.1"] },
124
+ { inlined: null, separated: ["1.2"] },
125
+ ]);
126
+ expect(stream.getOptionValues(kSovJoin)).toStrictEqual([
127
+ { inlined: "2.1", separated: [] },
128
+ { inlined: "2.2", separated: [] },
129
+ ]);
106
130
 
107
- expect(stream.getOptionValues(kDd)).toStrictEqual(["true"]);
108
- expect(stream.getOptionValues(kEe)).toStrictEqual(["true"]);
109
- expect(stream.getOptionValues(kFf)).toStrictEqual(["true"]);
131
+ expect(stream.getOptionValues(kDd)).toStrictEqual([
132
+ { inlined: null, separated: [] },
133
+ ]);
134
+ expect(stream.getOptionValues(kEe)).toStrictEqual([
135
+ { inlined: null, separated: [] },
136
+ ]);
137
+ expect(stream.getOptionValues(kFf)).toStrictEqual([
138
+ { inlined: null, separated: [] },
139
+ ]);
140
+ expect(stream.getOptionValues(kGg)).toStrictEqual([
141
+ { inlined: null, separated: [] },
142
+ ]);
110
143
  });
144
+
145
+ const optionFlagParsing: ReaderOptionParsing = {
146
+ consumeShortGroup: false,
147
+ consumeNextArg: () => false,
148
+ };
149
+
150
+ const optionValueFixedUniqueParsing: ReaderOptionParsing = {
151
+ consumeShortGroup: true,
152
+ consumeNextArg: (inlined, separated) =>
153
+ inlined === null && separated.length === 0,
154
+ };
@@ -12,19 +12,30 @@ import {
12
12
  positionalRequired,
13
13
  positionalVariadics,
14
14
  ReaderArgs,
15
+ type,
15
16
  typeList,
16
17
  typeNumber,
17
- typeString,
18
18
  } from "../src";
19
19
 
20
20
  const rootCommand = commandChained(
21
21
  { description: "?" },
22
22
  operation(
23
23
  {
24
- options: { flag: optionFlag({ short: "b", long: "boolean-flag" }) },
25
- positionals: [positionalRequired({ label: "POS-1", type: typeNumber })],
24
+ options: {
25
+ flagPositive: optionFlag({
26
+ short: "fp",
27
+ long: "flag-positive",
28
+ default: true,
29
+ }),
30
+ flagNegative: optionFlag({
31
+ short: "fn",
32
+ long: "flag-negative",
33
+ default: false,
34
+ }),
35
+ },
36
+ positionals: [positionalRequired({ type: typeNumber() })],
26
37
  },
27
- async (context, inputs) => {
38
+ async function (context, inputs) {
28
39
  return { at: "root", context, inputs };
29
40
  },
30
41
  ),
@@ -35,17 +46,17 @@ const rootCommand = commandChained(
35
46
  options: {
36
47
  string: optionSingleValue({
37
48
  long: "string-option",
38
- type: typeString,
39
- default: () => undefined,
49
+ type: type(),
50
+ defaultWhenNotDefined: () => undefined,
40
51
  }),
41
52
  number: optionRepeatable({
42
53
  long: "number-option",
43
- type: typeList(typeNumber),
54
+ type: typeList(typeNumber()),
44
55
  }),
45
56
  },
46
- positionals: [positionalRequired({ type: typeNumber })],
57
+ positionals: [positionalRequired({ type: typeNumber() })],
47
58
  },
48
- async (context, inputs) => {
59
+ async function (context, inputs) {
49
60
  return { at: "mid", context, inputs };
50
61
  },
51
62
  ),
@@ -55,9 +66,9 @@ const rootCommand = commandChained(
55
66
  operation(
56
67
  {
57
68
  options: {},
58
- positionals: [positionalRequired({ type: typeString })],
69
+ positionals: [positionalRequired({ type: type() })],
59
70
  },
60
- async (context, inputs) => {
71
+ async function (context, inputs) {
61
72
  return { at: "sub1", context, inputs };
62
73
  },
63
74
  ),
@@ -68,12 +79,12 @@ const rootCommand = commandChained(
68
79
  {
69
80
  options: {},
70
81
  positionals: [
71
- positionalRequired({ type: typeNumber }),
72
- positionalOptional({ type: typeString, default: () => "42" }),
73
- positionalVariadics({ type: typeString }),
82
+ positionalRequired({ type: typeNumber() }),
83
+ positionalOptional({ type: type(), default: () => "42" }),
84
+ positionalVariadics({ type: type() }),
74
85
  ],
75
86
  },
76
- async (context, inputs) => {
87
+ async function (context, inputs) {
77
88
  return { at: "sub2", context, inputs };
78
89
  },
79
90
  ),
@@ -82,13 +93,14 @@ const rootCommand = commandChained(
82
93
  ),
83
94
  );
84
95
 
85
- it("run", async () => {
86
- const res1 = await executeInterpreted(
87
- ["50", "51", "sub1", "final"],
88
- "Run Context Input",
89
- rootCommand,
90
- );
91
- expect(res1).toStrictEqual({
96
+ it("run", async function () {
97
+ expect(
98
+ await executeInterpreted(
99
+ ["-fn=TRUE", "-fp", "50", "51", "sub1", "final"],
100
+ "Run Context Input",
101
+ rootCommand,
102
+ ),
103
+ ).toStrictEqual({
92
104
  at: "sub1",
93
105
  context: {
94
106
  at: "mid",
@@ -96,7 +108,7 @@ it("run", async () => {
96
108
  at: "root",
97
109
  context: "Run Context Input",
98
110
  inputs: {
99
- options: { flag: false },
111
+ options: { flagPositive: true, flagNegative: true },
100
112
  positionals: [50],
101
113
  },
102
114
  },
@@ -111,25 +123,56 @@ it("run", async () => {
111
123
  },
112
124
  });
113
125
 
114
- const res2 = await executeInterpreted(
115
- [
116
- "40",
117
- "41",
118
- "sub2",
119
- "--string-option=hello",
120
- "--number-option",
121
- "123.1,123.2",
122
- "--number-option",
123
- "123.3",
124
- "88.88",
125
- "a,b",
126
- "final",
127
- "--boolean-flag",
128
- ],
129
- "Run Context Input",
130
- rootCommand,
131
- );
132
- expect(res2).toStrictEqual({
126
+ expect(
127
+ await executeInterpreted(
128
+ ["50", "51", "sub2", "9999.99"],
129
+ "Run Context Input",
130
+ rootCommand,
131
+ ),
132
+ ).toStrictEqual({
133
+ at: "sub2",
134
+ context: {
135
+ at: "mid",
136
+ context: {
137
+ at: "root",
138
+ context: "Run Context Input",
139
+ inputs: {
140
+ options: { flagPositive: true, flagNegative: false },
141
+ positionals: [50],
142
+ },
143
+ },
144
+ inputs: {
145
+ options: { string: undefined, number: [] },
146
+ positionals: [51],
147
+ },
148
+ },
149
+ inputs: {
150
+ options: {},
151
+ positionals: [9999.99, "42", []],
152
+ },
153
+ });
154
+
155
+ expect(
156
+ await executeInterpreted(
157
+ [
158
+ "40",
159
+ "41",
160
+ "sub2",
161
+ "--string-option=hello",
162
+ "--number-option",
163
+ "123.1,123.2",
164
+ "--number-option",
165
+ "123.3",
166
+ "88.88",
167
+ "a,b",
168
+ "final",
169
+ "--flag-positive=NO",
170
+ "--flag-negative=false",
171
+ ],
172
+ "Run Context Input",
173
+ rootCommand,
174
+ ),
175
+ ).toStrictEqual({
133
176
  at: "sub2",
134
177
  context: {
135
178
  at: "mid",
@@ -137,7 +180,7 @@ it("run", async () => {
137
180
  at: "root",
138
181
  context: "Run Context Input",
139
182
  inputs: {
140
- options: { flag: true },
183
+ options: { flagPositive: false, flagNegative: false },
141
184
  positionals: [40],
142
185
  },
143
186
  },