forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineAction.ts
More file actions
125 lines (104 loc) · 3.81 KB
/
CommandLineAction.ts
File metadata and controls
125 lines (104 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as argparse from 'argparse';
import { CommandLineParameterProvider, ICommandLineParserData } from './CommandLineParameterProvider';
/**
* Options for the CommandLineAction constructor.
* @public
*/
export 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;
}
/**
* 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 abstract class CommandLineAction extends CommandLineParameterProvider {
// Example: "do-something"
private static _actionNameRegExp: RegExp = /^[a-z][a-z0-9]*([-:][a-z0-9]+)*$/;
/** {@inheritDoc ICommandLineActionOptions.actionName} */
public readonly actionName: string;
/** {@inheritDoc ICommandLineActionOptions.summary} */
public readonly summary: string;
/** {@inheritDoc ICommandLineActionOptions.documentation} */
public readonly documentation: string;
private _argumentParser: argparse.ArgumentParser | undefined;
public constructor(options: ICommandLineActionOptions) {
super();
if (!CommandLineAction._actionNameRegExp.test(options.actionName)) {
throw new Error(`Invalid action name "${options.actionName}". `
+ `The name must be comprised of lower-case words optionally separated by hyphens or colons.`);
}
this.actionName = options.actionName;
this.summary = options.summary;
this.documentation = options.documentation;
this._argumentParser = undefined;
}
/**
* This is called internally by CommandLineParser.addAction()
* @internal
*/
public _buildParser(actionsSubParser: argparse.SubParser): void {
this._argumentParser = actionsSubParser.addParser(this.actionName, {
help: this.summary,
description: this.documentation
});
this.onDefineParameters();
}
/**
* This is called internally by CommandLineParser.execute()
* @internal
*/
public _processParsedData(data: ICommandLineParserData): void {
super._processParsedData(data);
}
/**
* Invoked by CommandLineParser.onExecute().
* @internal
*/
public _execute(): Promise<void> {
return this.onExecute();
}
/**
* {@inheritDoc CommandLineParameterProvider._getArgumentParser}
* @internal
*/
protected _getArgumentParser(): argparse.ArgumentParser { // override
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;
}
/**
* {@inheritDoc CommandLineParameterProvider.onDefineParameters}
*/
protected abstract onDefineParameters(): void;
/**
* Your subclass should implement this hook to perform the operation.
*/
protected abstract onExecute(): Promise<void>;
}