@@ -11,7 +11,7 @@ internal class ProxyHost
11
11
{
12
12
private Option < int ? > _portOption ;
13
13
private Option < string ? > _ipAddressOption ;
14
- private Option < LogLevel ? > _logLevelOption ;
14
+ private static Option < LogLevel ? > ? _logLevelOption ;
15
15
private Option < bool ? > _recordOption ;
16
16
private Option < IEnumerable < int > ? > _watchPidsOption ;
17
17
private Option < IEnumerable < string > ? > _watchProcessNamesOption ;
@@ -87,8 +87,56 @@ public static string ConfigFile
87
87
}
88
88
}
89
89
90
- public ProxyHost ( )
90
+ private static bool _logLevelResolved = false ;
91
+ private static LogLevel ? _logLevel ;
92
+ public static LogLevel ? LogLevel
91
93
{
94
+ get
95
+ {
96
+ if ( _logLevelResolved )
97
+ {
98
+ return _logLevel ;
99
+ }
100
+
101
+ if ( _logLevelOption is null )
102
+ {
103
+ _logLevelOption = new Option < LogLevel ? > (
104
+ "--log-level" ,
105
+ $ "Level of messages to log. Allowed values: { string . Join ( ", " , Enum . GetNames ( typeof ( LogLevel ) ) ) } "
106
+ )
107
+ {
108
+ ArgumentHelpName = "logLevel"
109
+ } ;
110
+ _logLevelOption . AddValidator ( input => {
111
+ if ( ! Enum . TryParse < LogLevel > ( input . Tokens . First ( ) . Value , true , out var logLevel ) ) {
112
+ input . ErrorMessage = $ "{ input . Tokens . First ( ) . Value } is not a valid log level. Allowed values are: { string . Join ( ", " , Enum . GetNames ( typeof ( LogLevel ) ) ) } ";
113
+ }
114
+ } ) ;
115
+ }
116
+
117
+ var result = _logLevelOption . Parse ( Environment . GetCommandLineArgs ( ) ) ;
118
+ // since we're parsing all args, and other options are not instantiated yet
119
+ // we're getting here a bunch of other errors, so we only need to look for
120
+ // errors related to the log level option
121
+ var error = result . Errors . Where ( e => e . SymbolResult ? . Symbol == _logLevelOption ) . FirstOrDefault ( ) ;
122
+ if ( error is not null )
123
+ {
124
+ // Logger is not available here yet so we need to fallback to Console
125
+ var color = Console . ForegroundColor ;
126
+ Console . ForegroundColor = ConsoleColor . Red ;
127
+ Console . Error . WriteLine ( error . Message ) ;
128
+ Console . ForegroundColor = color ;
129
+ Environment . Exit ( 1 ) ;
130
+ }
131
+
132
+ _logLevel = result . GetValueForOption ( _logLevelOption ) ;
133
+ _logLevelResolved = true ;
134
+
135
+ return _logLevel ;
136
+ }
137
+ }
138
+
139
+ public ProxyHost ( ) {
92
140
_portOption = new Option < int ? > ( "--port" , "The port for the proxy to listen on" ) ;
93
141
_portOption . AddAlias ( "-p" ) ;
94
142
_portOption . ArgumentHelpName = "port" ;
@@ -105,16 +153,6 @@ public ProxyHost()
105
153
}
106
154
} ) ;
107
155
108
- _logLevelOption = new Option < LogLevel ? > ( "--log-level" , $ "Level of messages to log. Allowed values: { string . Join ( ", " , Enum . GetNames ( typeof ( LogLevel ) ) ) } ") ;
109
- _logLevelOption . ArgumentHelpName = "logLevel" ;
110
- _logLevelOption . AddValidator ( input =>
111
- {
112
- if ( ! Enum . TryParse < LogLevel > ( input . Tokens . First ( ) . Value , true , out var logLevel ) )
113
- {
114
- input . ErrorMessage = $ "{ input . Tokens . First ( ) . Value } is not a valid log level. Allowed values are: { string . Join ( ", " , Enum . GetNames ( typeof ( LogLevel ) ) ) } ";
115
- }
116
- } ) ;
117
-
118
156
_recordOption = new Option < bool ? > ( "--record" , "Use this option to record all request logs" ) ;
119
157
120
158
_watchPidsOption = new Option < IEnumerable < int > ? > ( "--watch-pids" , "The IDs of processes to watch for requests" ) ;
@@ -145,7 +183,9 @@ public RootCommand GetRootCommand(ILogger logger)
145
183
var command = new RootCommand {
146
184
_portOption ,
147
185
_ipAddressOption ,
148
- _logLevelOption ,
186
+ // _logLevelOption is set while initialize the Program
187
+ // As such, it's always set here
188
+ _logLevelOption ! ,
149
189
_recordOption ,
150
190
_watchPidsOption ,
151
191
_watchProcessNamesOption ,
0 commit comments