From acbc92cacb652347e09b174b22b70a3af3101e03 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 27 Oct 2024 18:17:15 +1300 Subject: [PATCH] Add example of specifying GlobalOpts with (sub)Command constructor. --- examples/assemble-program.ts | 11 +++++++++++ examples/assemble-sub.ts | 14 ++++++++++++++ examples/assemble.ts | 11 +++++++++++ 3 files changed, 36 insertions(+) create mode 100644 examples/assemble-program.ts create mode 100644 examples/assemble-sub.ts create mode 100644 examples/assemble.ts diff --git a/examples/assemble-program.ts b/examples/assemble-program.ts new file mode 100644 index 0000000..bde8dd5 --- /dev/null +++ b/examples/assemble-program.ts @@ -0,0 +1,11 @@ +import { Command } from '../index.js'; + +// Example of strongly typed globals in a subcommand which is added to program using .addCommand(). +// Declare factory function for root Command in separate file from adding subcommands to avoid circular dependencies. + +export function createProgram() { + const program = new Command().option('-g, --global'); + return program; +} + +export type ProgramOpts = ReturnType['opts']>; diff --git a/examples/assemble-sub.ts b/examples/assemble-sub.ts new file mode 100644 index 0000000..173d21e --- /dev/null +++ b/examples/assemble-sub.ts @@ -0,0 +1,14 @@ +/* eslint-disable @typescript-eslint/no-empty-object-type */ + +// Example of strongly typed globals in a subcommand which is added to program using .addCommand(). + +import { Command } from '../index.js'; +import { type ProgramOpts } from './assemble-program.js'; + +export function createSub() { + const program = new Command<[], {}, ProgramOpts>('sub').option('-l, --local'); + const optsWithGlobals = program.optsWithGlobals(); + return program; +} + +export type SubOpts = ReturnType['opts']; diff --git a/examples/assemble.ts b/examples/assemble.ts new file mode 100644 index 0000000..15bd899 --- /dev/null +++ b/examples/assemble.ts @@ -0,0 +1,11 @@ +import { createProgram, type ProgramOpts } from './assemble-program'; +import { createSub, type SubOpts } from './assemble-sub'; + +// Example of strongly typed globals in a subcommand which is added to program using .addCommand(). + +export function AssembleProgram() { + const program = createProgram(); + const subCommand = createSub(); + program.addCommand(subCommand); + return program; +}