Skip to content

Help

reference
2 min readUpdated

Kind: Class

Source: lib/help.js

TypeScript import types for JSDoc, used by Visual Studio Code IntelliSense and npm run typescript-checkJS https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#import-types

Help prepares command metadata for help output, including visible commands, options, global options, and arguments. It also creates display terms and comparison data so command help can be ordered and aligned consistently.

Methods

MethodSignatureReturns
prepareContextprepareContext(contextOptions: undefined)void
visibleCommandsvisibleCommands(cmd: undefined)void
compareOptionscompareOptions(a: undefined, b: undefined)void
visibleOptionsvisibleOptions(cmd: undefined)void
visibleGlobalOptionsvisibleGlobalOptions(cmd: undefined)void
visibleArgumentsvisibleArguments(cmd: undefined)void
subcommandTermsubcommandTerm(cmd: undefined)void
optionTermoptionTerm(option: undefined)void
argumentTermargumentTerm(argument: undefined)void
longestSubcommandTermLengthlongestSubcommandTermLength(cmd: undefined, helper: undefined)void
longestOptionTermLengthlongestOptionTermLength(cmd: undefined, helper: undefined)void
longestGlobalOptionTermLengthlongestGlobalOptionTermLength(cmd: undefined, helper: undefined)void
longestArgumentTermLengthlongestArgumentTermLength(cmd: undefined, helper: undefined)void
commandUsagecommandUsage(cmd: undefined)void
commandDescriptioncommandDescription(cmd: undefined)void
subcommandDescriptionsubcommandDescription(cmd: undefined)void
optionDescriptionoptionDescription(option: undefined)void
argumentDescriptionargumentDescription(argument: undefined)void
formatItemListformatItemList(heading: undefined, items: undefined, helper: undefined)void
groupItemsgroupItems(unsortedItems: undefined, visibleItems: undefined, getGroup: undefined)void
formatHelpformatHelp(cmd: undefined, helper: undefined)void
displayWidthdisplayWidth(str: undefined)void
styleTitlestyleTitle(str: undefined)void
styleUsagestyleUsage(str: undefined)void
styleCommandDescriptionstyleCommandDescription(str: undefined)void
styleOptionDescriptionstyleOptionDescription(str: undefined)void
styleSubcommandDescriptionstyleSubcommandDescription(str: undefined)void
styleArgumentDescriptionstyleArgumentDescription(str: undefined)void
styleDescriptionTextstyleDescriptionText(str: undefined)void
styleOptionTermstyleOptionTerm(str: undefined)void
styleSubcommandTermstyleSubcommandTerm(str: undefined)void
styleArgumentTermstyleArgumentTerm(str: undefined)void
styleOptionTextstyleOptionText(str: undefined)void
styleArgumentTextstyleArgumentText(str: undefined)void
styleSubcommandTextstyleSubcommandText(str: undefined)void
styleCommandTextstyleCommandText(str: undefined)void
padWidthpadWidth(cmd: undefined, helper: undefined)void
preformattedpreformatted(str: undefined)void
formatItemformatItem(term: undefined, termWidth: undefined, description: undefined, helper: undefined)void
boxWrapboxWrap(str: undefined, width: undefined)void

Where it refuses work

  • Help stops the work with an early return when word === '[options]', in 2 places.
  • Help stops the work with an early return when word[0] === '[' || word[0] === '<', in 2 places.
  • Help stops the work with an early return when !this.showGlobalOptions.
  • Help stops the work with an early return when cmd.registeredArguments.find((argument) => argument.description).
  • Help stops the work with an early return when option.description.
  • Help stops the work with an early return when argument.description.

Diagram

mermaid
graph LR
  Command[Command definition] --> Help[Help]
  Help --> Context[Prepared context]
  Context --> Commands[Visible commands]
  Context --> Options[Visible options]
  Context --> Arguments[Visible arguments]
  Options --> Terms[Option terms]
  Commands --> Terms
  Arguments --> Terms
  Terms --> HelpOutput[Help output]

Usage

js
import { Command, Help } from 'commander';

const program = new Command()
  .name('deploy')
  .description('Deploy an application')
  .argument('<environment>', 'target environment')
  .option('-d, --dry-run', 'show changes without deploying');

const help = new Help();

help.prepareContext();

const optionTerms = help
  .visibleOptions(program)
  .map((option) => help.optionTerm(option));

const argumentTerms = help
  .visibleArguments(program)
  .map((argument) => help.argumentTerm(argument));

console.log({
  options: optionTerms,
  arguments: argumentTerms,
});

AI Coding Instructions

  • Call prepareContext() before reading visible commands, options, or arguments when generating help content.
  • Use visibleOptions(), visibleGlobalOptions(), and visibleCommands() instead of reading command collections directly so hidden entries are excluded.
  • Build displayed labels through subcommandTerm(), optionTerm(), and argumentTerm() rather than duplicating formatting logic.
  • Use compareOptions() when sorting option lists to keep help output ordering consistent.
  • Use longestSubcommandTermLength() when calculating padding for aligned subcommand output.

Relationships

  • IMPORTS → humanReadableArgName

Used by

2 references from 2 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (2)

  • programindex.js:7
  • Commandlib/command.js:14

Was this page helpful?

Download as PDF