Please consider adding a helper like this to batch-configure args/options/etc. across multiple unrelated commands.
declare module '@commander-js/extra-typings' {
interface Command<
Args extends any[] = [],
Opts extends OptionValues = {},
GlobalOpts extends OptionValues = {},
> {
apply<R>(fn: (command: this) => R): R
}
}
// Passes the command to a function that configures it, so reusable configurators
// can be applied without breaking the call chain.
Command.prototype.apply = function <R>(
this: Command,
fn: (command: Command) => R,
): R {
return fn(this)
}
This can be used like so:
export function addBuildDirectoryOption<
Args extends any[],
Opts extends OptionValues,
GlobalOpts extends OptionValues,
>(command: Command<Args, Opts, GlobalOpts>) {
return command
.option('-C <build_dir>', 'build directory, relative to out/ or absolute')
.hook('preAction', (thisCommand) => {
config.applyBuildDirectoryOption(thisCommand.opts())
})
}
...
program
.description('hello')
.apply(addBuildDirectoryOption)
.apply(addOtherOptions)
.apply(addAnotherOptions)
.action(...)
another_program_in_other_file
.description('hello2')
.apply(addOtherOptions)
.action(...)
Please consider adding a helper like this to batch-configure args/options/etc. across multiple unrelated commands.
This can be used like so: