burgee 0.0.0 → 0.2.0

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.
Files changed (114) hide show
  1. package/README.md +28 -1
  2. package/dist/agent.d.ts +19 -0
  3. package/dist/agent.js +25 -0
  4. package/dist/brand.d.ts +186 -0
  5. package/dist/brand.js +232 -0
  6. package/dist/cli.d.ts +51 -0
  7. package/dist/cli.js +105 -0
  8. package/dist/commander-argument.d.ts +22 -0
  9. package/dist/commander-argument.js +72 -0
  10. package/dist/commander-command.d.ts +345 -0
  11. package/dist/commander-command.js +1605 -0
  12. package/dist/commander-error.d.ts +10 -0
  13. package/dist/commander-error.js +20 -0
  14. package/dist/commander-help.d.ts +67 -0
  15. package/dist/commander-help.js +319 -0
  16. package/dist/commander-option.d.ts +58 -0
  17. package/dist/commander-option.js +164 -0
  18. package/dist/commander-suggest.d.ts +2 -0
  19. package/dist/commander-suggest.js +59 -0
  20. package/dist/commander.d.ts +18 -0
  21. package/dist/commander.js +12 -0
  22. package/dist/completions.d.ts +39 -0
  23. package/dist/completions.js +224 -0
  24. package/dist/config.d.ts +28 -0
  25. package/dist/config.js +98 -0
  26. package/dist/contrast.d.ts +70 -0
  27. package/dist/contrast.js +93 -0
  28. package/dist/execute.d.ts +97 -0
  29. package/dist/execute.js +433 -0
  30. package/dist/exit-code.d.ts +18 -0
  31. package/dist/exit-code.js +12 -0
  32. package/dist/help.d.ts +22 -0
  33. package/dist/help.js +158 -0
  34. package/dist/index.d.ts +16 -1
  35. package/dist/index.js +9 -2
  36. package/dist/manifest.d.ts +192 -0
  37. package/dist/manifest.js +55 -0
  38. package/dist/mcp.d.ts +40 -0
  39. package/dist/mcp.js +111 -0
  40. package/dist/names.d.ts +5 -0
  41. package/dist/names.js +6 -0
  42. package/dist/pkg.d.ts +5 -0
  43. package/dist/pkg.js +21 -0
  44. package/dist/precedence.d.ts +55 -0
  45. package/dist/precedence.js +100 -0
  46. package/dist/runtime.d.ts +27 -0
  47. package/dist/runtime.js +17 -0
  48. package/dist/schema.d.ts +71 -0
  49. package/dist/schema.js +108 -0
  50. package/dist/testing-helpers.d.ts +62 -0
  51. package/dist/testing-helpers.js +110 -0
  52. package/dist/testing.d.ts +10 -0
  53. package/dist/testing.js +3 -0
  54. package/dist/validate.d.ts +27 -0
  55. package/dist/validate.js +133 -0
  56. package/dist/yargs-burgee.d.ts +50 -0
  57. package/dist/yargs-burgee.js +104 -0
  58. package/dist/yargs-cliui.d.ts +56 -0
  59. package/dist/yargs-cliui.js +421 -0
  60. package/dist/yargs-command.d.ts +82 -0
  61. package/dist/yargs-command.js +414 -0
  62. package/dist/yargs-completion.d.ts +41 -0
  63. package/dist/yargs-completion.js +271 -0
  64. package/dist/yargs-factory.d.ts +193 -0
  65. package/dist/yargs-factory.js +1606 -0
  66. package/dist/yargs-helpers.d.ts +6 -0
  67. package/dist/yargs-helpers.js +2 -0
  68. package/dist/yargs-middleware.d.ts +32 -0
  69. package/dist/yargs-middleware.js +81 -0
  70. package/dist/yargs-parser.d.ts +41 -0
  71. package/dist/yargs-parser.js +929 -0
  72. package/dist/yargs-shim.d.ts +54 -0
  73. package/dist/yargs-shim.js +84 -0
  74. package/dist/yargs-usage.d.ts +42 -0
  75. package/dist/yargs-usage.js +479 -0
  76. package/dist/yargs-utils.d.ts +33 -0
  77. package/dist/yargs-utils.js +209 -0
  78. package/dist/yargs-validation.d.ts +26 -0
  79. package/dist/yargs-validation.js +261 -0
  80. package/dist/yargs-y18n.d.ts +21 -0
  81. package/dist/yargs-y18n.js +117 -0
  82. package/dist/yargs.d.ts +6 -0
  83. package/dist/yargs.js +8 -0
  84. package/locales/be.json +46 -0
  85. package/locales/cs.json +51 -0
  86. package/locales/de.json +46 -0
  87. package/locales/en.json +55 -0
  88. package/locales/es.json +46 -0
  89. package/locales/fi.json +49 -0
  90. package/locales/fr.json +53 -0
  91. package/locales/he.json +55 -0
  92. package/locales/hi.json +49 -0
  93. package/locales/hu.json +46 -0
  94. package/locales/id.json +50 -0
  95. package/locales/it.json +46 -0
  96. package/locales/ja.json +51 -0
  97. package/locales/ka.json +55 -0
  98. package/locales/ko.json +49 -0
  99. package/locales/nb.json +44 -0
  100. package/locales/nl.json +49 -0
  101. package/locales/nn.json +44 -0
  102. package/locales/pirate.json +13 -0
  103. package/locales/pl.json +49 -0
  104. package/locales/pt.json +45 -0
  105. package/locales/pt_BR.json +48 -0
  106. package/locales/ru.json +51 -0
  107. package/locales/th.json +46 -0
  108. package/locales/tr.json +48 -0
  109. package/locales/uk_UA.json +51 -0
  110. package/locales/uz.json +52 -0
  111. package/locales/zh_CN.json +48 -0
  112. package/locales/zh_TW.json +51 -0
  113. package/package.json +61 -8
  114. package/dist/index.js.map +0 -1
