@forwardimpact/libcli 0.1.4 → 0.1.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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/cli.js +51 -21
  3. package/src/help.js +15 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forwardimpact/libcli",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Shared CLI infrastructure for the Forward Impact monorepo",
5
5
  "license": "Apache-2.0",
6
6
  "author": "D. Olsson <hi@senzilla.io>",
package/src/cli.js CHANGED
@@ -39,7 +39,23 @@ export class Cli {
39
39
 
40
40
  parse(argv) {
41
41
  const command = this.#findCommand(argv);
42
+ const options = this.#buildOptions(command);
43
+ const { values, positionals } = this.#parseArgs(argv, options);
42
44
 
45
+ if (values.help) {
46
+ this.#renderHelp(command, values.json);
47
+ return null;
48
+ }
49
+
50
+ if (values.version && this.#definition.version) {
51
+ this.#proc.stdout.write(this.#definition.version + "\n");
52
+ return null;
53
+ }
54
+
55
+ return { values, positionals };
56
+ }
57
+
58
+ #buildOptions(command) {
43
59
  const globalOpts = this.#definition.globalOptions || {};
44
60
  const commandOpts = command?.options || {};
45
61
  const merged = { ...globalOpts, ...commandOpts };
@@ -51,32 +67,46 @@ export class Cli {
51
67
  if (opt.default !== undefined) options[name].default = opt.default;
52
68
  if (opt.multiple) options[name].multiple = opt.multiple;
53
69
  }
70
+ return options;
71
+ }
54
72
 
55
- const { values, positionals } = parseArgs({
56
- options,
57
- allowPositionals: true,
58
- args: argv,
59
- });
60
-
61
- if (values.help) {
62
- if (values.json) {
63
- this.#helpRenderer.renderJson(
64
- this.#definition,
65
- this.#proc.stdout,
66
- command,
67
- );
68
- } else {
69
- this.#helpRenderer.render(this.#definition, this.#proc.stdout, command);
73
+ #parseArgs(argv, options) {
74
+ try {
75
+ return parseArgs({ options, allowPositionals: true, args: argv });
76
+ } catch (err) {
77
+ if (err.code === "ERR_PARSE_ARGS_UNKNOWN_OPTION") {
78
+ const commandError = this.#commandAsOptionError(err);
79
+ if (commandError) throw commandError;
70
80
  }
71
- return null;
81
+ throw err;
72
82
  }
83
+ }
73
84
 
74
- if (values.version && this.#definition.version) {
75
- this.#proc.stdout.write(this.#definition.version + "\n");
76
- return null;
77
- }
85
+ #commandAsOptionError(err) {
86
+ const match = err.message.match(/'(--?)([^']+)'/);
87
+ if (!match) return null;
88
+ const bare = match[2];
89
+ const asCommand = this.#definition.commands?.find((c) => c.name === bare);
90
+ if (!asCommand) return null;
91
+ const usage = asCommand.args
92
+ ? `${this.#definition.name} ${bare} ${asCommand.args}`
93
+ : `${this.#definition.name} ${bare}`;
94
+ return new Error(
95
+ `Unknown option "${match[1]}${bare}". "${bare}" is a command, not an option. Usage: ${usage}`,
96
+ { cause: err },
97
+ );
98
+ }
78
99
 
79
- return { values, positionals };
100
+ #renderHelp(command, asJson) {
101
+ if (asJson) {
102
+ this.#helpRenderer.renderJson(
103
+ this.#definition,
104
+ this.#proc.stdout,
105
+ command,
106
+ );
107
+ } else {
108
+ this.#helpRenderer.render(this.#definition, this.#proc.stdout, command);
109
+ }
80
110
  }
81
111
 
82
112
  #findCommand(argv) {
package/src/help.js CHANGED
@@ -83,6 +83,20 @@ export class HelpRenderer {
83
83
  return this.#renderExamplesArray(definition.examples);
84
84
  }
85
85
 
86
+ #renderDocumentation(definition) {
87
+ if (!definition.documentation || definition.documentation.length === 0) {
88
+ return [];
89
+ }
90
+ const lines = [this.#sectionHeader("Documentation:")];
91
+ for (const entry of definition.documentation) {
92
+ lines.push(` ${entry.title}`);
93
+ lines.push(` ${entry.url}`);
94
+ if (entry.description) lines.push(` ${entry.description}`);
95
+ }
96
+ lines.push("");
97
+ return lines;
98
+ }
99
+
86
100
  #renderHintLine(definition) {
87
101
  if (!definition.commands || definition.commands.length === 0) return [];
88
102
  return [
@@ -135,6 +149,7 @@ export class HelpRenderer {
135
149
  ...this.#renderCommands(definition),
136
150
  ...this.#renderOptionSection(definition.globalOptions, "Options:"),
137
151
  ...this.#renderExamples(definition),
152
+ ...this.#renderDocumentation(definition),
138
153
  ...this.#renderHintLine(definition),
139
154
  ];
140
155
  out.write(lines.join("\n"));