diff --git a/.changeset/good-islands-drive.md b/.changeset/good-islands-drive.md new file mode 100644 index 0000000000..5b909dc84f --- /dev/null +++ b/.changeset/good-islands-drive.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Added `info` object to the context of the alpha CLI. diff --git a/packages/cli/cli-report.backstage-cli-alpha.md b/packages/cli/cli-report.backstage-cli-alpha.md index 917a915fe5..386a642c21 100644 --- a/packages/cli/cli-report.backstage-cli-alpha.md +++ b/packages/cli/cli-report.backstage-cli-alpha.md @@ -56,12 +56,11 @@ Commands: ### `backstage-cli-alpha config docs` ``` -Usage: +Usage: backstage-cli config docs [options] Options: - --help - --package - --version + --package + -h, --help ``` ### `backstage-cli-alpha config schema` diff --git a/packages/cli/src/modules/config/alpha.ts b/packages/cli/src/modules/config/alpha.ts index a0f2624922..a49fddfa24 100644 --- a/packages/cli/src/modules/config/alpha.ts +++ b/packages/cli/src/modules/config/alpha.ts @@ -40,22 +40,21 @@ export default createCliPlugin({ reg.addCommand({ path: ['config', 'docs'], description: 'Browse the configuration reference documentation', - execute: async ({ args }) => { - const argv = await yargs - .options({ - package: { type: 'string' }, - }) - .help() - .parse(args); - const m = - (await require('./commands/docs')) as typeof import('./commands/docs'); - await m.default(argv); + execute: async ({ args, info }) => { + await new Command(info.usage) + .option( + '--package ', + 'Only include the schema that applies to the given package', + ) + .description(info.description) + .action(lazy(() => import('./commands/docs'), 'default')) + .parseAsync(args, { from: 'user' }); }, }); reg.addCommand({ path: ['config:print'], description: 'Print the app configuration for the current package', - execute: async ({ args }) => { + execute: async ({ args, info }) => { const argv = await yargs .options({ package: { type: 'string' }, @@ -65,6 +64,7 @@ export default createCliPlugin({ format: { type: 'string' }, config: { type: 'string', array: true }, }) + .usage(`$0 ${info.usageWithoutProgram}`, info.description) .help() .parse(args); const m = diff --git a/packages/cli/src/wiring/CliInitializer.ts b/packages/cli/src/wiring/CliInitializer.ts index d3cba34f60..8ad3e77cdf 100644 --- a/packages/cli/src/wiring/CliInitializer.ts +++ b/packages/cli/src/wiring/CliInitializer.ts @@ -62,9 +62,11 @@ export class CliInitializer { async run() { await this.#doInit(); + const programName = 'backstage-cli'; + const program = new Command(); program - .name('backstage-cli') + .name(programName) .version(version) .allowUnknownOption(true) .allowExcessArguments(true); @@ -117,6 +119,11 @@ export class CliInitializer { } await node.command.execute({ args: [...positionalArgs, ...args.unknown], + info: { + usage: [programName, ...node.command.path].join(' '), + usageWithoutProgram: node.command.path.join(' '), + description: node.command.description, + }, }); process.exit(0); } catch (error) { diff --git a/packages/cli/src/wiring/types.ts b/packages/cli/src/wiring/types.ts index 0decdc6479..43ca84d604 100644 --- a/packages/cli/src/wiring/types.ts +++ b/packages/cli/src/wiring/types.ts @@ -19,7 +19,23 @@ export interface BackstageCommand { path: string[]; description: string; deprecated?: boolean; - execute: (options: { args: string[] }) => Promise; + execute: (context: { + args: string[]; + info: { + /** + * The usage string of the current command, for example: "backstage-cli repo test" + */ + usage: string; + /** + * The usage string of the current command, without the program name, for example: "repo test" + */ + usageWithoutProgram: string; + /** + * The description provided for the command + */ + description: string; + }; + }) => Promise; } export interface CliFeature {