Skip to content

Commit

Permalink
Add example of specifying GlobalOpts with (sub)Command constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Oct 27, 2024
1 parent 5c7eae1 commit acbc92c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/assemble-program.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof createProgram>['opts']>;
14 changes: 14 additions & 0 deletions examples/assemble-sub.ts
Original file line number Diff line number Diff line change
@@ -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<typeof createSub>['opts'];
11 changes: 11 additions & 0 deletions examples/assemble.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit acbc92c

Please sign in to comment.