@@ -414,7 +414,7 @@ public static string CombineSettings(string[] settings)
414
414
}
415
415
416
416
// REVIEW: Add a method for cloning arguments, instead of going to text and back.
417
- public static string GetSettings ( IExceptionContext ectx , object values , object defaults , SettingsFlags flags = SettingsFlags . Default )
417
+ public static string GetSettings ( IHostEnvironment env , object values , object defaults , SettingsFlags flags = SettingsFlags . Default )
418
418
{
419
419
Type t1 = values . GetType ( ) ;
420
420
Type t2 = defaults . GetType ( ) ;
@@ -430,7 +430,7 @@ public static string GetSettings(IExceptionContext ectx, object values, object d
430
430
return null ;
431
431
432
432
var info = GetArgumentInfo ( t , defaults ) ;
433
- return GetSettingsCore ( ectx , info , values , flags ) ;
433
+ return GetSettingsCore ( env , info , values , flags ) ;
434
434
}
435
435
436
436
public static IEnumerable < KeyValuePair < string , string > > GetSettingPairs ( IHostEnvironment env , object values , object defaults , SettingsFlags flags = SettingsFlags . None )
@@ -862,20 +862,20 @@ private bool Parse(ArgumentInfo info, string[] strs, object destination)
862
862
return ! hadError ;
863
863
}
864
864
865
- private static string GetSettingsCore ( IExceptionContext ectx , ArgumentInfo info , object values , SettingsFlags flags )
865
+ private static string GetSettingsCore ( IHostEnvironment env , ArgumentInfo info , object values , SettingsFlags flags )
866
866
{
867
867
StringBuilder sb = new StringBuilder ( ) ;
868
868
869
869
if ( info . ArgDef != null )
870
870
{
871
871
var val = info . ArgDef . GetValue ( values ) ;
872
- info . ArgDef . AppendSetting ( ectx , sb , val , flags ) ;
872
+ info . ArgDef . AppendSetting ( env , sb , val , flags ) ;
873
873
}
874
874
875
875
foreach ( Argument arg in info . Args )
876
876
{
877
877
var val = arg . GetValue ( values ) ;
878
- arg . AppendSetting ( ectx , sb , val , flags ) ;
878
+ arg . AppendSetting ( env , sb , val , flags ) ;
879
879
}
880
880
881
881
return sb . ToString ( ) ;
@@ -886,7 +886,7 @@ private static string GetSettingsCore(IExceptionContext ectx, ArgumentInfo info,
886
886
/// It deals with custom "unparse" functionality, as well as quoting. It also appends to a StringBuilder
887
887
/// instead of returning a string.
888
888
/// </summary>
889
- private static void AppendCustomItem ( IExceptionContext ectx , ArgumentInfo info , object values , SettingsFlags flags , StringBuilder sb )
889
+ private static void AppendCustomItem ( IHostEnvironment env , ArgumentInfo info , object values , SettingsFlags flags , StringBuilder sb )
890
890
{
891
891
int ich = sb . Length ;
892
892
// We always call unparse, even when NoUnparse is specified, since Unparse can "cleanse", which
@@ -902,28 +902,28 @@ private static void AppendCustomItem(IExceptionContext ectx, ArgumentInfo info,
902
902
if ( info . ArgDef != null )
903
903
{
904
904
var val = info . ArgDef . GetValue ( values ) ;
905
- info . ArgDef . AppendSetting ( ectx , sb , val , flags ) ;
905
+ info . ArgDef . AppendSetting ( env , sb , val , flags ) ;
906
906
}
907
907
908
908
foreach ( Argument arg in info . Args )
909
909
{
910
910
var val = arg . GetValue ( values ) ;
911
- arg . AppendSetting ( ectx , sb , val , flags ) ;
911
+ arg . AppendSetting ( env , sb , val , flags ) ;
912
912
}
913
913
914
914
string str = sb . ToString ( ich , sb . Length - ich ) ;
915
915
sb . Length = ich ;
916
916
CmdQuoter . QuoteValue ( str , sb , force : true ) ;
917
917
}
918
918
919
- private IEnumerable < KeyValuePair < string , string > > GetSettingPairsCore ( IExceptionContext ectx , ArgumentInfo info , object values , SettingsFlags flags )
919
+ private IEnumerable < KeyValuePair < string , string > > GetSettingPairsCore ( IHostEnvironment env , ArgumentInfo info , object values , SettingsFlags flags )
920
920
{
921
921
StringBuilder buffer = new StringBuilder ( ) ;
922
922
foreach ( Argument arg in info . Args )
923
923
{
924
924
string key = arg . GetKey ( flags ) ;
925
925
object value = arg . GetValue ( values ) ;
926
- foreach ( string val in arg . GetSettingStrings ( ectx , value , buffer ) )
926
+ foreach ( string val in arg . GetSettingStrings ( env , value , buffer ) )
927
927
yield return new KeyValuePair < string , string > ( key , val ) ;
928
928
}
929
929
}
@@ -943,13 +943,13 @@ public ArgumentHelpStrings(string syntax, string help)
943
943
/// <summary>
944
944
/// A user friendly usage string describing the command line argument syntax.
945
945
/// </summary>
946
- private string GetUsageString ( IExceptionContext ectx , ArgumentInfo info , bool showRsp = true , int ? columns = null )
946
+ private string GetUsageString ( IHostEnvironment env , ArgumentInfo info , bool showRsp = true , int ? columns = null )
947
947
{
948
948
int screenWidth = columns ?? Console . BufferWidth ;
949
949
if ( screenWidth <= 0 )
950
950
screenWidth = 80 ;
951
951
952
- ArgumentHelpStrings [ ] strings = GetAllHelpStrings ( ectx , info , showRsp ) ;
952
+ ArgumentHelpStrings [ ] strings = GetAllHelpStrings ( env , info , showRsp ) ;
953
953
954
954
int maxParamLen = 0 ;
955
955
foreach ( ArgumentHelpStrings helpString in strings )
@@ -1039,17 +1039,17 @@ private static void AddNewLine(string newLine, StringBuilder builder, ref int cu
1039
1039
currentColumn = 0 ;
1040
1040
}
1041
1041
1042
- private static ArgumentHelpStrings [ ] GetAllHelpStrings ( IExceptionContext ectx , ArgumentInfo info , bool showRsp )
1042
+ private ArgumentHelpStrings [ ] GetAllHelpStrings ( IHostEnvironment env , ArgumentInfo info , bool showRsp )
1043
1043
{
1044
1044
List < ArgumentHelpStrings > strings = new List < ArgumentHelpStrings > ( ) ;
1045
1045
1046
1046
if ( info . ArgDef != null )
1047
- strings . Add ( GetHelpStrings ( ectx , info . ArgDef ) ) ;
1047
+ strings . Add ( GetHelpStrings ( env , info . ArgDef ) ) ;
1048
1048
1049
1049
foreach ( Argument arg in info . Args )
1050
1050
{
1051
1051
if ( ! arg . IsHidden )
1052
- strings . Add ( GetHelpStrings ( ectx , arg ) ) ;
1052
+ strings . Add ( GetHelpStrings ( env , arg ) ) ;
1053
1053
}
1054
1054
1055
1055
if ( showRsp )
@@ -1058,9 +1058,9 @@ private static ArgumentHelpStrings[] GetAllHelpStrings(IExceptionContext ectx, A
1058
1058
return strings . ToArray ( ) ;
1059
1059
}
1060
1060
1061
- private static ArgumentHelpStrings GetHelpStrings ( IExceptionContext ectx , Argument arg )
1061
+ private ArgumentHelpStrings GetHelpStrings ( IHostEnvironment env , Argument arg )
1062
1062
{
1063
- return new ArgumentHelpStrings ( arg . GetSyntaxHelp ( ) , arg . GetFullHelpText ( ectx ) ) ;
1063
+ return new ArgumentHelpStrings ( arg . GetSyntaxHelp ( ) , arg . GetFullHelpText ( env , this ) ) ;
1064
1064
}
1065
1065
1066
1066
private bool LexFileArguments ( string fileName , out string [ ] arguments )
@@ -1994,7 +1994,7 @@ private bool ParseValue(CmdParser owner, string data, out object value)
1994
1994
return false ;
1995
1995
}
1996
1996
1997
- private void AppendHelpValue ( IExceptionContext ectx , StringBuilder builder , object value )
1997
+ private void AppendHelpValue ( IHostEnvironment env , CmdParser owner , StringBuilder builder , object value )
1998
1998
{
1999
1999
if ( value == null )
2000
2000
builder . Append ( "{}" ) ;
@@ -2006,19 +2006,19 @@ private void AppendHelpValue(IExceptionContext ectx, StringBuilder builder, obje
2006
2006
foreach ( object o in ( System . Array ) value )
2007
2007
{
2008
2008
builder . Append ( pre ) ;
2009
- AppendHelpValue ( ectx , builder , o ) ;
2009
+ AppendHelpValue ( env , owner , builder , o ) ;
2010
2010
pre = ", " ;
2011
2011
}
2012
2012
}
2013
2013
else if ( value is IComponentFactory )
2014
2014
{
2015
2015
string name ;
2016
- var catalog = ModuleCatalog . CreateInstance ( ectx ) ;
2016
+ var catalog = owner . _catalog . Value ;
2017
2017
var type = value . GetType ( ) ;
2018
2018
bool success = catalog . TryGetComponentShortName ( type , out name ) ;
2019
2019
Contracts . Assert ( success ) ;
2020
2020
2021
- var settings = GetSettings ( ectx , value , Activator . CreateInstance ( type ) ) ;
2021
+ var settings = GetSettings ( env , value , Activator . CreateInstance ( type ) ) ;
2022
2022
builder . Append ( name ) ;
2023
2023
if ( ! string . IsNullOrWhiteSpace ( settings ) )
2024
2024
{
@@ -2035,21 +2035,21 @@ private void AppendHelpValue(IExceptionContext ectx, StringBuilder builder, obje
2035
2035
}
2036
2036
2037
2037
// If value differs from the default, appends the setting to sb.
2038
- public void AppendSetting ( IExceptionContext ectx , StringBuilder sb , object value , SettingsFlags flags )
2038
+ public void AppendSetting ( IHostEnvironment env , StringBuilder sb , object value , SettingsFlags flags )
2039
2039
{
2040
2040
object def = DefaultValue ;
2041
2041
if ( ! IsCollection )
2042
2042
{
2043
2043
if ( value == null )
2044
2044
{
2045
2045
if ( def != null || IsRequired )
2046
- AppendSettingCore ( ectx , sb , "" , flags ) ;
2046
+ AppendSettingCore ( env , sb , "" , flags ) ;
2047
2047
}
2048
2048
else if ( def == null || ! value . Equals ( def ) )
2049
2049
{
2050
2050
var buffer = new StringBuilder ( ) ;
2051
- if ( ! ( value is IComponentFactory ) || ( GetString ( ectx , value , buffer ) != GetString ( ectx , def , buffer ) ) )
2052
- AppendSettingCore ( ectx , sb , value , flags ) ;
2051
+ if ( ! ( value is IComponentFactory ) || ( GetString ( env , value , buffer ) != GetString ( env , def , buffer ) ) )
2052
+ AppendSettingCore ( env , sb , value , flags ) ;
2053
2053
}
2054
2054
return ;
2055
2055
}
@@ -2076,10 +2076,10 @@ public void AppendSetting(IExceptionContext ectx, StringBuilder sb, object value
2076
2076
}
2077
2077
2078
2078
foreach ( object x in vals )
2079
- AppendSettingCore ( ectx , sb , x , flags ) ;
2079
+ AppendSettingCore ( env , sb , x , flags ) ;
2080
2080
}
2081
2081
2082
- private void AppendSettingCore ( IExceptionContext ectx , StringBuilder sb , object value , SettingsFlags flags )
2082
+ private void AppendSettingCore ( IHostEnvironment env , StringBuilder sb , object value , SettingsFlags flags )
2083
2083
{
2084
2084
if ( sb . Length > 0 )
2085
2085
sb . Append ( " " ) ;
@@ -2100,11 +2100,11 @@ private void AppendSettingCore(IExceptionContext ectx, StringBuilder sb, object
2100
2100
else if ( value is bool )
2101
2101
sb . Append ( ( bool ) value ? "+" : "-" ) ;
2102
2102
else if ( IsCustomItemType )
2103
- AppendCustomItem ( ectx , _infoCustom , value , flags , sb ) ;
2103
+ AppendCustomItem ( env , _infoCustom , value , flags , sb ) ;
2104
2104
else if ( IsComponentFactory )
2105
2105
{
2106
2106
var buffer = new StringBuilder ( ) ;
2107
- sb . Append ( GetString ( ectx , value , buffer ) ) ;
2107
+ sb . Append ( GetString ( env , value , buffer ) ) ;
2108
2108
}
2109
2109
else
2110
2110
sb . Append ( value . ToString ( ) ) ;
@@ -2129,7 +2129,7 @@ private void ExtractTag(object value, out string tag, out object newValue)
2129
2129
2130
2130
// If value differs from the default, return the string representation of 'value',
2131
2131
// or an IEnumerable of string representations if 'value' is an array.
2132
- public IEnumerable < string > GetSettingStrings ( IExceptionContext ectx , object value , StringBuilder buffer )
2132
+ public IEnumerable < string > GetSettingStrings ( IHostEnvironment env , object value , StringBuilder buffer )
2133
2133
{
2134
2134
object def = DefaultValue ;
2135
2135
@@ -2138,12 +2138,12 @@ public IEnumerable<string> GetSettingStrings(IExceptionContext ectx, object valu
2138
2138
if ( value == null )
2139
2139
{
2140
2140
if ( def != null || IsRequired )
2141
- yield return GetString ( ectx , value , buffer ) ;
2141
+ yield return GetString ( env , value , buffer ) ;
2142
2142
}
2143
2143
else if ( def == null || ! value . Equals ( def ) )
2144
2144
{
2145
- if ( ! ( value is IComponentFactory ) || ( GetString ( ectx , value , buffer ) != GetString ( ectx , def , buffer ) ) )
2146
- yield return GetString ( ectx , value , buffer ) ;
2145
+ if ( ! ( value is IComponentFactory ) || ( GetString ( env , value , buffer ) != GetString ( env , def , buffer ) ) )
2146
+ yield return GetString ( env , value , buffer ) ;
2147
2147
}
2148
2148
yield break ;
2149
2149
}
@@ -2171,10 +2171,10 @@ public IEnumerable<string> GetSettingStrings(IExceptionContext ectx, object valu
2171
2171
}
2172
2172
2173
2173
foreach ( object x in vals )
2174
- yield return GetString ( ectx , x , buffer ) ;
2174
+ yield return GetString ( env , x , buffer ) ;
2175
2175
}
2176
2176
2177
- private string GetString ( IExceptionContext ectx , object value , StringBuilder buffer )
2177
+ private string GetString ( IHostEnvironment env , object value , StringBuilder buffer )
2178
2178
{
2179
2179
if ( value == null )
2180
2180
return "{}" ;
@@ -2192,12 +2192,13 @@ private string GetString(IExceptionContext ectx, object value, StringBuilder buf
2192
2192
if ( value is IComponentFactory )
2193
2193
{
2194
2194
string name ;
2195
- var catalog = ModuleCatalog . CreateInstance ( ectx ) ;
2195
+ // TODO: ModuleCatalog should be on env
2196
+ var catalog = ModuleCatalog . CreateInstance ( env ) ;
2196
2197
var type = value . GetType ( ) ;
2197
2198
bool isModuleComponent = catalog . TryGetComponentShortName ( type , out name ) ;
2198
2199
if ( isModuleComponent )
2199
2200
{
2200
- var settings = GetSettings ( ectx , value , Activator . CreateInstance ( type ) ) ;
2201
+ var settings = GetSettings ( env , value , Activator . CreateInstance ( type ) ) ;
2201
2202
buffer . Clear ( ) ;
2202
2203
buffer . Append ( name ) ;
2203
2204
if ( ! string . IsNullOrWhiteSpace ( settings ) )
@@ -2214,14 +2215,14 @@ private string GetString(IExceptionContext ectx, object value, StringBuilder buf
2214
2215
}
2215
2216
else
2216
2217
{
2217
- throw ectx . Except ( $ "IComponentFactory instances either need to be EntryPointComponents or implement { nameof ( ICommandLineComponentFactory ) } .") ;
2218
+ throw env . Except ( $ "IComponentFactory instances either need to be EntryPointComponents or implement { nameof ( ICommandLineComponentFactory ) } .") ;
2218
2219
}
2219
2220
}
2220
2221
2221
2222
return value . ToString ( ) ;
2222
2223
}
2223
2224
2224
- public string GetFullHelpText ( IExceptionContext ectx )
2225
+ public string GetFullHelpText ( IHostEnvironment env , CmdParser owner )
2225
2226
{
2226
2227
if ( IsHidden )
2227
2228
return null ;
@@ -2248,7 +2249,7 @@ public string GetFullHelpText(IExceptionContext ectx)
2248
2249
if ( builder . Length > 0 )
2249
2250
builder . Append ( " " ) ;
2250
2251
builder . Append ( "Default value:'" ) ;
2251
- AppendHelpValue ( ectx , builder , DefaultValue ) ;
2252
+ AppendHelpValue ( env , owner , builder , DefaultValue ) ;
2252
2253
builder . Append ( '\' ' ) ;
2253
2254
}
2254
2255
if ( Utils . Size ( ShortNames ) != 0 )
0 commit comments