Home > @microsoft/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 

Constructors

Constructor Modifiers Description
(constructor)(options)   Constructs a new instance of the CommandLineParser class

Properties

Property Modifiers Type Description
actions   ReadonlyArray<CommandLineAction> Returns the list of actions that were defined for this CommandLineParser object.
selectedAction   CommandLineAction | undefined Reports which CommandLineAction was specified on the command line.
toolDescription   string General documentation that is included in the “–help” main page
toolFilename   string The name of your tool when invoked from the command line

Methods

Method Modifiers Description
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()   This 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.