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

Add support for sharing and stand-alone parsing of subcommands. Warn about parse calls on commands added with .command() #1938

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4f5ab2d
Add missing checks for passThroughOptions
aweebit Aug 4, 2023
0ac6609
Add tests for illegal passThroughOptions
aweebit Aug 4, 2023
9d4c96a
Remove illegal passThroughOptions check deemed unnecessary
aweebit Aug 5, 2023
43d9faa
Use weaker wording: "broken" instead of "illegal"
aweebit Aug 5, 2023
43cc821
Unclutter error message in broken passThrough checks
aweebit Aug 5, 2023
7689506
Refactor _checkForBrokenPassThrough() to make it instance-aware
aweebit Aug 5, 2023
d1686db
Add subroutine for common parse call code
aweebit Aug 5, 2023
3de6161
Add support for sharing and stand-alone parsing of subcommands
aweebit Aug 5, 2023
680930d
Warn about parse calls on commands added with .command()
aweebit Aug 5, 2023
ea728ec
Merge branch 'release/12.x' into feature/parents-with-implicit-subcom…
aweebit Aug 5, 2023
5085ddc
Get rid of redundant currentParent
aweebit Aug 5, 2023
aa280af
Introduce _getCommandAndAncestors()
aweebit Aug 5, 2023
777a452
Use _getCommandAndAncestors() consistently
aweebit Aug 5, 2023
2005522
Introduce _getCommandAndAncestors()
aweebit Aug 5, 2023
da5a499
Use _getCommandAndAncestors() consistently
aweebit Aug 5, 2023
724d414
Merge branch 'feature/_getCommandAndAncestors' into feature/parents-w…
aweebit Aug 5, 2023
5828e16
Copy type for parents and JSDoc comment for parent to .d.ts file
aweebit Aug 5, 2023
8b11122
Use _getCommandAndAncestors() less aggressively
aweebit Aug 10, 2023
f7fd986
Remove NODE_ENV check
aweebit Aug 11, 2023
4d2415c
Merge branch 'feature/_getCommandAndAncestors' into feature/parents-w…
aweebit Aug 12, 2023
cea6e9d
Reword warning about parse calls on commands added with .command()
aweebit Aug 12, 2023
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
Prev Previous commit
Next Next commit
Use _getCommandAndAncestors() consistently
  • Loading branch information
aweebit committed Aug 5, 2023
commit 777a4528a6160171740fc795b517c7264d736d26
15 changes: 7 additions & 8 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1428,13 +1428,13 @@ Call on top-level command instead`);

_checkForMissingMandatoryOptions() {
// Walk up hierarchy so can call in subcommand after checking for displaying help.
for (let cmd = this; cmd; cmd = cmd.parent) {
this._getCommandAndAncestors().forEach((cmd) => {
cmd.options.forEach((anOption) => {
if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) {
cmd.missingMandatoryOptionValue(anOption);
}
});
}
});
}

/**
Expand Down Expand Up @@ -1475,9 +1475,9 @@ Call on top-level command instead`);
*/
_checkForConflictingOptions() {
// Walk up hierarchy so can call in subcommand after checking for displaying help.
for (let cmd = this; cmd; cmd = cmd.parent) {
this._getCommandAndAncestors().forEach((cmd) => {
cmd._checkForConflictingLocalOptions();
}
});
}

/**
Expand Down Expand Up @@ -1806,14 +1806,13 @@ Call on top-level command instead`);
if (flag.startsWith('--') && this._showSuggestionAfterError) {
// Looping to pick up the global options too
let candidateFlags = [];
let command = this;
do {
for (const command of this._getCommandAndAncestors()) {
const moreFlags = command.createHelp().visibleOptions(command)
.filter(option => option.long)
.map(option => option.long);
candidateFlags = candidateFlags.concat(moreFlags);
command = command.parent;
} while (command && !command._enablePositionalOptions);
if (command._enablePositionalOptions) break;
}
suggestion = suggestSimilar(flag, candidateFlags);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class Help {
if (!this.showGlobalOptions) return [];

const globalOptions = [];
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
const visibleOptions = parentCmd.options.filter((option) => !option.hidden);
cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
globalOptions.push(...visibleOptions);
}
});
if (this.sortOptions) {
globalOptions.sort(this.compareOptions);
}
Expand Down Expand Up @@ -240,11 +240,11 @@ class Help {
if (cmd._aliases[0]) {
cmdName = cmdName + '|' + cmd._aliases[0];
}
let parentCmdNames = '';
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
}
return parentCmdNames + cmdName + ' ' + cmd.usage();
let ancestorCmdNames = '';
cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {
ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;
});
return ancestorCmdNames + cmdName + ' ' + cmd.usage();
}

/**
Expand Down