@@ -0,0 +1,10 @@
1
+ /** commander's error classes, byte-for-byte in shape: `code`, `exitCode`, `nestedError`. */
2
+ export declare class CommanderError extends Error {
3
+ code: string;
4
+ exitCode: number;
5
+ nestedError: unknown;
6
+ constructor(exitCode: number, code: string, message?: string);
7
+ }
8
+ export declare class InvalidArgumentError extends CommanderError {
9
+ constructor(message?: string);
10
+ }
@@ -0,0 +1,20 @@
1
+ export class CommanderError extends Error {
2
+ code;
3
+ exitCode;
4
+ nestedError;
5
+ constructor(exitCode, code, message) {
6
+ super(message);
7
+ Error.captureStackTrace(this, this.constructor);
8
+ this.name = this.constructor.name;
9
+ this.code = code;
10
+ this.exitCode = exitCode;
11
+ this.nestedError = undefined;
12
+ }
13
+ }
14
+ export class InvalidArgumentError extends CommanderError {
15
+ constructor(message) {
16
+ super(1, 'commander.invalidArgument', message);
17
+ Error.captureStackTrace(this, this.constructor);
18
+ this.name = this.constructor.name;
19
+ }
20
+ }
@@ -0,0 +1,67 @@
1
+ import { type Argument } from './commander-argument.js';
2
+ import type { Command } from './commander-command.js';
3
+ import type { Option } from './commander-option.js';
4
+ export interface HelpContext {
5
+ error?: boolean;
6
+ helpWidth?: number;
7
+ outputHasColors?: boolean;
8
+ }
9
+ /** Methods are static in style so a subclass, or plain functions via `configureHelp`, can override them. */
10
+ export declare class Help {
11
+ helpWidth: number | undefined;
12
+ minWidthToWrap: number;
13
+ sortSubcommands: boolean;
14
+ sortOptions: boolean;
15
+ showGlobalOptions: boolean;
16
+ /** Called after `configureHelp` overrides are applied and just before `formatHelp`. */
17
+ prepareContext(contextOptions: HelpContext): void;
18
+ visibleCommands(cmd: Command): Command[];
19
+ compareOptions(a: Option, b: Option): number;
20
+ visibleOptions(cmd: Command): Option[];
21
+ visibleGlobalOptions(cmd: Command): Option[];
22
+ visibleArguments(cmd: Command): Argument[];
23
+ subcommandTerm(cmd: Command): string;
24
+ optionTerm(option: Option): string;
25
+ argumentTerm(argument: Argument): string;
26
+ longestSubcommandTermLength(cmd: Command, helper: Help): number;
27
+ longestOptionTermLength(cmd: Command, helper: Help): number;
28
+ longestGlobalOptionTermLength(cmd: Command, helper: Help): number;
29
+ longestArgumentTermLength(cmd: Command, helper: Help): number;
30
+ commandUsage(cmd: Command): string;
31
+ commandDescription(cmd: Command): string;
32
+ /** Summary, falling back to description for backwards compatibility. */
33
+ subcommandDescription(cmd: Command): string;
34
+ optionDescription(option: Option): string;
35
+ argumentDescription(argument: Argument): string;
36
+ formatItemList(heading: string, items: string[], helper: Help): string[];
37
+ /** Groups in order of first appearance among all items; members in order of the visible items. */
38
+ groupItems<T>(unsortedItems: T[], visibleItems: T[], getGroup: (item: T) => string): Map<string, T[]>;
39
+ formatHelp(cmd: Command, helper: Help): string;
40
+ /** Width ignoring ANSI escapes, for padding and wrapping. */
41
+ displayWidth(str: string): number;
42
+ styleTitle(str: string): string;
43
+ styleUsage(str: string): string;
44
+ styleCommandDescription(str: string): string;
45
+ styleOptionDescription(str: string): string;
46
+ styleSubcommandDescription(str: string): string;
47
+ styleArgumentDescription(str: string): string;
48
+ styleDescriptionText(str: string): string;
49
+ styleOptionTerm(str: string): string;
50
+ styleSubcommandTerm(str: string): string;
51
+ styleArgumentTerm(str: string): string;
52
+ styleOptionText(str: string): string;
53
+ styleArgumentText(str: string): string;
54
+ styleSubcommandText(str: string): string;
55
+ styleCommandText(str: string): string;
56
+ padWidth(cmd: Command, helper: Help): number;
57
+ /** Manually wrapped text: a line break followed by whitespace. */
58
+ preformatted(str: string): boolean;
59
+ /**
60
+ * Pad the term and wrap the description, indenting the following lines:
61
+ * TTT DDD DDDD
62
+ * DD DDD
63
+ */
64
+ formatItem(term: string, termWidth: number, description: string, helper: Help): string;
65
+ /** Wrap at whitespace, preserving existing line breaks; skipped below `minWidthToWrap`. */
66
+ boxWrap(str: string, width: number): string;
67
+ }
@@ -0,0 +1,319 @@
1
+ import { stripVTControlCharacters } from 'node:util';
2
+ import { humanReadableArgName } from './commander-argument.js';
3
+ export class Help {
4
+ helpWidth = undefined;
5
+ minWidthToWrap = 40;
6
+ sortSubcommands = false;
7
+ sortOptions = false;
8
+ showGlobalOptions = false;
9
+ prepareContext(contextOptions) {
10
+ this.helpWidth = this.helpWidth ?? contextOptions.helpWidth ?? 80;
11
+ }
12
+ visibleCommands(cmd) {
13
+ const visibleCommands = cmd.commands.filter((sub) => !sub._hidden);
14
+ const helpCommand = cmd._getHelpCommand();
15
+ if (helpCommand && !helpCommand._hidden)
16
+ visibleCommands.push(helpCommand);
17
+ if (this.sortSubcommands)
18
+ visibleCommands.sort((a, b) => a.name().localeCompare(b.name()));
19
+ return visibleCommands;
20
+ }
21
+ compareOptions(a, b) {
22
+ const getSortKey = (option) => option.short ? option.short.replace(/^-/, '') : (option.long ?? '').replace(/^--/, '');
23
+ return getSortKey(a).localeCompare(getSortKey(b));
24
+ }
25
+ visibleOptions(cmd) {
26
+ const visibleOptions = cmd.options.filter((option) => !option.hidden);
27
+ const helpOption = cmd._getHelpOption();
28
+ if (helpOption && !helpOption.hidden) {
29
+ const removeShort = helpOption.short && cmd._findOption(helpOption.short);
30
+ const removeLong = helpOption.long && cmd._findOption(helpOption.long);
31
+ if (!removeShort && !removeLong)
32
+ visibleOptions.push(helpOption);
33
+ else if (helpOption.long && !removeLong)
34
+ visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
35
+ else if (helpOption.short && !removeShort)
36
+ visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
37
+ }
38
+ if (this.sortOptions)
39
+ visibleOptions.sort(this.compareOptions);
40
+ return visibleOptions;
41
+ }
42
+ visibleGlobalOptions(cmd) {
43
+ if (!this.showGlobalOptions)
44
+ return [];
45
+ const globalOptions = [];
46
+ for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
47
+ globalOptions.push(...ancestorCmd.options.filter((option) => !option.hidden));
48
+ }
49
+ if (this.sortOptions)
50
+ globalOptions.sort(this.compareOptions);
51
+ return globalOptions;
52
+ }
53
+ visibleArguments(cmd) {
54
+ if (cmd._argsDescription) {
55
+ for (const argument of cmd.registeredArguments) {
56
+ argument.description = argument.description || cmd._argsDescription[argument.name()] || '';
57
+ }
58
+ }
59
+ if (cmd.registeredArguments.find((argument) => argument.description))
60
+ return cmd.registeredArguments;
61
+ return [];
62
+ }
63
+ subcommandTerm(cmd) {
64
+ const args = cmd.registeredArguments.map((arg) => humanReadableArgName(arg)).join(' ');
65
+ return (cmd._name +
66
+ (cmd._aliases[0] ? `|${cmd._aliases[0]}` : '') +
67
+ (cmd.options.length ? ' [options]' : '') +
68
+ (args ? ` ${args}` : ''));
69
+ }
70
+ optionTerm(option) {
71
+ return option.flags;
72
+ }
73
+ argumentTerm(argument) {
74
+ return argument.name();
75
+ }
76
+ longestSubcommandTermLength(cmd, helper) {
77
+ return helper
78
+ .visibleCommands(cmd)
79
+ .reduce((max, command) => Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command)))), 0);
80
+ }
81
+ longestOptionTermLength(cmd, helper) {
82
+ return helper
83
+ .visibleOptions(cmd)
84
+ .reduce((max, option) => Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option)))), 0);
85
+ }
86
+ longestGlobalOptionTermLength(cmd, helper) {
87
+ return helper
88
+ .visibleGlobalOptions(cmd)
89
+ .reduce((max, option) => Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option)))), 0);
90
+ }
91
+ longestArgumentTermLength(cmd, helper) {
92
+ return helper
93
+ .visibleArguments(cmd)
94
+ .reduce((max, argument) => Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument)))), 0);
95
+ }
96
+ commandUsage(cmd) {
97
+ let cmdName = cmd._name;
98
+ if (cmd._aliases[0])
99
+ cmdName = `${cmdName}|${cmd._aliases[0]}`;
100
+ let ancestorCmdNames = '';
101
+ for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
102
+ ancestorCmdNames = `${ancestorCmd.name()} ${ancestorCmdNames}`;
103
+ }
104
+ return `${ancestorCmdNames}${cmdName} ${cmd.usage()}`;
105
+ }
106
+ commandDescription(cmd) {
107
+ return cmd.description();
108
+ }
109
+ subcommandDescription(cmd) {
110
+ return cmd.summary() || cmd.description();
111
+ }
112
+ optionDescription(option) {
113
+ const extraInfo = [];
114
+ if (option.argChoices) {
115
+ extraInfo.push(`choices: ${option.argChoices.map((choice) => JSON.stringify(choice)).join(', ')}`);
116
+ }
117
+ if (option.defaultValue !== undefined) {
118
+ const showDefault = option.required || option.optional || (option.isBoolean() && typeof option.defaultValue === 'boolean');
119
+ if (showDefault)
120
+ extraInfo.push(`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue)}`);
121
+ }
122
+ if (option.presetArg !== undefined && option.optional)
123
+ extraInfo.push(`preset: ${JSON.stringify(option.presetArg)}`);
124
+ if (option.envVar !== undefined)
125
+ extraInfo.push(`env: ${option.envVar}`);
126
+ if (extraInfo.length > 0) {
127
+ const extraDescription = `(${extraInfo.join(', ')})`;
128
+ return option.description ? `${option.description} ${extraDescription}` : extraDescription;
129
+ }
130
+ return option.description;
131
+ }
132
+ argumentDescription(argument) {
133
+ const extraInfo = [];
134
+ if (argument.argChoices) {
135
+ extraInfo.push(`choices: ${argument.argChoices.map((choice) => JSON.stringify(choice)).join(', ')}`);
136
+ }
137
+ if (argument.defaultValue !== undefined) {
138
+ extraInfo.push(`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue)}`);
139
+ }
140
+ if (extraInfo.length > 0) {
141
+ const extraDescription = `(${extraInfo.join(', ')})`;
142
+ return argument.description ? `${argument.description} ${extraDescription}` : extraDescription;
143
+ }
144
+ return argument.description;
145
+ }
146
+ formatItemList(heading, items, helper) {
147
+ if (items.length === 0)
148
+ return [];
149
+ return [helper.styleTitle(heading), ...items, ''];
150
+ }
151
+ groupItems(unsortedItems, visibleItems, getGroup) {
152
+ const result = new Map();
153
+ for (const item of unsortedItems) {
154
+ const group = getGroup(item);
155
+ if (!result.has(group))
156
+ result.set(group, []);
157
+ }
158
+ for (const item of visibleItems) {
159
+ const group = getGroup(item);
160
+ const members = result.get(group);
161
+ if (members === undefined)
162
+ result.set(group, [item]);
163
+ else
164
+ members.push(item);
165
+ }
166
+ return result;
167
+ }
168
+ formatHelp(cmd, helper) {
169
+ const termWidth = helper.padWidth(cmd, helper);
170
+ const helpWidth = helper.helpWidth ?? 80;
171
+ const callFormatItem = (term, description) => helper.formatItem(term, termWidth, description, helper);
172
+ let output = [`${helper.styleTitle('Usage:')} ${helper.styleUsage(helper.commandUsage(cmd))}`, ''];
173
+ const commandDescription = helper.commandDescription(cmd);
174
+ if (commandDescription.length > 0) {
175
+ output = output.concat([helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth), '']);
176
+ }
177
+ const argumentList = helper
178
+ .visibleArguments(cmd)
179
+ .map((argument) => callFormatItem(helper.styleArgumentTerm(helper.argumentTerm(argument)), helper.styleArgumentDescription(helper.argumentDescription(argument))));
180
+ output = output.concat(this.formatItemList('Arguments:', argumentList, helper));
181
+ const optionGroups = this.groupItems(cmd.options, helper.visibleOptions(cmd), (option) => option.helpGroupHeading ?? 'Options:');
182
+ for (const [group, options] of optionGroups) {
183
+ const optionList = options.map((option) => callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option))));
184
+ output = output.concat(this.formatItemList(group, optionList, helper));
185
+ }
186
+ if (helper.showGlobalOptions) {
187
+ const globalOptionList = helper
188
+ .visibleGlobalOptions(cmd)
189
+ .map((option) => callFormatItem(helper.styleOptionTerm(helper.optionTerm(option)), helper.styleOptionDescription(helper.optionDescription(option))));
190
+ output = output.concat(this.formatItemList('Global Options:', globalOptionList, helper));
191
+ }
192
+ const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), (sub) => sub.helpGroup() || 'Commands:');
193
+ for (const [group, commands] of commandGroups) {
194
+ const commandList = commands.map((sub) => callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub)), helper.styleSubcommandDescription(helper.subcommandDescription(sub))));
195
+ output = output.concat(this.formatItemList(group, commandList, helper));
196
+ }
197
+ return output.join('\n');
198
+ }
199
+ displayWidth(str) {
200
+ return stripVTControlCharacters(str).length;
201
+ }
202
+ styleTitle(str) {
203
+ return str;
204
+ }
205
+ styleUsage(str) {
206
+ return str
207
+ .split(' ')
208
+ .map((word) => {
209
+ if (word === '[options]')
210
+ return this.styleOptionText(word);
211
+ if (word === '[command]')
212
+ return this.styleSubcommandText(word);
213
+ if (word[0] === '[' || word[0] === '<')
214
+ return this.styleArgumentText(word);
215
+ return this.styleCommandText(word);
216
+ })
217
+ .join(' ');
218
+ }
219
+ styleCommandDescription(str) {
220
+ return this.styleDescriptionText(str);
221
+ }
222
+ styleOptionDescription(str) {
223
+ return this.styleDescriptionText(str);
224
+ }
225
+ styleSubcommandDescription(str) {
226
+ return this.styleDescriptionText(str);
227
+ }
228
+ styleArgumentDescription(str) {
229
+ return this.styleDescriptionText(str);
230
+ }
231
+ styleDescriptionText(str) {
232
+ return str;
233
+ }
234
+ styleOptionTerm(str) {
235
+ return this.styleOptionText(str);
236
+ }
237
+ styleSubcommandTerm(str) {
238
+ return str
239
+ .split(' ')
240
+ .map((word) => {
241
+ if (word === '[options]')
242
+ return this.styleOptionText(word);
243
+ if (word[0] === '[' || word[0] === '<')
244
+ return this.styleArgumentText(word);
245
+ return this.styleSubcommandText(word);
246
+ })
247
+ .join(' ');
248
+ }
249
+ styleArgumentTerm(str) {
250
+ return this.styleArgumentText(str);
251
+ }
252
+ styleOptionText(str) {
253
+ return str;
254
+ }
255
+ styleArgumentText(str) {
256
+ return str;
257
+ }
258
+ styleSubcommandText(str) {
259
+ return str;
260
+ }
261
+ styleCommandText(str) {
262
+ return str;
263
+ }
264
+ padWidth(cmd, helper) {
265
+ return Math.max(helper.longestOptionTermLength(cmd, helper), helper.longestGlobalOptionTermLength(cmd, helper), helper.longestSubcommandTermLength(cmd, helper), helper.longestArgumentTermLength(cmd, helper));
266
+ }
267
+ preformatted(str) {
268
+ return /\n[^\S\r\n]/.test(str);
269
+ }
270
+ formatItem(term, termWidth, description, helper) {
271
+ const itemIndent = 2;
272
+ const itemIndentStr = ' '.repeat(itemIndent);
273
+ if (!description)
274
+ return itemIndentStr + term;
275
+ const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
276
+ const spacerWidth = 2;
277
+ const helpWidth = this.helpWidth ?? 80;
278
+ const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
279
+ let formattedDescription;
280
+ if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
281
+ formattedDescription = description;
282
+ }
283
+ else {
284
+ const wrappedDescription = helper.boxWrap(description, remainingWidth);
285
+ formattedDescription = wrappedDescription.replace(/\n/g, `\n${' '.repeat(termWidth + spacerWidth)}`);
286
+ }
287
+ return itemIndentStr + paddedTerm + ' '.repeat(spacerWidth) + formattedDescription.replace(/\n/g, `\n${itemIndentStr}`);
288
+ }
289
+ boxWrap(str, width) {
290
+ if (width < this.minWidthToWrap)
291
+ return str;
292
+ const rawLines = str.split(/\r\n|\n/);
293
+ const chunkPattern = /[\s]*[^\s]+/g;
294
+ const wrappedLines = [];
295
+ for (const line of rawLines) {
296
+ const chunks = line.match(chunkPattern);
297
+ if (chunks === null) {
298
+ wrappedLines.push('');
299
+ continue;
300
+ }
301
+ let sumChunks = [chunks.shift() ?? ''];
302
+ let sumWidth = this.displayWidth(sumChunks[0] ?? '');
303
+ for (const chunk of chunks) {
304
+ const visibleWidth = this.displayWidth(chunk);
305
+ if (sumWidth + visibleWidth <= width) {
306
+ sumChunks.push(chunk);
307
+ sumWidth += visibleWidth;
308
+ continue;
309
+ }
310
+ wrappedLines.push(sumChunks.join(''));
311
+ const nextChunk = chunk.trimStart();
312
+ sumChunks = [nextChunk];
313
+ sumWidth = this.displayWidth(nextChunk);
314
+ }
315
+ wrappedLines.push(sumChunks.join(''));
316
+ }
317
+ return wrappedLines.join('\n');
318
+ }
319
+ }
@@ -0,0 +1,58 @@
1
+ import { type ParseArg } from './commander-argument.js';
2
+ export declare class Option {
3
+ flags: string;
4
+ description: string;
5
+ required: boolean;
6
+ optional: boolean;
7
+ variadic: boolean;
8
+ mandatory: boolean;
9
+ short: string | undefined;
10
+ long: string | undefined;
11
+ negate: boolean;
12
+ defaultValue: unknown;
13
+ defaultValueDescription: string | undefined;
14
+ presetArg: unknown;
15
+ envVar: string | undefined;
16
+ parseArg: ParseArg | undefined;
17
+ hidden: boolean;
18
+ argChoices: string[] | undefined;
19
+ conflictsWith: string[];
20
+ implied: Record<string, unknown> | undefined;
21
+ helpGroupHeading: string | undefined;
22
+ constructor(flags: string, description?: string);
23
+ default(value: unknown, description?: string): this;
24
+ /** Value used when the option appears without an option-argument. `parseArg` still runs. */
25
+ preset(arg: unknown): this;
26
+ conflicts(names: string | string[]): this;
27
+ /** Values implied for other options when this one is set and they are not. `parseArg` does not run on them. */
28
+ implies(impliedOptionValues: string | Record<string, unknown>): this;
29
+ env(name: string): this;
30
+ argParser(fn?: ParseArg): this;
31
+ makeOptionMandatory(mandatory?: boolean): this;
32
+ hideHelp(hide?: boolean): this;
33
+ _collectValue(value: unknown, previous: unknown): unknown[];
34
+ choices(values: readonly string[]): this;
35
+ name(): string;
36
+ /** camelCase key used on the options object; `--no-foo` shares `foo` with `--foo`. */
37
+ attributeName(): string;
38
+ helpGroup(heading: string): this;
39
+ is(arg: string): boolean;
40
+ /** Options are one of boolean, negated, required-argument or optional-argument. */
41
+ isBoolean(): boolean;
42
+ }
43
+ /**
44
+ * `--build` and `--no-build` share one value. This works out which of the pair a
45
+ * value (probably) came from, so implied values are only applied for the right one.
46
+ */
47
+ export declare class DualOptions {
48
+ positiveOptions: Map<string, Option>;
49
+ negativeOptions: Map<string, Option>;
50
+ dualOptions: Set<string>;
51
+ constructor(options: Option[]);
52
+ valueFromOption(value: unknown, option: Option): boolean;
53
+ }
54
+ /** Split `'-m,--mixed <value>'` into its short and long flags, failing noisily on anything else. */
55
+ export declare function splitOptionFlags(flags: string): {
56
+ shortFlag: string | undefined;
57
+ longFlag: string | undefined;
58
+ };
@@ -0,0 +1,164 @@
1
+ import { InvalidArgumentError } from './commander-error.js';
2
+ export class Option {
3
+ flags;
4
+ description;
5
+ required;
6
+ optional;
7
+ variadic;
8
+ mandatory = false;
9
+ short;
10
+ long;
11
+ negate = false;
12
+ defaultValue = undefined;
13
+ defaultValueDescription = undefined;
14
+ presetArg = undefined;
15
+ envVar = undefined;
16
+ parseArg = undefined;
17
+ hidden = false;
18
+ argChoices = undefined;
19
+ conflictsWith = [];
20
+ implied = undefined;
21
+ helpGroupHeading = undefined;
22
+ constructor(flags, description) {
23
+ this.flags = flags;
24
+ this.description = description || '';
25
+ this.required = flags.includes('<');
26
+ this.optional = flags.includes('[');
27
+ this.variadic = /\w\.\.\.[>\]]$/.test(flags);
28
+ const { shortFlag, longFlag } = splitOptionFlags(flags);
29
+ this.short = shortFlag;
30
+ this.long = longFlag;
31
+ if (this.long)
32
+ this.negate = this.long.startsWith('--no-');
33
+ }
34
+ default(value, description) {
35
+ this.defaultValue = value;
36
+ this.defaultValueDescription = description;
37
+ return this;
38
+ }
39
+ preset(arg) {
40
+ this.presetArg = arg;
41
+ return this;
42
+ }
43
+ conflicts(names) {
44
+ this.conflictsWith = this.conflictsWith.concat(names);
45
+ return this;
46
+ }
47
+ implies(impliedOptionValues) {
48
+ const newImplied = typeof impliedOptionValues === 'string' ? { [impliedOptionValues]: true } : impliedOptionValues;
49
+ this.implied = Object.assign(this.implied ?? {}, newImplied);
50
+ return this;
51
+ }
52
+ env(name) {
53
+ this.envVar = name;
54
+ return this;
55
+ }
56
+ argParser(fn) {
57
+ this.parseArg = fn;
58
+ return this;
59
+ }
60
+ makeOptionMandatory(mandatory = true) {
61
+ this.mandatory = !!mandatory;
62
+ return this;
63
+ }
64
+ hideHelp(hide = true) {
65
+ this.hidden = !!hide;
66
+ return this;
67
+ }
68
+ _collectValue(value, previous) {
69
+ if (previous === this.defaultValue || !Array.isArray(previous))
70
+ return [value];
71
+ previous.push(value);
72
+ return previous;
73
+ }
74
+ choices(values) {
75
+ this.argChoices = values.slice();
76
+ this.parseArg = (arg, previous) => {
77
+ if (!this.argChoices?.includes(arg)) {
78
+ throw new InvalidArgumentError(`Allowed choices are ${this.argChoices?.join(', ')}.`);
79
+ }
80
+ return this.variadic ? this._collectValue(arg, previous) : arg;
81
+ };
82
+ return this;
83
+ }
84
+ name() {
85
+ if (this.long)
86
+ return this.long.replace(/^--/, '');
87
+ return (this.short ?? '').replace(/^-/, '');
88
+ }
89
+ attributeName() {
90
+ return camelcase(this.negate ? this.name().replace(/^no-/, '') : this.name());
91
+ }
92
+ helpGroup(heading) {
93
+ this.helpGroupHeading = heading;
94
+ return this;
95
+ }
96
+ is(arg) {
97
+ return this.short === arg || this.long === arg;
98
+ }
99
+ isBoolean() {
100
+ return !this.required && !this.optional && !this.negate;
101
+ }
102
+ }
103
+ export class DualOptions {
104
+ positiveOptions = new Map();
105
+ negativeOptions = new Map();
106
+ dualOptions = new Set();
107
+ constructor(options) {
108
+ for (const option of options) {
109
+ (option.negate ? this.negativeOptions : this.positiveOptions).set(option.attributeName(), option);
110
+ }
111
+ for (const key of this.negativeOptions.keys()) {
112
+ if (this.positiveOptions.has(key))
113
+ this.dualOptions.add(key);
114
+ }
115
+ }
116
+ valueFromOption(value, option) {
117
+ const optionKey = option.attributeName();
118
+ if (!this.dualOptions.has(optionKey))
119
+ return true;
120
+ const preset = this.negativeOptions.get(optionKey)?.presetArg;
121
+ const negativeValue = preset !== undefined ? preset : false;
122
+ return option.negate === (negativeValue === value);
123
+ }
124
+ }
125
+ function camelcase(str) {
126
+ return str.split('-').reduce((acc, word) => acc + (word[0] ?? '').toUpperCase() + word.slice(1));
127
+ }
128
+ export function splitOptionFlags(flags) {
129
+ let shortFlag;
130
+ let longFlag;
131
+ const shortFlagExp = /^-[^-]$/;
132
+ const longFlagExp = /^--[^-]/;
133
+ const flagParts = flags.split(/[ |,]+/).concat('guard');
134
+ const head = () => flagParts[0] ?? '';
135
+ if (shortFlagExp.test(head()))
136
+ shortFlag = flagParts.shift();
137
+ if (longFlagExp.test(head()))
138
+ longFlag = flagParts.shift();
139
+ if (!shortFlag && shortFlagExp.test(head()))
140
+ shortFlag = flagParts.shift();
141
+ if (!shortFlag && longFlagExp.test(head())) {
142
+ shortFlag = longFlag;
143
+ longFlag = flagParts.shift();
144
+ }
145
+ if (head().startsWith('-')) {
146
+ const unsupportedFlag = head();
147
+ const baseError = `option creation failed due to '${unsupportedFlag}' in option flags '${flags}'`;
148
+ if (/^-[^-][^-]/.test(unsupportedFlag)) {
149
+ throw new Error(`${baseError}
150
+ - a short flag is a single dash and a single character
151
+ - either use a single dash and a single character (for a short flag)
152
+ - or use a double dash for a long option (and can have two, like '--ws, --workspace')`);
153
+ }
154
+ if (shortFlagExp.test(unsupportedFlag))
155
+ throw new Error(`${baseError}\n- too many short flags`);
156
+ if (longFlagExp.test(unsupportedFlag))
157
+ throw new Error(`${baseError}\n- too many long flags`);
158
+ throw new Error(`${baseError}\n- unrecognised flag format`);
159
+ }
160
+ if (shortFlag === undefined && longFlag === undefined) {
161
+ throw new Error(`option creation failed due to no flags found in '${flags}'.`);
162
+ }
163
+ return { shortFlag, longFlag };
164
+ }
@@ -0,0 +1,2 @@
1
+ /** Close matches, restricted to the same number of edits, as `\n(Did you mean …?)`. */
2
+ export declare function suggestSimilar(word: string, candidates: string[] | undefined): string;