Add optional summary for short form of subcommand description. (#1726)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
For backwards compatibility, the description is used for the subcommand listing if the summary is missing. So by default there is no change in existing programs.
authored by John Gee · committed by GitHub · parent d660967 · Browse files · 8 files·+97 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+97 added−26 removed
- tests:+45 added−20 removed
- other:+52 added−6 removed
Provenance
- Commit
cc2db5b- Tree
4b8eefa- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
d660967
8 files changed+97 added−26 removed
modified:lib/command.js+14 added-1 removed
46 unmodified lines47 this._exitCallback = null;48 this._aliases = [];49 this._combineFlagAndOptionalValue = true;50 this._description = '';51+ this._summary = '';52 this._argsDescription = undefined; // legacy53 this._enablePositionalOptions = false;54 this._passThroughOptions = false;55 this._lifeCycleHooks = {}; // a hash of arrays1711 unmodified lines1767 return this;1768 }1769 1770 /**1770- * Set the description to `str`.1771+ * Set the description.1772 *1773 * @param {string} [str]1774 * @param {Object} [argsDescription]1775 * @return {string|Command}7 unmodified lines1783 return this;1784 }1785 1786 /**1787+ * Set the summary. Used when listed as subcommand of parent.1788+ *1789+ * @param {string} [str]1790+ * @return {string|Command}1791+ */1792+ summary(str) {1793+ if (str === undefined) return this._summary;1794+ this._summary = str;1795+ return this;1796+ }1797+1798+ /**1799 * Set an alias for the command.1800 *1801 * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.1802 *333 unmodified linesmodified:lib/help.js+3 added-2 removed
213 unmodified lines214 return cmd.description();215 }216 217 /**218- * Get the command description to show in the list of subcommands.218+ * Get the subcommand summary to show in the list of subcommands.219+ * (Fallback to description for backwards compatiblity.)220 *221 * @param {Command} cmd222 * @returns {string}223 */224 225 subcommandDescription(cmd) {226 // @ts-ignore: overloaded return type226- return cmd.description();227+ return cmd.summary() || cmd.description();228 }229 230 /**231 * Get the option description to show in the list of options.175 unmodified linesadded:tests/command.summary.test.js+8 added
12345678const commander = require('../');
test('when set summary then get summary', () => { const program = new commander.Command(); const summary = 'abcdef'; program.summary(summary); expect(program.summary()).toMatch(summary);});deleted:tests/help.commandDescription.test.js-20 removed
1234567891011121314151617181920const commander = require('../');
// These are tests of the Help class, not of the Command help.// There is some overlap with the higher level Command tests (which predate Help).
describe('subcommandDescription', () => { test('when program has no description then empty string', () => { const program = new commander.Command(); const helper = new commander.Help(); expect(helper.subcommandDescription(program)).toEqual(''); });
test('when program has description then return description', () => { const description = 'description'; const program = new commander.Command(); program.description(description); const helper = new commander.Help(); expect(helper.subcommandDescription(program)).toEqual(description); });});added:tests/help.subcommandDescription.test.js+37 added
12345678910111213141516171819202122232425262728293031323334353637const commander = require('../');
// These are tests of the Help class, not of the Command help.// There is some overlap with the higher level Command tests (which predate Help).
describe('subcommandDescription', () => { test('when program has no summary or description then empty string', () => { const program = new commander.Command(); const helper = new commander.Help(); expect(helper.subcommandDescription(program)).toEqual(''); });
test('when program has summary then return summary', () => { const summary = 'summary'; const program = new commander.Command(); program.summary(summary); const helper = new commander.Help(); expect(helper.subcommandDescription(program)).toEqual(summary); });
test('when program has description then return description', () => { const description = 'description'; const program = new commander.Command(); program.description(description); const helper = new commander.Help(); expect(helper.subcommandDescription(program)).toEqual(description); });
test('when program has summary and description then return summary', () => { const summary = 'summary'; const program = new commander.Command(); program.summary(summary); program.description('description'); const helper = new commander.Help(); expect(helper.subcommandDescription(program)).toEqual(summary); });});modified:typings/index.d.ts+13 added-1 removed
200 unmodified lines201 constructor();202 203 /** Get the command term to show in the list of subcommands. */204 subcommandTerm(cmd: Command): string;205- /** Get the command description to show in the list of subcommands. */205+ /** Get the command summary to show in the list of subcommands. */206 subcommandDescription(cmd: Command): string;207 /** Get the option term to show in the list of options. */208 optionTerm(option: Option): string;209 /** Get the option description to show in the list of options. */508 unmodified lines718 */719 description(): string;720 721 /**722+ * Set the summary. Used when listed as subcommand of parent.723+ *724+ * @returns `this` command for chaining725+ */726+727+ summary(str: string): this;728+ /**729+ * Get the summary.730+ */731+ summary(): string;732+733+ /**734 * Set an alias for the command.735 *736 * You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.737 *142 unmodified linesmodified:typings/index.test-d.ts+4 added
237 unmodified lines238 expectType<commander.Command>(program.description('my description'));239 expectType<string>(program.description());240 expectType<commander.Command>(program.description('my description of command with arg foo', { foo: 'foo description' })); // deprecated241 242+// summary243+expectType<commander.Command>(program.summary('my summary'));244+expectType<string>(program.summary());245+246 // alias247 expectType<commander.Command>(program.alias('my alias'));248 expectType<string>(program.alias());249 219 unmodified linesmodified:Readme.md+18 added-2 removed
33 unmodified lines34 - [Display help after errors](#display-help-after-errors)35 - [Display help from code](#display-help-from-code)36 - [.name](#name)37 - [.usage](#usage)38+ - [.description and .summary](#description-and-summary)39 - [.helpOption(flags, description)](#helpoptionflags-description)40 - [.addHelpCommand()](#addhelpcommand)41 - [More configuration](#more-configuration-2)42 - [Custom event listeners](#custom-event-listeners)771 unmodified lines814 error: unknown option '--unknown'815 (add --help for additional information)816 ```817 817-You can also show suggestions after an error for an unknown command or option.818+The default behaviour is to suggest correct spelling after an error for an unknown command or option. You819+can disable this.820 821 ```js820-program.showSuggestionAfterError();822+program.showSuggestionAfterError(false);823 ```824 825 ```console826 $ pizza --hepl40 unmodified lines867 ```Text868 Usage: my-command [global options] command869 ```870 871+### .description and .summary872+873+The description appears in the help for the command. You can optionally supply a shorter874+summary to use when listed as a subcommand of the program.875+876+```js877+program878+ .command("duplicate")879+ .summary("make a copy")880+ .description(`Make a copy of the current project.881+This may require additional disk space.882+ `);883+```884+885 ### .helpOption(flags, description)886 887 By default every command has a help option. You may change the default help flags and description. Pass false to disable the built-in help option.888 226 unmodified lines