argparse documentation Help

DefaultHelpPrinter

DefaultHelpPrinter is the help renderer that argparse uses out of the box. It implements HelpPrinter and is the class to derive from in order to customize help screen.

Public data members

config

DefaultHelpPrinter.config holds a config object that was passed to constructor.

style

DefaultHelpPrinter.style holds an actual style that should be applied to the help screen text. This should be used instead of config.styling.

sink

DefaultHelpPrinter.sink holds the delegate that was passed to constructor. All the functions that print help text send it there.

Public member functions

Constructor

Constructor of DefaultHelpPrinter initializes an object with specified Config, Style and a sink to print to.

Signature

this(const Config config, Style style, void delegate(string) sink)

formatUsage

formatUsage returns the usage line which is usually Usage: .... If the command has custom usage text then %(PROG) in it is replaced with the names of all commands in the stack.

Both printUsage and createHelpScreen use this function, so overriding it changes the usage line everywhere.

Signature

string formatUsage(const CommandHelpInfo[] commands)

Parameters

  • commands

    List of commands starting with top-level command. The usage line is built for the last one.

printUsage

printUsage prints the usage line built by formatUsage, terminated with \n.

Signature

void printUsage(const CommandHelpInfo[] commands)

Parameters

  • commands

    List of commands starting with top-level command. The usage line is printed for the last one.

formatArgumentUsage

formatArgumentUsage returns formatted string for argument usage (argument representation in command usage line) which is usually string like [--foo FOO].

Signature

string formatArgumentUsage(in ArgumentHelpInfo helpInfo, bool usageString)

Parameters

  • helpInfo

    Help info about argument.

  • usageString

    If true then the returned value will be used in usage string, otherwise in argument description.

Return value

String with formatted argument usage info.

formatArgumentValue

formatArgumentValue returns formatted string for argument value: --name <value>. For example, it returns [FOO ...] if argument usage is --foo [FOO ...].

Signature

string formatArgumentValue(in ArgumentHelpInfo helpInfo)

Parameters

  • helpInfo

    Help info about argument.

Return value

String with formatted argument value.

formatArgumentDescription

formatArgumentDescription returns the description of an argument that is printed next to its name, including the default value of the argument if it should be printed. For example, it returns path to config (default: /etc/app.conf) for an argument that is described as path to config and has /etc/app.conf default value.

Signature

string formatArgumentDescription(in ArgumentHelpInfo helpInfo)

Parameters

  • helpInfo

    Help info about argument.

Return value

String with formatted argument description.

printArgumentList

printArgumentList prints the list of arguments returned by formatArgumentList to sink.

Signature

void printArgumentList(const ArgumentHelpInfo[] args)

Parameters

  • args

    Arguments to be listed.

formatArgumentList

formatArgumentList returns a list of arguments formatted the same way the help screen formats them: every argument takes a line with its usage on the left and its description on the right, aligned into two columns and wrapped when needed. It is useful to spell out which arguments a message is about, for example:

--input INPUT File to read the data from destination Where to upload the result

Signature

string formatArgumentList(const ArgumentHelpInfo[] args)

Parameters

  • args

    Arguments to be listed.

Return value

String with the formatted list of arguments. Every line, including the last one, is terminated with \n. Empty string if args is empty.

createHelpScreen

This function creates HelpScreen based on list of commands.

Signature

HelpScreen createHelpScreen(const CommandHelpInfo[] commands)

Parameters

  • commands

    Current stack of (sub)commands starting with top-level command.

Return value

HelpScreen object with all information about help screen.

createSubCommandGroup

Function that creates a HelpScreen.Group group from CommandHelpInfo data.

Signature

HelpScreen.Group createSubCommandGroup(const ref CommandHelpInfo cmd)

Parameters

  • cmd

    Command that contains subcommands.

Return value

HelpScreen.Group object with subcommand entries.

createArgumentsGroups

Function that creates an array of HelpScreen.Group groups from commands data. Usually this function merges groups with the same names from all commands.

Signature

HelpScreen.Group[] createArgumentsGroups(const ref CommandHelpInfo[] commands)

Parameters

  • commands

    Current stack of (sub)commands starting with top-level command.

Return value

Array of HelpScreen.Group objects representing groups of arguments.

printHelp

Function that creates help screen from commands info and prints it to sink.

Signature

void printHelp(const CommandHelpInfo[] commands)

Parameters

  • commands

    Current stack of (sub)commands starting with top-level command.

printHelpScreen

This function prints HelpScreen object through output.

Signature

void printHelpScreen(void delegate(string) output, const ref HelpScreen screen, size_t descriptionOffset)

Parameters

  • output

    Delegate that receives output in pieces.

  • screen

    Help information to print.

  • descriptionOffset

    Screen offset for parameter description. If help text can be represented as two columns of text, descriptionOffset is the position when the second column should start.

printGroup

This function prints HelpScreen.Group object through output.

Signature

void printGroup(void delegate(string) output, const ref HelpScreen.Group group, size_t descriptionOffset)

Parameters

  • output

    Delegate that receives output in pieces.

  • group

    Group of parameters to print.

  • descriptionOffset

    Screen offset for parameter description. If help text can be represented as two columns of text, descriptionOffset is the position when the second column should start.

printParameter

This function prints HelpScreen.Parameter object through output.

Signature

void printParameter(void delegate(string) output, const ref HelpScreen.Parameter param, size_t descriptionOffset)

Parameters

  • output

    Delegate that receives output in pieces. Note that it is not necessarily sink: formatArgumentList uses it to render arguments into a string.

  • param

    Parameter to print.

  • descriptionOffset

    Screen offset for parameter description. If help text can be represented as two columns of text, descriptionOffset is the position when the second column should start.

Public static functions

wrapText

wrapText function wraps text into paragraphs by breaking it up into a sequence of lines separated with \n, such that the length of each line does not exceed specific limit. The last line is terminated with \n.

This function is similar to std.string.wrap but with few adjustments:

  • Styling, if any, is removed during calculation of word length.

  • Line breaks \n are preserved. This allows having line breaks where needed in addition to those that are added by function itself.

  • Output is returned via sink in parts rather than in allocated string. Caller can use appender!string if string is needed.

Signature

static void wrapText(void delegate(string) sink, string text, string firstIndent, string indent, size_t maxLineLength = 80)

Parameters

  • sink

    Delegate that receives output in pieces.

  • text

    Text to be wrapped.

  • firstIndent

    String used to indent first line.

  • indent

    String used to indent second and following lines.

  • maxLineLength

    Maximum line length.

Last modified: 25 September 2026