Skip to content

Commit 21fe90c

Browse files
committed
Redesign of CommandLineParameterProvider to support the new DynamicCommandLineParser scenario
1 parent ae0b10b commit 21fe90c

8 files changed

Lines changed: 340 additions & 141 deletions

File tree

common/reviews/api/ts-command-line.api.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @internal (undocumented)
1+
// @internal
22
interface _ICommandLineParserData {
33
// (undocumented)
44
[key: string]: any;
@@ -22,26 +22,51 @@ class CommandLineAction extends CommandLineParameterProvider {
2222

2323
// @public
2424
class CommandLineChoiceParameter extends CommandLineParameter<string> {
25+
// @internal
26+
constructor(definition: ICommandLineChoiceDefinition);
27+
readonly alternatives: ReadonlyArray<string>;
28+
readonly defaultValue: string | undefined;
29+
readonly kind: CommandLineParameterKind;
2530
}
2631

2732
// @public
2833
class CommandLineFlagParameter extends CommandLineParameter<boolean> {
34+
// @internal
35+
constructor(definition: ICommandLineFlagDefinition);
36+
readonly kind: CommandLineParameterKind;
2937
}
3038

3139
// @public
32-
class CommandLineIntegerParameter extends CommandLineParameter<number> {
40+
class CommandLineIntegerParameter extends CommandLineParameterWithArgument<number> {
41+
// @internal
42+
constructor(definition: ICommandLineIntegerDefinition);
43+
readonly kind: CommandLineParameterKind;
3344
}
3445

3546
// @public
3647
class CommandLineParameter<T> {
37-
constructor(key: string, converter?: (data: string) => T);
3848
// @internal
39-
readonly _key: string;
49+
constructor(definition: IBaseCommandLineDefinition);
4050
// @internal
41-
_setValue(data: ICommandLineParserData): void;
51+
_parserKey: string;
52+
// @internal
53+
_setValue(data: any): void;
54+
readonly description: string;
55+
readonly kind: CommandLineParameterKind;
56+
readonly longName: string;
57+
readonly shortName: string | undefined;
4258
readonly value: T;
4359
}
4460

61+
// @public
62+
enum CommandLineParameterKind {
63+
Choice = 0,
64+
Flag = 1,
65+
Integer = 2,
66+
String = 3,
67+
StringList = 4
68+
}
69+
4570
// @public
4671
class CommandLineParameterProvider {
4772
// @internal
@@ -55,6 +80,11 @@ class CommandLineParameterProvider {
5580
defineIntegerParameter(definition: ICommandLineIntegerDefinition): CommandLineIntegerParameter;
5681
defineStringListParameter(definition: ICommandLineStringListDefinition): CommandLineStringListParameter;
5782
defineStringParameter(definition: ICommandLineStringDefinition): CommandLineStringParameter;
83+
getChoiceParameter(parameterLongName: string): CommandLineChoiceParameter;
84+
getFlagParameter(parameterLongName: string): CommandLineFlagParameter;
85+
getIntegerParameter(parameterLongName: string): CommandLineIntegerParameter;
86+
getStringListParameter(parameterLongName: string): CommandLineStringListParameter;
87+
getStringParameter(parameterLongName: string): CommandLineStringParameter;
5888
protected abstract onDefineParameters(): void;
5989
}
6090

@@ -69,11 +99,17 @@ class CommandLineParser extends CommandLineParameterProvider {
6999
}
70100

71101
// @public
72-
class CommandLineStringListParameter extends CommandLineParameter<string[]> {
102+
class CommandLineStringListParameter extends CommandLineParameterWithArgument<string[]> {
103+
// @internal
104+
constructor(definition: ICommandLineStringListDefinition);
105+
readonly kind: CommandLineParameterKind;
73106
}
74107

75108
// @public
76-
class CommandLineStringParameter extends CommandLineParameter<string> {
109+
class CommandLineStringParameter extends CommandLineParameterWithArgument<string> {
110+
// @internal
111+
constructor(definition: ICommandLineStringDefinition);
112+
readonly kind: CommandLineParameterKind;
77113
}
78114

79115
// @public (undocumented)
@@ -106,16 +142,16 @@ interface ICommandLineActionOptions {
106142

107143
// @public
108144
interface ICommandLineChoiceDefinition extends IBaseCommandLineDefinition {
145+
alternatives: string[];
109146
defaultValue?: string;
110-
options: string[];
111147
}
112148

113149
// @public
114150
interface ICommandLineFlagDefinition extends IBaseCommandLineDefinition {
115151
}
116152

117153
// @public
118-
interface ICommandLineIntegerDefinition extends IKeyedCommandLineDefinition {
154+
interface ICommandLineIntegerDefinition extends IBaseCommandLineDefinitionWithArgument {
119155
}
120156

121157
// @public
@@ -125,10 +161,10 @@ interface ICommandLineParserOptions {
125161
}
126162

127163
// @public
128-
interface ICommandLineStringDefinition extends IKeyedCommandLineDefinition {
164+
interface ICommandLineStringDefinition extends IBaseCommandLineDefinitionWithArgument {
129165
}
130166

131167
// @public
132-
interface ICommandLineStringListDefinition extends IKeyedCommandLineDefinition {
168+
interface ICommandLineStringListDefinition extends IBaseCommandLineDefinitionWithArgument {
133169
}
134170

libraries/ts-command-line/src/CommandLineAction.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import * as argparse from 'argparse';
5-
import { ICommandLineParserData } from './CommandLineParameter';
6-
import { CommandLineParameterProvider } from './CommandLineParameterProvider';
5+
import { CommandLineParameterProvider, ICommandLineParserData } from './CommandLineParameterProvider';
76

87
/**
98
* Options for the CommandLineAction constructor.

libraries/ts-command-line/src/CommandLineDefinition.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,59 @@ export interface IBaseCommandLineDefinition {
2323
description: string;
2424
}
2525

26-
export interface IKeyedCommandLineDefinition extends IBaseCommandLineDefinition {
26+
export interface IBaseCommandLineDefinitionWithArgument extends IBaseCommandLineDefinition {
2727
/**
28-
* The key used to identify the value of this parameter. This must be a unique value. If it is
29-
* omitted, a unique key is created. This key name appears in the help menu.
30-
* For certain definitions, the key value is not surfaced in the UI.
28+
* The name of the argument, which will be shown in the command-line help.
29+
*
30+
* @remarks
31+
* Suppose the help shows "--output FILE". Then "--output' is the parameter name,
32+
* and "FILE" is the argument name.
3133
*/
32-
key?: string;
34+
argumentName: string;
3335
}
3436

3537
/**
36-
* For use with CommandLineParser, this interface represents a boolean flag command line parameter
38+
* For use with CommandLineParser, this interface represents a parameter which is constrained to
39+
* a list of possible options
3740
*
3841
* @public
3942
*/
40-
export interface ICommandLineFlagDefinition extends IBaseCommandLineDefinition { }
43+
export interface ICommandLineChoiceDefinition extends IBaseCommandLineDefinition {
44+
/**
45+
* A list of strings (which contain no spaces), of possible options which can be selected
46+
*/
47+
alternatives: string[];
48+
49+
/**
50+
* The default value which will be used if the parameter is omitted from the command line
51+
*/
52+
defaultValue?: string;
53+
}
4154

4255
/**
43-
* For use with CommandLineParser, this interface represents a string command line parameter
56+
* For use with CommandLineParser, this interface represents a boolean flag command line parameter
4457
*
4558
* @public
4659
*/
47-
export interface ICommandLineStringDefinition extends IKeyedCommandLineDefinition { }
60+
export interface ICommandLineFlagDefinition extends IBaseCommandLineDefinition { }
4861

4962
/**
50-
* For use with CommandLineParser, this interface represents a string command line parameter
63+
* For use with CommandLineParser, this interface represents an integer command line parameter
5164
*
5265
* @public
5366
*/
54-
export interface ICommandLineStringListDefinition extends IKeyedCommandLineDefinition { }
67+
export interface ICommandLineIntegerDefinition extends IBaseCommandLineDefinitionWithArgument { }
5568

5669
/**
57-
* For use with CommandLineParser, this interface represents a parameter which is constrained to
58-
* a list of possible options
70+
* For use with CommandLineParser, this interface represents a string command line parameter
5971
*
6072
* @public
6173
*/
62-
export interface ICommandLineChoiceDefinition extends IBaseCommandLineDefinition {
63-
/**
64-
* A list of strings (which contain no spaces), of possible options which can be selected
65-
*/
66-
options: string[];
67-
68-
/**
69-
* The default value which will be used if the parameter is omitted from the command line
70-
*/
71-
defaultValue?: string;
72-
}
74+
export interface ICommandLineStringDefinition extends IBaseCommandLineDefinitionWithArgument { }
7375

7476
/**
75-
* For use with CommandLineParser, this interface represents an integer command line parameter
77+
* For use with CommandLineParser, this interface represents a string command line parameter
7678
*
7779
* @public
7880
*/
79-
export interface ICommandLineIntegerDefinition extends IKeyedCommandLineDefinition { }
81+
export interface ICommandLineStringListDefinition extends IBaseCommandLineDefinitionWithArgument { }

0 commit comments

Comments
 (0)