22using CommandLine . Attributes ;
33using OutputColorizer ;
44using System ;
5- using System . Collections . Generic ;
65using System . Linq ;
76using System . Reflection ;
7+ using CommandLine . Colors ;
88
99namespace CommandLine
1010{
@@ -13,105 +13,114 @@ internal static class HelpGenerator
1313 public const string RequestShortHelpParameter = "-?" ;
1414 public const string RequestLongHelpParameter = "--help" ;
1515
16- private static int DisplayCommandLine ( ArgumentGroupInfo arguments )
17- {
18- int maxStringSize = 0 ;
19- for ( int i = 0 ; i < arguments . RequiredArguments . Count ; i ++ )
20- {
21- if ( ! arguments . RequiredArguments . ContainsKey ( i ) )
22- {
23- Colorizer . WriteLine ( $ "{ Environment . NewLine } [Red!Error]: Required argument expected at position [Cyan!{ i } ]. Type declares arguments at position(s) [Cyan!{ string . Join ( "," , arguments . RequiredArguments . Keys . OrderBy ( x => x ) ) } ]. [Red!Check type definition].") ;
24- return - 1 ;
25- }
26-
27- var b = arguments . RequiredArguments [ i ] . GetCustomAttribute < ActualArgumentAttribute > ( ) ;
28- maxStringSize = Math . Max ( maxStringSize , b . Name . Length ) ;
29- Colorizer . Write ( "[Cyan!{0}] " , b . Name ) ;
30- }
31-
32- foreach ( var item in arguments . OptionalArguments . Values )
33- {
34- var b = item . GetCustomAttribute < ActualArgumentAttribute > ( ) ;
35- maxStringSize = Math . Max ( maxStringSize , b . Name . Length ) ;
36- Colorizer . Write ( "\\ [-[Yellow!{0}] value\\ ] " , b . Name ) ;
37- }
38-
39- Colorizer . WriteLine ( string . Empty ) ;
40-
41- return maxStringSize ;
42- }
43-
44- internal static void DisplayHelp ( HelpFormat helpFormat , TypeArgumentInfo arguments )
16+ internal static void DisplayHelp ( HelpFormat helpFormat , TypeArgumentInfo arguments , IColors colors )
4517 {
4618 switch ( helpFormat )
4719 {
4820 case HelpFormat . Short :
49- DisplayShortHelp ( arguments ) ;
21+ DisplayShortHelp ( arguments , colors ) ;
5022 break ;
5123 case HelpFormat . Full :
52- DisplayDetailedHelp ( arguments ) ;
24+ DisplayDetailedHelp ( arguments , colors ) ;
5325 break ;
54- default :
55- throw new ArgumentException ( "Unrecognized help format" , nameof ( helpFormat ) ) ;
5626 }
5727 }
5828
59- private static void DisplayShortHelp ( TypeArgumentInfo type )
29+ private static void DisplayShortHelp ( TypeArgumentInfo type , IColors colors )
6030 {
6131 string exeName = Assembly . GetEntryAssembly ( ) ? . GetName ( ) ? . Name ;
6232 Colorizer . WriteLine ( "Usage: " ) ;
6333
64- DisplayCommandLine ( exeName , type ) ;
34+ DisplayCommandLine ( exeName , type , colors ) ;
6535
6636 Colorizer . WriteLine ( string . Empty ) ;
67- Colorizer . WriteLine ( "For detailed information run '[White!{0} --help]'." , exeName ) ;
37+ string errorFormat = $ "For detailed information run '[{ colors . AssemblyNameColor } !{{0}} --help]'.";
38+ Colorizer . WriteLine ( errorFormat , exeName ) ;
6839 }
6940
70- private static void DisplayDetailedHelp ( TypeArgumentInfo type )
41+ private static void DisplayDetailedHelp ( TypeArgumentInfo type , IColors colors )
7142 {
7243 string exeName = Assembly . GetEntryAssembly ( ) ? . GetName ( ) ? . Name ;
7344 Colorizer . WriteLine ( "Usage: " ) ;
7445
7546 foreach ( var item in type . ArgumentGroups . Keys )
7647 {
77- DisplayDetailedArgumentHelp ( exeName , item , type . ArgumentGroups [ item ] ) ;
48+ DisplayDetailedArgumentHelp ( exeName , item , type . ArgumentGroups [ item ] , colors ) ;
7849 }
7950 }
8051
81- public static void DisplayHelpForCommmand ( string command , ArgumentGroupInfo propertyGroup )
52+ public static void DisplayHelpForCommmand ( string command , ArgumentGroupInfo propertyGroup , IColors colors )
8253 {
8354 string exeName = Assembly . GetEntryAssembly ( ) ? . GetName ( ) ? . Name ;
8455 Colorizer . WriteLine ( "Usage: " ) ;
8556
86- DisplayDetailedArgumentHelp ( exeName , command , propertyGroup ) ;
57+ DisplayDetailedArgumentHelp ( exeName , command , propertyGroup , colors ) ;
8758 }
8859
89- private static void DisplayCommandLine ( string exeName , TypeArgumentInfo type )
60+ private static void DisplayCommandLine ( string exeName , TypeArgumentInfo type , IColors colors )
9061 {
9162 foreach ( var group in type . ArgumentGroups )
9263 {
93- Colorizer . Write ( " [White!{0}.exe] " , exeName ) ;
64+ string assemblyNameFormat = $ " [{ colors . AssemblyNameColor } !{{0}}.exe] "; // " [White!{{0}}.exe] "
65+ Colorizer . Write ( assemblyNameFormat , exeName ) ;
9466 if ( ! string . IsNullOrEmpty ( group . Key ) )
9567 {
96- Colorizer . Write ( $ "[Green!{ group . Key } ] ") ;
68+ string groupFormat = $ " [{ colors . ArgumentGroupColor } !{{0}}] "; // " [Green!{{0}}] "
69+ Colorizer . Write ( string . Format ( groupFormat , group . Key ) ) ;
70+ }
71+ DisplayCommandLine ( group . Value , colors ) ;
72+ }
73+ }
74+
75+ private static int DisplayCommandLine ( ArgumentGroupInfo arguments , IColors colors )
76+ {
77+ int maxStringSize = 0 ;
78+ for ( int i = 0 ; i < arguments . RequiredArguments . Count ; i ++ )
79+ {
80+ if ( ! arguments . RequiredArguments . ContainsKey ( i ) )
81+ {
82+ string requiredArgError = $ "{ Environment . NewLine } [{ colors . ErrorColor } !Error]: Required argument expected at position[{ colors . RequiredArgumentColor } !{{0}}]. Type declares arguments at position(s) [{ colors . RequiredArgumentColor } !{{1}}]. [{ colors . ErrorColor } !Check type definition].";
83+
84+ Colorizer . WriteLine ( requiredArgError , i , string . Join ( "," , arguments . RequiredArguments . Keys . OrderBy ( x => x ) ) ) ;
85+ return - 1 ;
9786 }
98- DisplayCommandLine ( group . Value ) ;
87+
88+ var b = arguments . RequiredArguments [ i ] . GetCustomAttribute < ActualArgumentAttribute > ( ) ;
89+ maxStringSize = Math . Max ( maxStringSize , b . Name . Length ) ;
90+
91+ string argumentName = $ "[{ colors . RequiredArgumentColor } !{{0}}] ";
92+ Colorizer . Write ( argumentName , b . Name ) ;
93+ }
94+
95+ foreach ( var item in arguments . OptionalArguments . Values )
96+ {
97+ var b = item . GetCustomAttribute < ActualArgumentAttribute > ( ) ;
98+ maxStringSize = Math . Max ( maxStringSize , b . Name . Length ) ;
99+
100+ string optionalArgumentFormat = $ "\\ [-[{ colors . OptionalArgumentColor } !{{0}}] value\\ ] ";
101+ Colorizer . Write ( optionalArgumentFormat , b . Name ) ;
99102 }
103+
104+ Colorizer . WriteLine ( string . Empty ) ;
105+
106+ return maxStringSize ;
100107 }
101108
102- private static void DisplayDetailedArgumentHelp ( string exeName , string command , ArgumentGroupInfo arguments )
109+ private static void DisplayDetailedArgumentHelp ( string exeName , string command , ArgumentGroupInfo arguments , IColors colors )
103110 {
104- Colorizer . Write ( " [White!{0}.exe] " , exeName ) ;
111+ string argumentNameFormat = $ " [{ colors . AssemblyNameColor } !{{ 0}}.exe] ";
112+ Colorizer . Write ( argumentNameFormat , exeName ) ;
105113 if ( ! string . IsNullOrEmpty ( command ) )
106114 {
107- Colorizer . Write ( "[Green!{0}] " , command ) ;
115+ string formatArgs = $ "[{ colors . ArgumentGroupColor } !{{0}}] ";
116+ Colorizer . Write ( formatArgs , command ) ;
108117 }
109- DisplayDetailedParameterHelp ( arguments ) ;
118+ DisplayDetailedParameterHelp ( arguments , colors ) ;
110119 }
111120
112- private static void DisplayDetailedParameterHelp ( ArgumentGroupInfo arguments )
121+ private static void DisplayDetailedParameterHelp ( ArgumentGroupInfo arguments , IColors colors )
113122 {
114- int maxStringSize = DisplayCommandLine ( arguments ) ;
123+ int maxStringSize = DisplayCommandLine ( arguments , colors ) ;
115124 if ( maxStringSize < 0 )
116125 {
117126 return ;
@@ -123,11 +132,13 @@ private static void DisplayDetailedParameterHelp(ArgumentGroupInfo arguments)
123132 var b = arguments . RequiredArguments [ i ] . GetCustomAttribute < ActualArgumentAttribute > ( ) ;
124133 if ( TypeHelpers . IsEnum ( arguments . RequiredArguments [ i ] . PropertyType ) )
125134 {
126- Colorizer . WriteLine ( " - [Cyan!{0}] : {1} (one of [Green!{2}], [Cyan!required])" , b . Name . PadRight ( maxStringSize ) , b . Description , GetEnumValuesAsString ( arguments . RequiredArguments [ i ] . PropertyType ) ) ;
135+ string requiredArgFormatDetailed = $ " - [{ colors . RequiredArgumentColor } !{{0}}] : {{1}} (one of [{ colors . ArgumentValueColor } !{{2}}], [{ colors . RequiredArgumentColor } !required])";
136+ Colorizer . WriteLine ( requiredArgFormatDetailed , b . Name . PadRight ( maxStringSize ) , b . Description , GetEnumValuesAsString ( arguments . RequiredArguments [ i ] . PropertyType ) ) ;
127137 }
128138 else
129139 {
130- Colorizer . WriteLine ( " - [Cyan!{0}] : {1} ([Green!{2}], [Cyan!required])" , b . Name . PadRight ( maxStringSize ) , b . Description , GetFriendlyTypeName ( arguments . RequiredArguments [ i ] . PropertyType ) ) ;
140+ string requiredArgFormatDetailed = $ " - [{ colors . RequiredArgumentColor } !{{0}}] : {{1}} ([{ colors . ArgumentValueColor } !{{2}}], [{ colors . RequiredArgumentColor } !required])";
141+ Colorizer . WriteLine ( requiredArgFormatDetailed , b . Name . PadRight ( maxStringSize ) , b . Description , GetFriendlyTypeName ( arguments . RequiredArguments [ i ] . PropertyType ) ) ;
131142 }
132143 }
133144
@@ -138,11 +149,13 @@ private static void DisplayDetailedParameterHelp(ArgumentGroupInfo arguments)
138149
139150 if ( TypeHelpers . IsEnum ( item . PropertyType ) )
140151 {
141- Colorizer . WriteLine ( " - [Yellow!{0}] : {1} (one of [Green!{2}], default=[Yellow!{3}])" , b . Name . PadRight ( maxStringSize ) , b . Description , GetEnumValuesAsString ( item . PropertyType ) , b . DefaultValue ) ;
152+ string optionalFormatArg = $ " - [{ colors . OptionalArgumentColor } !{{0}}] : {{1}} (one of [{ colors . ArgumentValueColor } !{{2}}], default=[{ colors . OptionalArgumentColor } !{{3}}])";
153+ Colorizer . WriteLine ( optionalFormatArg , b . Name . PadRight ( maxStringSize ) , b . Description , GetEnumValuesAsString ( item . PropertyType ) , b . DefaultValue ) ;
142154 }
143155 else
144156 {
145- Colorizer . WriteLine ( " - [Yellow!{0}] : {1} ([Green!{3}], default=[Yellow!{2}])" , b . Name . PadRight ( maxStringSize ) , b . Description , b . DefaultValue ?? "" , GetFriendlyTypeName ( item . PropertyType ) ) ;
157+ string optionalFormatArg = $ " - [{ colors . OptionalArgumentColor } !{{0}}] : {{1}} ([{ colors . ArgumentValueColor } !{{3}}], default=[{ colors . OptionalArgumentColor } !{{2}}])";
158+ Colorizer . WriteLine ( optionalFormatArg , b . Name . PadRight ( maxStringSize ) , b . Description , b . DefaultValue ?? "" , GetFriendlyTypeName ( item . PropertyType ) ) ;
146159 }
147160 }
148161 Colorizer . WriteLine ( string . Empty ) ;
@@ -156,26 +169,33 @@ private static string GetEnumValuesAsString(Type enumType)
156169 private static string GetFriendlyTypeName ( Type propertyType )
157170 {
158171 if ( propertyType == typeof ( string ) )
172+ {
159173 return "string" ;
174+ }
160175
161176 if ( propertyType == typeof ( int ) || propertyType == typeof ( uint ) ||
162177 propertyType == typeof ( sbyte ) || propertyType == typeof ( byte ) ||
163178 propertyType == typeof ( short ) || propertyType == typeof ( ushort ) ||
164179 propertyType == typeof ( double ) || propertyType == typeof ( float ) ||
165180 propertyType == typeof ( long ) || propertyType == typeof ( ulong ) )
181+ {
166182 return "number" ;
183+ }
167184
168185 if ( propertyType == typeof ( bool ) )
186+ {
169187 return "true or false" ;
188+ }
170189
171190 if ( propertyType == typeof ( char ) )
191+ {
172192 return "char" ;
173-
174- if ( TypeHelpers . IsEnum ( propertyType ) )
175- return "enum" ;
193+ }
176194
177195 if ( TypeHelpers . IsList ( propertyType ) )
196+ {
178197 return "list" ;
198+ }
179199
180200 return "" ;
181201 }
0 commit comments