Replies: 4 comments 2 replies
-
|
Made this a discussion. You should be able to accomplish this with a |
Beta Was this translation helpful? Give feedback.
-
Could you give a |
Beta Was this translation helpful? Give feedback.
-
|
Mmh, today I found the time to check this out, but unfortunately, there is a problem: public class CommaSeparatedStringConverter : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is not string stringValue)
{
throw new NotSupportedException("Can't convert value to verbosity.");
}
return stringValue.Split(",");
}
}Just splitting the string would not work - and since there is a type change (from string to array), is this really possible with a Here is another approach - result is: Details
System.InvalidCastException: Object cannot be stored in an array of this type.
at System.Array.InternalSetValue(Object value, IntPtr flattenedIndex)
at System.Array.SetValue(Object value, Int32 index)
at Spectre.Console.Cli.CommandValueBinder.GetArray(CommandParameter parameter, Object value) in /_/src/Spectre.Console/Cli/Internal/Binding/CommandValueBinder.cs:line 88
at Spectre.Console.Cli.CommandValueBinder.Bind(CommandParameter parameter, ITypeResolver resolver, Object value) in /_/src/Spectre.Console/Cli/Internal/Binding/CommandValueBinder.cs:line 20
at Spectre.Console.Cli.CommandValueResolver.GetParameterValues(CommandTree tree, ITypeResolver resolver) in /_/src/Spectre.Console/Cli/Internal/Binding/CommandValueResolver.cs:line 88
at Spectre.Console.Cli.CommandBinder.Bind(CommandTree tree, Type settingsType, ITypeResolver resolver) in /_/src/Spectre.Console/Cli/Internal/CommandBinder.cs:line 7
at Spectre.Console.Cli.CommandExecutor.Execute(CommandTree leaf, CommandTree tree, CommandContext context, ITypeResolver resolver, IConfiguration configuration) in /_/src/Spectre.Console/Cli/Internal/CommandExecutor.cs:line 97
at Spectre.Console.Cli.CommandExecutor.Execute(IConfiguration configuration, IEnumerable`1 args) in /_/src/Spectre.Console/Cli/Internal/CommandExecutor.cs:line 78
at Spectre.Console.Cli.CommandApp.RunAsync(IEnumerable`1 args) in /_/src/Spectre.Console/Cli/CommandApp.cs:line 81
public class DumpCommandSettings : CommandSettingsBase
{
[CommandOption("--include-property")]
[TypeConverter(typeof(CommaSeparatedStringConverter<MetadataProperty>))]
public MetadataProperty[] IncludeProperties { get; set; } = Array.Empty<MetadataProperty>();
}
public class CommaSeparatedStringConverter<T> : ArrayConverter where T : struct, IConvertible
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is not string s || string.IsNullOrEmpty(s))
{
return base.ConvertFrom(context, culture, value);
}
var results = s.Split(",");
var parsedResults = new List<T>();
foreach (var result in results)
{
if (!Enum.TryParse<T>(result, true, out var parsedResult))
{
continue;
}
parsedResults.Add(parsedResult);
}
return parsedResults.ToArray();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
This really needs some proper attention. I'm trying to migrate away from CliFx to Spectre.Console, but I cannot do this: To me this seems like a basic requirement. Many POSIX CLI applications on linux support this. Just require positional arguments before any options, and this is easy to parse AFAICS. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is your feature request related to a problem? Please describe.
Let's say you can define multiple colors for an enum array:
I'm a bit frustrated, that I have to provide the option parameter 4 times, because it could be much shorter just providing the options with a separator.
Describe the solution you'd like
myapp --color="red,green,blue,yellow"Describe alternatives you've considered
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions