44using Microsoft . Diagnostics . Telemetry ;
55using Microsoft . Diagnostics . Telemetry . Internal ;
66using System . CommandLine ;
7+ using System . CommandLine . Help ;
78using System . CommandLine . Parsing ;
89using System . Diagnostics . Tracing ;
910using System . Text . Json ;
@@ -25,32 +26,43 @@ internal class CommandInvokedEvent : EventBase
2526 internal CommandInvokedEvent ( CommandResult commandResult , DateTime startedTime )
2627 {
2728 CommandName = commandResult . Command . GetType ( ) . FullName ! ;
28- var argumentsDict = commandResult . Children
29- . OfType < ArgumentResult > ( )
30- . ToDictionary ( a => a . Argument . Name , GetValue ) ;
31- var optionsDict = commandResult . Children
32- . OfType < OptionResult > ( )
33- . ToDictionary ( o => o . Option . Name , GetValue ) ;
34- var commandExecutionContext = new CommandExecutionContext ( argumentsDict , optionsDict ) ;
35- Context = JsonSerializer . Serialize ( commandExecutionContext , CommandInvokedEventJsonContext . Default . CommandExecutionContext ) ;
29+ try
30+ {
31+ var argumentsDict = commandResult . Children
32+ . OfType < ArgumentResult > ( )
33+ . ToDictionary ( a => a . Argument . Name , GetValue ) ;
34+ var optionsDict = commandResult . Children
35+ . OfType < OptionResult > ( )
36+ . ToDictionary ( o => o . Option . Name , GetValue ) ;
37+ var commandExecutionContext = new CommandExecutionContext ( argumentsDict , optionsDict ) ;
38+ Context = JsonSerializer . Serialize ( commandExecutionContext , CommandInvokedEventJsonContext . Default . CommandExecutionContext ) ;
39+ }
40+ catch ( Exception ex )
41+ {
42+ Context = $ "[error parsing context]: { ex . Message } ";
43+ }
3644 StartedTime = startedTime ;
3745 }
3846
3947 private string ? GetValue ( OptionResult o )
4048 {
41- return ! o . Errors . Any ( ) ? GetValue ( o . Option . ValueType , o . Implicit , o . GetValueOrDefault < object ? > ( ) ) : "[error]" ;
49+ return o . Option is HelpOption
50+ ? "true"
51+ : ! o . Errors . Any ( ) ? GetValue ( o . Option . ValueType , o . Implicit , ( ) => o . GetValueOrDefault < object ? > ( ) ) : "[error]" ;
4252 }
4353
4454 private string ? GetValue ( ArgumentResult a )
4555 {
46- return ! a . Errors . Any ( ) ? GetValue ( a . Argument . ValueType , a . Implicit , a . GetValueOrDefault < object ? > ( ) ) : "[error]" ;
56+ return ! a . Errors . Any ( )
57+ ? GetValue ( a . Argument . ValueType , a . Implicit , ( ) => a . GetValueOrDefault < object ? > ( ) )
58+ : ( a . Errors . Any ( e => e . Message . StartsWith ( "Required argument missing for command:" ) ) ? null : "[error]" ) ;
4759 }
4860
49- private static string ? GetValue ( Type valueType , bool isImplicit , object ? value )
61+ private static string ? GetValue ( Type valueType , bool isImplicit , Func < object ? > value )
5062 {
5163 return isImplicit ? null : ( ( valueType == typeof ( string ) ||
5264 valueType == typeof ( FileInfo ) ||
53- valueType == typeof ( DirectoryInfo ) ) ? "[string]" : value ) ? . ToString ( ) ;
65+ valueType == typeof ( DirectoryInfo ) ) ? "[string]" : value != null ? value ( ) : null ) ? . ToString ( ) ;
5466 }
5567
5668 public string CommandName { get ; private set ; }
0 commit comments