@@ -65,7 +65,30 @@ static async Task<int> Main(string[] args)
6565 }
6666
6767 var builder = BuildCommandLine ( )
68- . UseDefaults ( )
68+ . UseVersionOption ( )
69+ . UseHelp ( )
70+ . UseEnvironmentVariableDirective ( )
71+ . UseParseDirective ( )
72+ . UseSuggestDirective ( )
73+ . RegisterWithDotnetSuggest ( )
74+ . UseTypoCorrections ( )
75+ . UseParseErrorReporting ( )
76+ . CancelOnProcessTermination ( )
77+ . UseExceptionHandler ( ( ex , context ) =>
78+ {
79+ var message = GetExceptionMessage ( ex ) ;
80+ var exitCode = GetExceptionExitCode ( ex ) ;
81+
82+ if ( ! string . IsNullOrEmpty ( message ) )
83+ {
84+ Console . ResetColor ( ) ;
85+ Console . ForegroundColor = ConsoleColor . Red ;
86+ context . Console . Error . WriteLine ( message ) ;
87+ Console . ResetColor ( ) ;
88+ }
89+
90+ context . ExitCode = exitCode ;
91+ } )
6992 . UseHost ( CreateHostBuilder )
7093 . UseRequestAdapter ( ic =>
7194 {
@@ -91,9 +114,20 @@ static async Task<int> Main(string[] args)
91114 ic . BindingContext . AddService ( _ => host . Services . GetRequiredService < LogoutService > ( ) ) ;
92115 await next ( ic ) ;
93116 } ) ;
94- builder . UseExceptionHandler ( ( ex , context ) =>
117+
118+ try
95119 {
96- var message = ex switch
120+ var parser = builder . Build ( ) ;
121+ return await parser . InvokeAsync ( args ) ;
122+ }
123+ finally
124+ {
125+ listener ? . Dispose ( ) ;
126+ }
127+ }
128+
129+ static string ? GetExceptionMessage < E > ( E ex ) where E : Exception {
130+ return ex switch
97131 {
98132 _ when ex is AuthenticationRequiredException => "Token acquisition failed. Run mgc login command first to get an access token." ,
99133 _ when ex is TaskCanceledException => string . Empty ,
@@ -103,36 +137,15 @@ static async Task<int> Main(string[] args)
103137 Identity . Client . MsalException e => $ "Authentication failed: { e . Message } ",
104138 _ => ex . Message
105139 } ;
140+ }
106141
107- var exitCode = ex switch
142+ static int GetExceptionExitCode < E > ( E ex ) where E : Exception => ex switch
108143 {
109144 _ when ex is AuthenticationRequiredException => 1 ,
110145 _ when ex is TaskCanceledException => 0 ,
111146 _ => - 1
112147 } ;
113148
114- if ( ! string . IsNullOrEmpty ( message ) )
115- {
116- Console . ResetColor ( ) ;
117- Console . ForegroundColor = ConsoleColor . Red ;
118- context . Console . Error . WriteLine ( message ) ;
119- Console . ResetColor ( ) ;
120- }
121-
122- context . ExitCode = exitCode ;
123- } ) ;
124-
125- try
126- {
127- var parser = builder . Build ( ) ;
128- return await parser . InvokeAsync ( args ) ;
129- }
130- finally
131- {
132- listener ? . Dispose ( ) ;
133- }
134- }
135-
136149 static CommandLineBuilder BuildCommandLine ( )
137150 {
138151 var rootCommand = new GraphClient ( ) . BuildRootCommand ( ) ;
0 commit comments