Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add globalOpts() #1478

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,19 @@ class Command extends EventEmitter {
return this._optionValues;
};

/**
* Return an object containing global options as key-value pairs, from parent command(s).
*
* @return {Object}
*/
globalOpts() {
const result = {};
for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
Object.assign(result, parentCmd.opts());
}
return result;
}

/**
* Internal bottleneck for handling of parsing errors.
*
Expand Down
5 changes: 5 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ declare namespace commander {
*/
opts(): OptionValues;

/**
* Return an object containing global options as key-value pairs, from parent command(s).
*/
globalOpts(): OptionValues;

/**
* Set the description.
*
Expand Down
6 changes: 6 additions & 0 deletions typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ expectType<commander.OptionValues>(opts);
expectType(opts.foo);
expectType(opts['bar']);

// globalOpts
const globalOpts = program.globalOpts();
expectType<commander.OptionValues>(globalOpts);
expectType(globalOpts.foo);
expectType(globalOpts['bar']);

// description
expectType<commander.Command>(program.description('my description'));
expectType<string>(program.description());
Expand Down