/** * An object-oriented command-line parser for TypeScript projects. * * @packageDocumentation */ import * as argparse from 'argparse'; /** * Represents a sub-command that is part of the CommandLineParser command line. * Applications should create subclasses of CommandLineAction corresponding to * each action that they want to expose. * * The action name should be comprised of lower case words separated by hyphens * or colons. The name should include an English verb (e.g. "deploy"). Use a * hyphen to separate words (e.g. "upload-docs"). A group of related commands * can be prefixed with a colon (e.g. "docs:generate", "docs:deploy", * "docs:serve", etc). * * @public */ export declare abstract class CommandLineAction extends CommandLineParameterProvider { private static _actionNameRegExp; /** {@inheritDoc ICommandLineActionOptions.actionName} */ readonly actionName: string; /** {@inheritDoc ICommandLineActionOptions.summary} */ readonly summary: string; /** {@inheritDoc ICommandLineActionOptions.documentation} */ readonly documentation: string; private _argumentParser; constructor(options: ICommandLineActionOptions); /* Excluded from this release type: _buildParser */ /* Excluded from this release type: _processParsedData */ /* Excluded from this release type: _execute */ /* Excluded from this release type: _getArgumentParser */ /** * {@inheritDoc CommandLineParameterProvider.onDefineParameters} */ protected abstract onDefineParameters(): void; /** * Your subclass should implement this hook to perform the operation. */ protected abstract onExecute(): Promise; } /** * The data type returned by {@link CommandLineParameterProvider.defineChoiceParameter}. * @public */ export declare class CommandLineChoiceParameter extends CommandLineParameter { /** {@inheritDoc ICommandLineChoiceDefinition.alternatives} */ readonly alternatives: ReadonlyArray; /** {@inheritDoc ICommandLineStringDefinition.defaultValue} */ readonly defaultValue: string | undefined; private _value; /** {@inheritDoc ICommandLineChoiceDefinition.completions} */ readonly completions: (() => Promise) | undefined; /* Excluded from this release type: __constructor */ /** {@inheritDoc CommandLineParameter.kind} */ get kind(): CommandLineParameterKind; /* Excluded from this release type: _setValue */ /* Excluded from this release type: _getSupplementaryNotes */ /** * Returns the argument value for a choice parameter that was parsed from the command line. * * @remarks * The return value will be `undefined` if the command-line has not been parsed yet, * or if the parameter was omitted and has no default value. */ get value(): string | undefined; /** {@inheritDoc CommandLineParameter.appendToArgList} @override */ appendToArgList(argList: string[]): void; } /** * String constants for command line processing. * * @public */ export declare const enum CommandLineConstants { /** * The name of the built-in action that serves suggestions for tab-completion */ TabCompletionActionName = "tab-complete" } /** * The data type returned by {@link CommandLineParameterProvider.defineFlagParameter}. * @public */ export declare class CommandLineFlagParameter extends CommandLineParameter { private _value; /* Excluded from this release type: __constructor */ /** {@inheritDoc CommandLineParameter.kind} */ get kind(): CommandLineParameterKind; /* Excluded from this release type: _setValue */ /** * Returns a boolean indicating whether the parameter was included in the command line. * * @remarks * The return value will be false if the command-line has not been parsed yet, * or if the flag was not used. */ get value(): boolean; /** {@inheritDoc CommandLineParameter.appendToArgList} @override */ appendToArgList(argList: string[]): void; } /** * Helpers for working with the ts-command-line API. * * @public */ export declare class CommandLineHelper { /** * Returns true if the current command line action is tab-complete. * * @public */ static isTabCompletionActionRequest(argv: string[]): boolean; } /** * The data type returned by {@link CommandLineParameterProvider.defineIntegerParameter}. * @public */ export declare class CommandLineIntegerParameter extends CommandLineParameterWithArgument { /** {@inheritDoc ICommandLineStringDefinition.defaultValue} */ readonly defaultValue: number | undefined; private _value; /* Excluded from this release type: __constructor */ /** {@inheritDoc CommandLineParameter.kind} */ get kind(): CommandLineParameterKind; /* Excluded from this release type: _setValue */ /* Excluded from this release type: _getSupplementaryNotes */ /** * Returns the argument value for an integer parameter that was parsed from the command line. * * @remarks * The return value will be undefined if the command-line has not been parsed yet, * or if the parameter was omitted and has no default value. */ get value(): number | undefined; /** {@inheritDoc CommandLineParameter.appendToArgList} @override */ appendToArgList(argList: string[]): void; } /** * The base class for the various command-line parameter types. * @public */ export declare abstract class CommandLineParameter { private static _longNameRegExp; private static _shortNameRegExp; private static _environmentVariableRegExp; /* Excluded from this release type: _parserKey */ /** {@inheritDoc IBaseCommandLineDefinition.parameterLongName} */ readonly longName: string; /** {@inheritDoc IBaseCommandLineDefinition.parameterShortName} */ readonly shortName: string | undefined; /** {@inheritDoc IBaseCommandLineDefinition.description} */ readonly description: string; /** {@inheritDoc IBaseCommandLineDefinition.required} */ readonly required: boolean; /** {@inheritDoc IBaseCommandLineDefinition.environmentVariable} */ readonly environmentVariable: string | undefined; /** {@inheritDoc IBaseCommandLineDefinition.undocumentedSynonyms } */ readonly undocumentedSynonyms: string[] | undefined; /* Excluded from this release type: __constructor */ /* Excluded from this release type: _setValue */ /* Excluded from this release type: _getSupplementaryNotes */ /** * Indicates the type of parameter. */ abstract get kind(): CommandLineParameterKind; /** * Append the parsed values to the provided string array. * @remarks * Sometimes a command line parameter is not used directly, but instead gets passed through to another * tool that will use it. For example if our parameter comes in as "--max-count 3", then we might want to * call `child_process.spawn()` and append ["--max-count", "3"] to the args array for that tool. * appendToArgList() appends zero or more strings to the provided array, based on the input command-line * that we parsed. * * If the parameter was omitted from our command-line and has no default value, then * nothing will be appended. If the short name was used, the long name will be appended instead. * @param argList - the parsed strings will be appended to this string array */ abstract appendToArgList(argList: string[]): void; /** * Internal usage only. Used to report unexpected output from the argparse library. */ protected reportInvalidData(data: any): never; protected validateDefaultValue(hasDefaultValue: boolean): void; } /** * Identifies the kind of a CommandLineParameter. * @public */ export declare enum CommandLineParameterKind { /** Indicates a CommandLineChoiceParameter */ Choice = 0, /** Indicates a CommandLineFlagParameter */ Flag = 1, /** Indicates a CommandLineIntegerParameter */ Integer = 2, /** Indicates a CommandLineStringParameter */ String = 3, /** Indicates a CommandLineStringListParameter */ StringList = 4 } /** * This is the common base class for CommandLineAction and CommandLineParser * that provides functionality for defining command-line parameters. * * @public */ export declare abstract class CommandLineParameterProvider { private static _keyCounter; private _parameters; private _parametersByLongName; private _remainder; /* Excluded from this release type: __constructor */ /** * Returns a collection of the parameters that were defined for this object. */ get parameters(): ReadonlyArray; /** * If {@link CommandLineParameterProvider.defineCommandLineRemainder} was called, * this object captures any remaining command line arguments after the recognized portion. */ get remainder(): CommandLineRemainder | undefined; /** * Defines a command-line parameter whose value must be a string from a fixed set of * allowable choices (similar to an enum). * * @remarks * Example of a choice parameter: * ``` * example-tool --log-level warn * ``` */ defineChoiceParameter(definition: ICommandLineChoiceDefinition): CommandLineChoiceParameter; /** * Returns the CommandLineChoiceParameter with the specified long name. * @remarks * This method throws an exception if the parameter is not defined. */ getChoiceParameter(parameterLongName: string): CommandLineChoiceParameter; /** * Defines a command-line switch whose boolean value is true if the switch is provided, * and false otherwise. * * @remarks * Example usage of a flag parameter: * ``` * example-tool --debug * ``` */ defineFlagParameter(definition: ICommandLineFlagDefinition): CommandLineFlagParameter; /** * Returns the CommandLineFlagParameter with the specified long name. * @remarks * This method throws an exception if the parameter is not defined. */ getFlagParameter(parameterLongName: string): CommandLineFlagParameter; /** * Defines a command-line parameter whose argument is an integer. * * @remarks * Example usage of an integer parameter: * ``` * example-tool --max-attempts 5 * ``` */ defineIntegerParameter(definition: ICommandLineIntegerDefinition): CommandLineIntegerParameter; /** * Returns the CommandLineIntegerParameter with the specified long name. * @remarks * This method throws an exception if the parameter is not defined. */ getIntegerParameter(parameterLongName: string): CommandLineIntegerParameter; /** * Defines a command-line parameter whose argument is a single text string. * * @remarks * Example usage of a string parameter: * ``` * example-tool --message "Hello, world!" * ``` */ defineStringParameter(definition: ICommandLineStringDefinition): CommandLineStringParameter; /** * Returns the CommandLineStringParameter with the specified long name. * @remarks * This method throws an exception if the parameter is not defined. */ getStringParameter(parameterLongName: string): CommandLineStringParameter; /** * Defines a command-line parameter whose argument is a single text string. The parameter can be * specified multiple times to build a list. * * @remarks * Example usage of a string list parameter: * ``` * example-tool --add file1.txt --add file2.txt --add file3.txt * ``` */ defineStringListParameter(definition: ICommandLineStringListDefinition): CommandLineStringListParameter; /** * Defines a rule that captures any remaining command line arguments after the recognized portion. * * @remarks * This feature is useful for commands that pass their arguments along to an external tool, relying on * that tool to perform validation. (It could also be used to parse parameters without any validation * or documentation, but that is not recommended.) * * Example of capturing the remainder after an optional flag parameter. * ``` * example-tool --my-flag this is the remainder * ``` * * In the "--help" documentation, the remainder rule will be represented as "...". */ defineCommandLineRemainder(definition: ICommandLineRemainderDefinition): CommandLineRemainder; /** * Returns the CommandLineStringListParameter with the specified long name. * @remarks * This method throws an exception if the parameter is not defined. */ getStringListParameter(parameterLongName: string): CommandLineStringListParameter; /** * Generates the command-line help text. */ renderHelpText(): string; /** * The child class should implement this hook to define its command-line parameters, * e.g. by calling defineFlagParameter(). */ protected abstract onDefineParameters(): void; /* Excluded from this release type: _getArgumentParser */ /* Excluded from this release type: _processParsedData */ private _generateKey; private _getParameter; private _defineParameter; } /** * The common base class for parameters types that receive an argument. * * @remarks * An argument is an accompanying command-line token, such as "123" in the * example "--max-count 123". * @public */ export declare abstract class CommandLineParameterWithArgument extends CommandLineParameter { private static _invalidArgumentNameRegExp; /** {@inheritDoc IBaseCommandLineDefinitionWithArgument.argumentName} */ readonly argumentName: string; /** {@inheritDoc IBaseCommandLineDefinitionWithArgument.completions} */ readonly completions: (() => Promise) | undefined; /* Excluded from this release type: __constructor */ } /** * 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. * * @public */ export declare abstract class CommandLineParser extends CommandLineParameterProvider { /** * Reports which CommandLineAction was specified on the command line. * @remarks * The value will be assigned before onExecute() is invoked. */ selectedAction: CommandLineAction | undefined; private _argumentParser; private _actionsSubParser; private _options; private _actions; private _actionsByName; private _executed; private _tabCompleteActionWasAdded; constructor(options: ICommandLineParserOptions); /** * Returns the list of actions that were defined for this CommandLineParser object. */ get actions(): ReadonlyArray; /** * Defines a new action that can be used with the CommandLineParser instance. */ addAction(action: CommandLineAction): void; /** * Retrieves the action with the specified name. If no matching action is found, * an exception is thrown. */ getAction(actionName: string): CommandLineAction; /** * Retrieves the action with the specified name. If no matching action is found, * undefined is returned. */ tryGetAction(actionName: string): CommandLineAction | undefined; /** * The program entry point will call this method to begin parsing command-line arguments * and executing the corresponding action. * * @remarks * The returned promise will never reject: If an error occurs, it will be printed * to stderr, process.exitCode will be set to 1, and the promise will resolve to false. * This simplifies the most common usage scenario where the program entry point doesn't * want to be involved with the command-line logic, and will discard the promise without * a then() or catch() block. * * If your caller wants to trap and handle errors, use {@link CommandLineParser.executeWithoutErrorHandling} * instead. * * @param args - the command-line arguments to be parsed; if omitted, then * the process.argv will be used */ execute(args?: string[]): Promise; /** * This is similar to {@link CommandLineParser.execute}, except that execution errors * simply cause the promise to reject. It is the caller's responsibility to trap */ executeWithoutErrorHandling(args?: string[]): Promise; private _validateDefinitions; /* Excluded from this release type: _getArgumentParser */ /** * This hook allows the subclass to perform additional operations before or after * the chosen action is executed. */ protected onExecute(): Promise; } /** * The data type returned by {@link CommandLineParameterProvider.defineCommandLineRemainder}. * @public */ export declare class CommandLineRemainder { private _values; /** {@inheritDoc IBaseCommandLineDefinition.description} */ readonly description: string; /* Excluded from this release type: __constructor */ /** * Returns any remaining command line arguments after the recognized portion * that was parsed from the command line. * * @remarks * The array will be empty if the command-line has not been parsed yet. */ get values(): ReadonlyArray; /* Excluded from this release type: _setValue */ /** {@inheritDoc CommandLineParameter.appendToArgList} @override */ appendToArgList(argList: string[]): void; } /** * The data type returned by {@link CommandLineParameterProvider.defineStringListParameter}. * @public */ export declare class CommandLineStringListParameter extends CommandLineParameterWithArgument { private _values; /* Excluded from this release type: __constructor */ /** {@inheritDoc CommandLineParameter.kind} */ get kind(): CommandLineParameterKind; /* Excluded from this release type: _setValue */ /** * Returns the string arguments for a string list parameter that was parsed from the command line. * * @remarks * The array will be empty if the command-line has not been parsed yet, * or if the parameter was omitted and has no default value. */ get values(): ReadonlyArray; /** {@inheritDoc CommandLineParameter.appendToArgList} @override */ appendToArgList(argList: string[]): void; } /** * The data type returned by {@link CommandLineParameterProvider.defineStringParameter}. * @public */ export declare class CommandLineStringParameter extends CommandLineParameterWithArgument { /** {@inheritDoc ICommandLineStringDefinition.defaultValue} */ readonly defaultValue: string | undefined; private _value; /* Excluded from this release type: __constructor */ /** {@inheritDoc CommandLineParameter.kind} */ get kind(): CommandLineParameterKind; /* Excluded from this release type: _setValue */ /* Excluded from this release type: _getSupplementaryNotes */ /** * Returns the argument value for a string parameter that was parsed from the command line. * * @remarks * The return value will be undefined if the command-line has not been parsed yet, * or if the parameter was omitted and has no default value. */ get value(): string | undefined; /** {@inheritDoc CommandLineParameter.appendToArgList} @override */ appendToArgList(argList: string[]): void; } /** * @public */ export declare class DynamicCommandLineAction extends CommandLineAction { protected onDefineParameters(): void; protected onExecute(): Promise; } /** * @public */ export declare class DynamicCommandLineParser extends CommandLineParser { protected onDefineParameters(): void; } /** * For use with CommandLineParser, this interface represents a generic command-line parameter * * @public */ export declare interface IBaseCommandLineDefinition { /** * The long name of the flag including double dashes, e.g. "--do-something" */ parameterLongName: string; /** * An optional short name for the flag including the dash, e.g. "-d" */ parameterShortName?: string; /** * Documentation for the parameter that will be shown when invoking the tool with "--help" */ description: string; /** * If true, then an error occurs if the parameter was not included on the command-line. */ required?: boolean; /** * The name of an environment variable that the parameter value will be read from, * if it was omitted from the command-line. An error will be reported if the * environment value cannot be parsed. * * @remarks * The environment variable name must consist only of upper-case letters, numbers, * and underscores. It may not start with a number. * * This feature cannot be used when {@link IBaseCommandLineDefinition.required} is true, * because in that case the environmentVariable would never be used. * * Syntax notes for environment variable values: * * - Choice Parameter: The value must match one of the defined choices, * otherwise a validation error is reported. * An empty string causes the environment variable to be ignored. * * - Flag Parameter: The value must be `1` for true, or `0` for false, * otherwise a validation error is reported. * An empty string causes the environment variable to be ignored. * * - Integer Parameter: The value must be an integer number, * otherwise a validation error is reported. * An empty string causes the environment variable to be ignored. * * - String Parameter: Any value is accepted, including an empty string. * * - String List Parameter: If the string starts with `[` (ignoring whitespace) * then it will be parsed as a JSON array, whose elements must be strings, * numbers, or boolean values. * If the string does not start with `[`, then it behaves like an * ordinary String Parameter: Any value is accepted, including an empty string. */ environmentVariable?: string; /** * Specifies additional names for this parameter that are accepted but not displayed * in the command line help. * * @remarks * This option can be used in cases where a command-line parameter may have been renamed, * but the developer doesn't want to break backwards compatibility with systems that may * still be using the old name. Only the `parameterLongName` syntax is currently allowed. */ undocumentedSynonyms?: string[]; } /** * The common base interface for parameter types that accept an argument. * * @remarks * An argument is an accompanying command-line token, such as "123" in the * example "--max-count 123". * @public */ export declare interface IBaseCommandLineDefinitionWithArgument extends IBaseCommandLineDefinition { /** * The name of the argument, which will be shown in the command-line help. * * @remarks * For example, if the parameter name is '--count" and the argument name is "NUMBER", * then the command-line help would display "--count NUMBER". The argument name must * be comprised of upper-case letters, numbers, and underscores. It should be kept short. */ argumentName: string; /** * An optional callback that provides a list of custom choices for tab completion. * @remarks * This option is only used when `ICommandLineParserOptions.enableTabCompletionAction` * is enabled. */ completions?: () => Promise; } /** * Options for the CommandLineAction constructor. * @public */ export declare interface ICommandLineActionOptions { /** * The name of the action. For example, if the tool is called "example", * then the "build" action might be invoked as: "example build -q --some-other-option" */ actionName: string; /** * A quick summary that is shown on the main help page, which is displayed * by the command "example --help" */ summary: string; /** * A detailed description that is shown on the action help page, which is displayed * by the command "example build --help", e.g. for actionName="build". */ documentation: string; } /** * For use with CommandLineParser, this interface represents a parameter which is constrained to * a list of possible options * * @public */ export declare interface ICommandLineChoiceDefinition extends IBaseCommandLineDefinition { /** * A list of strings (which contain no spaces), of possible options which can be selected */ alternatives: string[]; /** * {@inheritDoc ICommandLineStringDefinition.defaultValue} */ defaultValue?: string; /** * An optional callback that provides a list of custom choices for tab completion. * @remarks * This option is only used when `ICommandLineParserOptions.enableTabCompletionAction` * is enabled. */ completions?: () => Promise; } /** * For use with {@link CommandLineParameterProvider.defineFlagParameter}, * this interface defines a command line parameter that is a boolean flag. * * @public */ export declare interface ICommandLineFlagDefinition extends IBaseCommandLineDefinition { } /** * For use with {@link CommandLineParameterProvider.defineIntegerParameter}, * this interface defines a command line parameter whose argument is an integer value. * * @public */ export declare interface ICommandLineIntegerDefinition extends IBaseCommandLineDefinitionWithArgument { /** * {@inheritDoc ICommandLineStringDefinition.defaultValue} */ defaultValue?: number; } /* Excluded from this release type: _ICommandLineParserData */ /** * Options for the {@link CommandLineParser} constructor. * @public */ export declare interface ICommandLineParserOptions { /** * The name of your tool when invoked from the command line */ toolFilename: string; /** * General documentation that is included in the "--help" main page */ toolDescription: string; /** * Set to true to auto-define a tab completion action. False by default. */ enableTabCompletionAction?: boolean; } /** * For use with {@link CommandLineParameterProvider.defineCommandLineRemainder}, * this interface defines a rule that captures any remaining command line arguments after the recognized portion. * * @public */ export declare interface ICommandLineRemainderDefinition { /** * Documentation for how the remaining arguments will be used. This will be shown when invoking * the tool with "--help". */ description: string; } /** * For use with {@link CommandLineParameterProvider.defineStringParameter}, * this interface defines a command line parameter whose argument is a string value. * * @public */ export declare interface ICommandLineStringDefinition extends IBaseCommandLineDefinitionWithArgument { /** * The default value which will be used if the parameter is omitted from the command line. * * @remarks * If a default value is specified, then {@link IBaseCommandLineDefinition.required} * must not be true. Instead, a custom error message should be used to report cases * where a default value was not available. */ defaultValue?: string; } /** * For use with {@link CommandLineParameterProvider.defineStringListParameter}, * this interface defines a command line parameter whose argument is a single text string. * The parameter can be specified multiple times to build a list. * * @public */ export declare interface ICommandLineStringListDefinition extends IBaseCommandLineDefinitionWithArgument { } export { }