66 * found in the LICENSE file at https://angular.io/license
77 *
88 */
9- import { BaseException , strings } from '@angular-devkit/core' ;
9+ import { BaseException , logging , strings } from '@angular-devkit/core' ;
1010import { Arguments , Option , OptionType , Value } from './interface' ;
1111
1212
@@ -112,12 +112,15 @@ function _getOptionFromName(name: string, options: Option[]): Option | undefined
112112function _assignOption (
113113 arg : string ,
114114 args : string [ ] ,
115- options : Option [ ] ,
116- parsedOptions : Arguments ,
117- _positionals : string [ ] ,
118- leftovers : string [ ] ,
119- ignored : string [ ] ,
120- errors : string [ ] ,
115+ { options, parsedOptions, leftovers, ignored, errors, deprecations } : {
116+ options : Option [ ] ,
117+ parsedOptions : Arguments ,
118+ positionals : string [ ] ,
119+ leftovers : string [ ] ,
120+ ignored : string [ ] ,
121+ errors : string [ ] ,
122+ deprecations : string [ ] ,
123+ } ,
121124) {
122125 const from = arg . startsWith ( '--' ) ? 2 : 1 ;
123126 let key = arg . substr ( from ) ;
@@ -185,6 +188,11 @@ function _assignOption(
185188 const v = _coerce ( value , option , parsedOptions [ option . name ] ) ;
186189 if ( v !== undefined ) {
187190 parsedOptions [ option . name ] = v ;
191+
192+ if ( option . deprecated !== undefined && option . deprecated !== false ) {
193+ deprecations . push ( `Option ${ JSON . stringify ( option . name ) } is deprecated${
194+ typeof option . deprecated == 'string' ? ': ' + option . deprecated : '' } .`) ;
195+ }
188196 } else {
189197 let error = `Argument ${ key } could not be parsed using value ${ JSON . stringify ( value ) } .` ;
190198 if ( option . enum ) {
@@ -258,9 +266,14 @@ export function parseFreeFormArguments(args: string[]): Arguments {
258266 *
259267 * @param args The argument array to parse.
260268 * @param options List of supported options. {@see Option}.
269+ * @param logger Logger to use to warn users.
261270 * @returns An object that contains a property per option.
262271 */
263- export function parseArguments ( args : string [ ] , options : Option [ ] | null ) : Arguments {
272+ export function parseArguments (
273+ args : string [ ] ,
274+ options : Option [ ] | null ,
275+ logger ?: logging . Logger ,
276+ ) : Arguments {
264277 if ( options === null ) {
265278 options = [ ] ;
266279 }
@@ -271,6 +284,9 @@ export function parseArguments(args: string[], options: Option[] | null): Argume
271284
272285 const ignored : string [ ] = [ ] ;
273286 const errors : string [ ] = [ ] ;
287+ const deprecations : string [ ] = [ ] ;
288+
289+ const state = { options, parsedOptions, positionals, leftovers, ignored, errors, deprecations } ;
274290
275291 for ( let arg = args . shift ( ) ; arg !== undefined ; arg = args . shift ( ) ) {
276292 if ( arg == '--' ) {
@@ -280,22 +296,22 @@ export function parseArguments(args: string[], options: Option[] | null): Argume
280296 }
281297
282298 if ( arg . startsWith ( '--' ) ) {
283- _assignOption ( arg , args , options , parsedOptions , positionals , leftovers , ignored , errors ) ;
299+ _assignOption ( arg , args , state ) ;
284300 } else if ( arg . startsWith ( '-' ) ) {
285301 // Argument is of form -abcdef. Starts at 1 because we skip the `-`.
286302 for ( let i = 1 ; i < arg . length ; i ++ ) {
287303 const flag = arg [ i ] ;
288304 // If the next character is an '=', treat it as a long flag.
289305 if ( arg [ i + 1 ] == '=' ) {
290306 const f = '-' + flag + arg . slice ( i + 1 ) ;
291- _assignOption ( f , args , options , parsedOptions , positionals , leftovers , ignored , errors ) ;
307+ _assignOption ( f , args , state ) ;
292308 break ;
293309 }
294310 // Treat the last flag as `--a` (as if full flag but just one letter). We do this in
295311 // the loop because it saves us a check to see if the arg is just `-`.
296312 if ( i == arg . length - 1 ) {
297313 const arg = '-' + flag ;
298- _assignOption ( arg , args , options , parsedOptions , positionals , leftovers , ignored , errors ) ;
314+ _assignOption ( arg , args , state ) ;
299315 } else {
300316 const maybeOption = _getOptionFromName ( flag , options ) ;
301317 if ( maybeOption ) {
@@ -352,6 +368,10 @@ export function parseArguments(args: string[], options: Option[] | null): Argume
352368 parsedOptions [ '--' ] = [ ...positionals , ...leftovers ] ;
353369 }
354370
371+ if ( deprecations . length > 0 && logger ) {
372+ deprecations . forEach ( message => logger . warn ( message ) ) ;
373+ }
374+
355375 if ( errors . length > 0 ) {
356376 throw new ParseArgumentException ( errors , parsedOptions , ignored ) ;
357377 }
0 commit comments