commander
Advanced tools
Comparing version 9.4.1 to 9.5.0
@@ -19,2 +19,3 @@ const { humanReadableArgName } = require('./argument.js'); | ||
this.sortOptions = false; | ||
this.showGlobalOptions = false; | ||
} | ||
@@ -50,2 +51,17 @@ | ||
/** | ||
* Compare options for sort. | ||
* | ||
* @param {Option} a | ||
* @param {Option} b | ||
* @returns number | ||
*/ | ||
compareOptions(a, b) { | ||
const getSortKey = (option) => { | ||
// WYSIWYG for order displayed in help. Short used for comparison if present. No special handling for negated. | ||
return option.short ? option.short.replace(/^-/, '') : option.long.replace(/^--/, ''); | ||
}; | ||
return getSortKey(a).localeCompare(getSortKey(b)); | ||
} | ||
/** | ||
* Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one. | ||
@@ -74,9 +90,3 @@ * | ||
if (this.sortOptions) { | ||
const getSortKey = (option) => { | ||
// WYSIWYG for order displayed in help with short before long, no special handling for negated. | ||
return option.short ? option.short.replace(/^-/, '') : option.long.replace(/^--/, ''); | ||
}; | ||
visibleOptions.sort((a, b) => { | ||
return getSortKey(a).localeCompare(getSortKey(b)); | ||
}); | ||
visibleOptions.sort(this.compareOptions); | ||
} | ||
@@ -87,2 +97,23 @@ return visibleOptions; | ||
/** | ||
* Get an array of the visible global options. (Not including help.) | ||
* | ||
* @param {Command} cmd | ||
* @returns {Option[]} | ||
*/ | ||
visibleGlobalOptions(cmd) { | ||
if (!this.showGlobalOptions) return []; | ||
const globalOptions = []; | ||
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) { | ||
const visibleOptions = parentCmd.options.filter((option) => !option.hidden); | ||
globalOptions.push(...visibleOptions); | ||
} | ||
if (this.sortOptions) { | ||
globalOptions.sort(this.compareOptions); | ||
} | ||
return globalOptions; | ||
} | ||
/** | ||
* Get an array of the arguments if any have a description. | ||
@@ -176,2 +207,16 @@ * | ||
/** | ||
* Get the longest global option term length. | ||
* | ||
* @param {Command} cmd | ||
* @param {Help} helper | ||
* @returns {number} | ||
*/ | ||
longestGlobalOptionTermLength(cmd, helper) { | ||
return helper.visibleGlobalOptions(cmd).reduce((max, option) => { | ||
return Math.max(max, helper.optionTerm(option).length); | ||
}, 0); | ||
} | ||
/** | ||
* Get the longest argument term length. | ||
@@ -224,3 +269,3 @@ * | ||
* Get the subcommand summary to show in the list of subcommands. | ||
* (Fallback to description for backwards compatiblity.) | ||
* (Fallback to description for backwards compatibility.) | ||
* | ||
@@ -350,2 +395,11 @@ * @param {Command} cmd | ||
if (this.showGlobalOptions) { | ||
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => { | ||
return formatItem(helper.optionTerm(option), helper.optionDescription(option)); | ||
}); | ||
if (globalOptionList.length > 0) { | ||
output = output.concat(['Global Options:', formatList(globalOptionList), '']); | ||
} | ||
} | ||
// Commands | ||
@@ -373,2 +427,3 @@ const commandList = helper.visibleCommands(cmd).map((cmd) => { | ||
helper.longestOptionTermLength(cmd, helper), | ||
helper.longestGlobalOptionTermLength(cmd, helper), | ||
helper.longestSubcommandTermLength(cmd, helper), | ||
@@ -375,0 +430,0 @@ helper.longestArgumentTermLength(cmd, helper) |
{ | ||
"name": "commander", | ||
"version": "9.4.1", | ||
"version": "9.5.0", | ||
"description": "the complete solution for node.js command-line programs", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -546,2 +546,4 @@ # Commander.js | ||
You can add alternative names for a command with `.alias()`. ([example](./examples/alias.js)) | ||
For safety, `.addCommand()` does not automatically copy the inherited settings from the parent command. There is a helper routine `.copyInheritedSettings()` for copying the settings when they are wanted. | ||
@@ -919,2 +921,3 @@ | ||
- `sortOptions`: sort the options alphabetically | ||
- `showGlobalOptions`: show a section with the global options from the parent command(s) | ||
@@ -1016,4 +1019,12 @@ There are methods getting the visible lists of arguments, options, and subcommands. There are methods for formatting the items in the lists, with each item having a _term_ and _description_. Take a look at `.formatHelp()` to see how they are used. | ||
If you use `ts-node` and stand-alone executable subcommands written as `.ts` files, you need to call your program through node to get the subcommands called correctly. e.g. | ||
extra-typings: There is an optional project to infer extra type information from the option and argument definitions. | ||
This adds strong typing to the options returned by `.opts()` and the parameters to `.action()`. | ||
See [commander-js/extra-typings](https://github.com/commander-js/extra-typings) for more. | ||
``` | ||
import { Command } from '@commander-js/extra-typings'; | ||
``` | ||
ts-node: If you use `ts-node` and stand-alone executable subcommands written as `.ts` files, you need to call your program through node to get the subcommands called correctly. e.g. | ||
```sh | ||
@@ -1020,0 +1031,0 @@ node -r ts-node/register pm.ts |
@@ -202,2 +202,3 @@ // Type definitions for commander | ||
sortOptions: boolean; | ||
showGlobalOptions: boolean; | ||
@@ -228,2 +229,4 @@ constructor(); | ||
visibleOptions(cmd: Command): Option[]; | ||
/** Get an array of the visible global options. (Not including help.) */ | ||
visibleGlobalOptions(cmd: Command): Option[]; | ||
/** Get an array of the arguments which have descriptions. */ | ||
@@ -236,2 +239,4 @@ visibleArguments(cmd: Command): Argument[]; | ||
longestOptionTermLength(cmd: Command, helper: Help): number; | ||
/** Get the longest global option term length. */ | ||
longestGlobalOptionTermLength(cmd: Command, helper: Help): number; | ||
/** Get the longest argument term length. */ | ||
@@ -601,3 +606,3 @@ longestArgumentTermLength(cmd: Command, helper: Help): number; | ||
/** | ||
* Retrieve option value source. | ||
* Get source of option value. | ||
*/ | ||
@@ -607,2 +612,7 @@ getOptionValueSource(key: string): OptionValueSource | undefined; | ||
/** | ||
* Get source of option value. See also .optsWithGlobals(). | ||
*/ | ||
getOptionValueSourceWithGlobals(key: string): OptionValueSource | undefined; | ||
/** | ||
* Alter parsing of short flags with optional values. | ||
@@ -609,0 +619,0 @@ * |
Sorry, the diff of this file is too big to display
173252
3733
1130