@@ -11,6 +11,15 @@ import type {FlagUsage} from './types.js';
1111
1212type CliOptions = typeof cliOptions ;
1313
14+ /**
15+ * For enums, log the value as uppercase.
16+ * We're going to have an enum for such flags with choices represented
17+ * as an `enum` where the keys of the enum will map to the uppercase `choice`.
18+ */
19+ function formatEnumChoice ( snakeCaseName : string , choice : string ) : string {
20+ return `${ snakeCaseName } _${ choice } ` . toUpperCase ( ) ;
21+ }
22+
1423/**
1524 * Computes telemetry flag usage from parsed arguments and CLI options.
1625 *
@@ -21,6 +30,8 @@ type CliOptions = typeof cliOptions;
2130 * - The provided value differs from the default value.
2231 * - Boolean flags are logged with their literal value.
2332 * - String flags with defined `choices` (Enums) are logged as their uppercase value.
33+ *
34+ * IMPORTANT: keep getPossibleFlagMetrics() in sync with this function.
2435 */
2536export function computeFlagUsage (
2637 args : Record < string , unknown > ,
@@ -49,12 +60,58 @@ export function computeFlagUsage(
4960 'choices' in config &&
5061 config . choices
5162 ) {
52- // For enums, log the value as uppercase
53- // We're going to have an enum for such flags with choices represented
54- // as an `enum` where the keys of the enum will map to the uppercase `choice`.
55- usage [ snakeCaseName ] = `${ snakeCaseName } _${ value } ` . toUpperCase ( ) ;
63+ usage [ snakeCaseName ] = formatEnumChoice ( snakeCaseName , value ) ;
5664 }
5765 }
5866
5967 return usage ;
6068}
69+
70+ export interface FlagMetric {
71+ name : string ;
72+ flagType : 'boolean' | 'enum' ;
73+ choices ?: string [ ] ;
74+ }
75+
76+ /**
77+ * Computes the list of possible flag metrics based on the CLI options.
78+ *
79+ * IMPORTANT: keep this function in sync with computeFlagUsage().
80+ */
81+ export function getPossibleFlagMetrics ( options : CliOptions ) : FlagMetric [ ] {
82+ const metrics : FlagMetric [ ] = [ ] ;
83+
84+ for ( const [ flagName , config ] of Object . entries ( options ) ) {
85+ const snakeCaseName = toSnakeCase ( flagName ) ;
86+
87+ // _present is always a possible metric
88+ metrics . push ( {
89+ name : `${ snakeCaseName } _present` ,
90+ flagType : 'boolean' ,
91+ } ) ;
92+
93+ if ( config . type === 'boolean' ) {
94+ metrics . push ( {
95+ name : snakeCaseName ,
96+ flagType : 'boolean' ,
97+ } ) ;
98+ } else if (
99+ config . type === 'string' &&
100+ 'choices' in config &&
101+ config . choices
102+ ) {
103+ metrics . push ( {
104+ name : snakeCaseName ,
105+ flagType : 'enum' ,
106+ choices : [
107+ `${ snakeCaseName . toUpperCase ( ) } _UNSPECIFIED` ,
108+ ...config . choices . map ( choice =>
109+ formatEnumChoice ( snakeCaseName , choice ) ,
110+ ) ,
111+ ] ,
112+ } ) ;
113+ }
114+ }
115+
116+ return metrics ;
117+ }
0 commit comments