Skip to content

Commit ac5a39a

Browse files
committed
feat!: core Usage plugin generates usage for stdout and Markdown
BREAKING CHANGE: Core no longer exports usage() or usageMarkdown() (they are exported from the Usage plugin in the core module).
1 parent 406fa30 commit ac5a39a

10 files changed

Lines changed: 62 additions & 49 deletions

File tree

examples/dev-readme/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Demonstrate using `@qui-cli/markdown` to generate a README
88
- Call `Positionals.requireNoMoreThan(0)` immediately before `Core.init()` to override any required positionals from the imported command.
99
- Note that the command name in the documentation is set by the name of the script that invokes `@qui-cli/markdown` (`./scripts/dev-readme` → `dev-readme`)
1010

11-
## Usage:
12-
13-
```bash
14-
dev-readme -hA --z=<buzz> --bargle --commands --silent --logging --baz=<baz> --biz=<biz> --pi=<pi> --a=<a> --opAccount=<example.1password.com> --opItem=<1Password unique identifier> --opToken=<token value> --logFilePath=<logFilePath> --stdoutLevel=<all|trace|debug|info|warning|error|fatal|off> --fileLevel=<all|trace|debug|info|warning|error|fatal|off> `foo` `bar`
11+
Usage:
12+
`foo` `bar`
13+
```
14+
dev-readme -hA --z=<buzz> --bargle --commands --silent --logging --baz=<baz> --biz=<biz> --pi=<pi> --a=<a> --opAccount=<example.1password.com> --opItem=<1Password unique identifier> --opToken=<token value> --logFilePath=<logFilePath> --stdoutLevel=<all|trace|debug|info|warning|error|fatal|off> --fileLevel=<all|trace|debug|info|warning|error|fatal|off>
1515
```
1616

1717
## Arguments

packages/core/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"url": "https://github.com/battis"
1515
},
1616
"type": "module",
17+
"imports": {
18+
"#plugins": "./dist/Plugins/index.js"
19+
},
1720
"main": "./dist/index.js",
1821
"types": "./dist/index.d.ts",
1922
"scripts": {

packages/core/src/Core.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1+
import { Help, JackSpeak, Positionals } from '#plugins';
12
import * as Plugin from '@qui-cli/plugin';
23
import { Base } from '@qui-cli/plugin/dist/Plugin.js';
3-
import * as Help from './Help.js';
4-
import * as JackSpeak from './JackSpeak.js';
5-
import * as Positionals from './Positionals.js';
64

75
export { Options } from '@qui-cli/plugin';
8-
export * from './Usage.js';
96

107
/** Core configuration options */
118
export type Configuration = Plugin.Registrar.Configuration & {
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as Plugin from '@qui-cli/plugin';
2-
import * as Core from './Core.js';
32

43
export type Configuration = Plugin.Configuration & {
54
/** Get usage information */
@@ -27,8 +26,8 @@ export function options(): Plugin.Options {
2726

2827
export function init({ values }: Plugin.ExpectedArguments<typeof options>) {
2928
configure(values);
30-
if (help) {
31-
process.stdout.write(Core.usage());
32-
process.exit(0);
33-
}
29+
}
30+
31+
export function run() {
32+
return true;
3433
}
Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -277,28 +277,30 @@ export function usage(usage: string, isMarkdown = false): string {
277277
)}${post}`;
278278
}
279279

