77 行
3.0 KiB
JavaScript
77 行
3.0 KiB
JavaScript
|
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
||
|
|
// See LICENSE in the project root for license information.
|
||
|
|
import { CommandLineParameterProvider } from './CommandLineParameterProvider';
|
||
|
|
import { CommandLineParserExitError } from './CommandLineParserExitError';
|
||
|
|
import { escapeSprintf } from '../escapeSprintf';
|
||
|
|
/**
|
||
|
|
* Example: "do-something"
|
||
|
|
*/
|
||
|
|
const ACTION_NAME_REGEXP = /^[a-z][a-z0-9]*([-:][a-z0-9]+)*$/;
|
||
|
|
/**
|
||
|
|
* 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 class CommandLineAction extends CommandLineParameterProvider {
|
||
|
|
constructor(options) {
|
||
|
|
super();
|
||
|
|
const { actionName, summary, documentation } = options;
|
||
|
|
if (!ACTION_NAME_REGEXP.test(actionName)) {
|
||
|
|
throw new Error(`Invalid action name "${actionName}". ` +
|
||
|
|
`The name must be comprised of lower-case words optionally separated by hyphens or colons.`);
|
||
|
|
}
|
||
|
|
this.actionName = actionName;
|
||
|
|
this.summary = summary;
|
||
|
|
this.documentation = documentation;
|
||
|
|
this._argumentParser = undefined;
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* This is called internally by CommandLineParser.addAction()
|
||
|
|
* @internal
|
||
|
|
*/
|
||
|
|
_buildParser(actionsSubParser) {
|
||
|
|
this._argumentParser = actionsSubParser.addParser(this.actionName, {
|
||
|
|
help: escapeSprintf(this.summary),
|
||
|
|
description: escapeSprintf(this.documentation)
|
||
|
|
});
|
||
|
|
// Monkey-patch the error handling for the action parser
|
||
|
|
this._argumentParser.exit = (status, message) => {
|
||
|
|
throw new CommandLineParserExitError(status, message);
|
||
|
|
};
|
||
|
|
const originalArgumentParserErrorFn = this._argumentParser.error.bind(this._argumentParser);
|
||
|
|
this._argumentParser.error = (err) => {
|
||
|
|
// Ensure the ParserExitError bubbles up to the top without any special processing
|
||
|
|
if (err instanceof CommandLineParserExitError) {
|
||
|
|
throw err;
|
||
|
|
}
|
||
|
|
originalArgumentParserErrorFn(err);
|
||
|
|
};
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* Invoked by CommandLineParser.onExecute().
|
||
|
|
* @internal
|
||
|
|
*/
|
||
|
|
async _executeAsync() {
|
||
|
|
await this.onExecuteAsync();
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* {@inheritDoc CommandLineParameterProvider._getArgumentParser}
|
||
|
|
* @internal
|
||
|
|
*/
|
||
|
|
_getArgumentParser() {
|
||
|
|
if (!this._argumentParser) {
|
||
|
|
// We will improve this in the future
|
||
|
|
throw new Error('The CommandLineAction must be added to a CommandLineParser before it can be used');
|
||
|
|
}
|
||
|
|
return this._argumentParser;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//# sourceMappingURL=CommandLineAction.js.map
|