Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/ts-command-line > CommandLineParser

CommandLineParser class

The "argparse" library is a relatively advanced command-line parser with features such as word-wrapping and intelligible error messages (that are lacking in other similar libraries such as commander, yargs, and nomnom). Unfortunately, its ruby-inspired API is awkward to use. The abstract base classes CommandLineParser and CommandLineAction provide a wrapper for "argparse" that makes defining and consuming arguments quick and simple, and enforces that appropriate documentation is provided for each parameter.

Signature:

export declare abstract class CommandLineParser extends CommandLineParameterProvider 

Extends: CommandLineParameterProvider

Constructors

ConstructorModifiersDescription
(constructor)(options)Constructs a new instance of the CommandLineParser class

Properties

PropertyModifiersTypeDescription
actionsreadonlyReadonlyArray<CommandLineAction>Returns the list of actions that were defined for this CommandLineParser object.
selectedActionCommandLineAction | undefinedReports which CommandLineAction was specified on the command line.

Methods

MethodModifiersDescription
addAction(action)Defines a new action that can be used with the CommandLineParser instance.
execute(args)The program entry point will call this method to begin parsing command-line arguments and executing the corresponding action.
executeWithoutErrorHandling(args)This is similar to CommandLineParser.execute(), except that execution errors simply cause the promise to reject. It is the caller's responsibility to trap
getAction(actionName)Retrieves the action with the specified name. If no matching action is found, an exception is thrown.
onExecute()protectedThis hook allows the subclass to perform additional operations before or after the chosen action is executed.
tryGetAction(actionName)Retrieves the action with the specified name. If no matching action is found, undefined is returned.