280-
export function run() {
281-
const s = positionals.length !== 1 ? 's' : '';
282-
if (positionals.length < config.min) {
283-
throw new Error(
284-
`Expected at least ${config.min} positional arguments, received ${positionals.length} positional argument${s}.`
285-
);
286-
}
287-
if (config.max !== undefined && positionals.length > config.max) {
288-
throw new Error(
289-
`Expected no more than ${config.max} positional arguments, received ${positionals.length} positional argument${s}.`
290-
);
291-
}
292-
names().forEach((name, i) => {
293-
if (configSet[name].validate) {
294-
const message = configSet[name].validate(positionals[i]);
295-
if (!message || typeof message === 'string') {
296-
throw new Error(
297-
`Positional argument '${name}' (arg${i}) is not valid${!message ? '.' : `: ${message}`}`
298-
);
299-
}
280+
export function run({ 'core.help': help }: Plugin.AccumulatedResults = {}) {
281+
if (!help) {
282+
const s = positionals.length !== 1 ? 's' : '';
283+
if (positionals.length < config.min) {
284+
throw new Error(
285+
`Expected at least ${config.min} positional arguments, received ${positionals.length} positional argument${s}.`
286+
);
300287
}
301-
});
288+
if (config.max !== undefined && positionals.length > config.max) {
289+
throw new Error(
290+
`Expected no more than ${config.max} positional arguments, received ${positionals.length} positional argument${s}.`
291+
);
292+
}
293+
names().forEach((name, i) => {
294+
if (configSet[name].validate) {
295+
const message = configSet[name].validate(positionals[i]);
296+
if (!message || typeof message === 'string') {
297+
throw new Error(
298+
`Positional argument '${name}' (arg${i}) is not valid${!message ? '.' : `: ${message}`}`
299+
);
300+
}
301+
}
302+
});
303+
}
302304
}
303305

304306
export function get(positionalArgName: string) {
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { JackSpeak, Positionals } from '#plugins';
12
import * as Colors from '@qui-cli/colors/dist/Colors.js';
2-
import * as JackSpeak from './JackSpeak.js';
3-
import * as Positionals from './Positionals.js';
3+
import * as Plugin from '@qui-cli/plugin';
4+
5+
export const name = 'core.usage';
46

57
function commandColor(usage: string) {
68
const pre = usage.slice(0, usage.indexOf('\n ') + 3);
@@ -18,5 +20,12 @@ export function usage() {
1820
}
1921

2022
export function usageMarkdown() {
21-
return Positionals.usage(JackSpeak.usageMarkdown(), true);
23+
return Positionals.usage(JackSpeak.usageMarkdown());
24+
}
25+
26+
export function run({ 'core.help': help }: Plugin.AccumulatedResults = {}) {
27+
if (help) {
28+
process.stdout.write(usage());
29+
process.exit(0);
30+
}
2231
}

packages/core/src/Plugins/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { register } from '@qui-cli/plugin';
2+
import * as Help from './Help.js';
3+
import * as JackSpeak from './JackSpeak.js';
4+
import * as Positionals from './Positionals.js';
5+
import * as Usage from './Usage.js';
6+
7+
export { Help, JackSpeak, Positionals, Usage };
8+
9+
await register(Help);
10+
await register(JackSpeak);
11+
await register(Positionals);
12+
await register(Usage);

packages/core/src/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,2 @@
1-
import { register } from '@qui-cli/plugin';
2-
import * as Help from './Help.js';
3-
import * as JackSpeak from './JackSpeak.js';
4-
import * as Positionals from './Positionals.js';
5-
1+
export * from '#plugins';
62
export * as Core from './Core.js';
7-
export { Help, JackSpeak, Positionals };
8-
9-
await register(Help);
10-
await register(JackSpeak);
11-
await register(Positionals);

packages/markdown/src/Markdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Colors } from '@qui-cli/colors';
2-
import { Core } from '@qui-cli/core';
2+
import { Usage } from '@qui-cli/core';
33
import * as Plugin from '@qui-cli/plugin';
44
import { Root } from '@qui-cli/root';
55
import fs from 'node:fs';
@@ -122,7 +122,7 @@ export async function run() {
122122
fs.writeFileSync(
123123
config.outputPath,
124124
`${config.pre.length ? `${config.pre}\n` : ''}${adjustHeadingLevel(
125-
stripAnsi(restyle(Core.usageMarkdown()))
125+
stripAnsi(restyle(Usage.usageMarkdown()))
126126
)
127127
.replace('Usage:\n\n```', '## Usage:\n\n```bash')
128128
.replace(

0 commit comments

Comments
 (0)