Skip to content

Commit a6cbc63

Browse files
alan-agius4hansl
authored andcommitted
fix(@angular/cli): handle case senstive aliases
Closes angular#12780
1 parent b9e0c99 commit a6cbc63

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

packages/angular/cli/models/parser.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ function _coerceType(str: string | undefined, type: OptionType, v?: Value): Valu
2929
}
3030

3131
return _coerceType(str, OptionType.Boolean, v) !== undefined
32-
? _coerceType(str, OptionType.Boolean, v)
33-
: _coerceType(str, OptionType.Number, v) !== undefined
34-
? _coerceType(str, OptionType.Number, v)
35-
: _coerceType(str, OptionType.String, v);
32+
? _coerceType(str, OptionType.Boolean, v)
33+
: _coerceType(str, OptionType.Number, v) !== undefined
34+
? _coerceType(str, OptionType.Number, v)
35+
: _coerceType(str, OptionType.String, v);
3636

3737
case OptionType.String:
3838
return str || '';
@@ -93,21 +93,28 @@ function _coerce(str: string | undefined, o: Option | null, v?: Value): Value |
9393

9494

9595
function _getOptionFromName(name: string, options: Option[]): Option | undefined {
96-
const cName = strings.camelize(name);
96+
const camelName = /(-|_)/.test(name)
97+
? strings.camelize(name)
98+
: name;
9799

98100
for (const option of options) {
99-
if (option.name == name || option.name == cName) {
101+
if (option.name === name || option.name === camelName) {
100102
return option;
101103
}
102104

103-
if (option.aliases.some(x => x == name || x == cName)) {
105+
if (option.aliases.some(x => x === name || x === camelName)) {
104106
return option;
105107
}
106108
}
107109

108110
return undefined;
109111
}
110112

113+
function _removeLeadingDashes(key: string): string {
114+
const from = key.startsWith('--') ? 2 : key.startsWith('-') ? 1 : 0;
115+
116+
return key.substr(from);
117+
}
111118

112119
function _assignOption(
113120
arg: string,
@@ -127,16 +134,10 @@ function _assignOption(
127134

128135
// If flag is --no-abc AND there's no equal sign.
129136
if (i == -1) {
130-
if (key.startsWith('no-')) {
131-
// Only use this key if the option matching the rest is a boolean.
132-
const maybeOption = _getOptionFromName(key.substr(3), options);
133-
if (maybeOption && maybeOption.type == 'boolean') {
134-
value = 'false';
135-
option = maybeOption;
136-
}
137-
} else if (key.startsWith('no')) {
137+
if (key.startsWith('no')) {
138138
// Only use this key if the option matching the rest is a boolean.
139-
const maybeOption = _getOptionFromName(key.substr(2), options);
139+
const from = key.startsWith('no-') ? 3 : 2;
140+
const maybeOption = _getOptionFromName(strings.camelize(key.substr(from)), options);
140141
if (maybeOption && maybeOption.type == 'boolean') {
141142
value = 'false';
142143
option = maybeOption;
@@ -168,7 +169,7 @@ function _assignOption(
168169
}
169170
} else {
170171
key = arg.substring(0, i);
171-
option = _getOptionFromName(key, options) || null;
172+
option = _getOptionFromName(_removeLeadingDashes(key), options) || null;
172173
if (option) {
173174
value = arg.substring(i + 1);
174175
}

packages/angular/cli/models/parser_spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('parseArguments', () => {
1414
{ name: 'bool', aliases: [ 'b' ], type: OptionType.Boolean, description: '' },
1515
{ name: 'num', aliases: [ 'n' ], type: OptionType.Number, description: '' },
1616
{ name: 'str', aliases: [ 's' ], type: OptionType.String, description: '' },
17+
{ name: 'strUpper', aliases: [ 'S' ], type: OptionType.String, description: '' },
1718
{ name: 'helloWorld', aliases: [], type: OptionType.String, description: '' },
1819
{ name: 'helloBool', aliases: [], type: OptionType.Boolean, description: '' },
1920
{ name: 'arr', aliases: [ 'a' ], type: OptionType.Array, description: '' },
@@ -34,6 +35,7 @@ describe('parseArguments', () => {
3435
];
3536

3637
const tests: { [test: string]: Partial<Arguments> | ['!!!', Partial<Arguments>, string[]] } = {
38+
'-S=b': { strUpper: 'b' },
3739
'--bool': { bool: true },
3840
'--bool=1': ['!!!', {}, ['--bool=1']],
3941
'--bool ': { bool: true, p1: '' },

0 commit comments

Comments
 (0)