Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The [DefaultValue] attribute does not support arrays #1163

Closed
0xced opened this issue Feb 16, 2023 · 0 comments · Fixed by #1164
Closed

The [DefaultValue] attribute does not support arrays #1163

0xced opened this issue Feb 16, 2023 · 0 comments · Fixed by #1164
Labels
bug Something isn't working needs triage

Comments

@0xced
Copy link
Contributor

0xced commented Feb 16, 2023

Information

  • OS: Any
  • Version: Spectre.Console.Cli 0.46.0
  • Terminal: Any

Describe the bug
The [DefaultValue] attribute does not support arrays.

To Reproduce
Run the following program (without passing any arguments):

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Spectre.Console;
using Spectre.Console.Cli;

var app = new CommandApp<DefaultCommand>();
app.Configure(config => config.PropagateExceptions());
app.Run(args);

internal class DefaultCommand : Command<DefaultCommand.Settings>
{
    internal class Settings : CommandSettings
    {
        [CommandOption("--arch <architecture>")]
        [DefaultValue(new[] { Architecture.X64, Architecture.Arm64 })]
        public Architecture[] Architectures { get; init; } = Array.Empty<Architecture>();
    }

    public override int Execute(CommandContext context,Settings settings)
    {
        AnsiConsole.WriteLine($"{settings.Architectures.Length} architectures: {string.Join(", ", settings.Architectures)}");
        return 0;
    }
}

Expected behavior
The following output should be printed on the console:

2 architectures: X64, Arm64

And when running with the --help option, the default values should be properly displayed:

OPTIONS:
                                 DEFAULT
    -h, --help                                 Prints help information
        --arch <ARCHITECTURE>    X64, Arm64

Actual behavior

When running without any arguments an InvalidCastException exception is thrown:

Unhandled exception. 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 45
   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 105
   at Spectre.Console.Cli.CommandExecutor.Execute(IConfiguration configuration, IEnumerable`1 args) in /_/src/Spectre.Console.Cli/Internal/CommandExecutor.cs:line 86
   at Spectre.Console.Cli.CommandApp.RunAsync(IEnumerable`1 args) in /_/src/Spectre.Console.Cli/CommandApp.cs:line 81
   at Spectre.Console.Cli.CommandApp.Run(IEnumerable`1 args) in /_/src/Spectre.Console.Cli/CommandApp.cs:line 55
   at Spectre.Console.Cli.CommandApp`1.Run(IEnumerable`1 args) in /_/src/Spectre.Console.Cli/CommandAppOfT.cs:line 38
   at Program.<Main>$(String[] args) in Program.cs:line 9

When running with the --help option the default value is useless:

OPTIONS:
                                 DEFAULT                                                                 
    -h, --help                                                                    Prints help information
        --arch <ARCHITECTURE>    System.Runtime.InteropServices.Architecture[] 

Additional context
A pull request is coming soon to address this issue.

@0xced 0xced added bug Something isn't working needs triage labels Feb 16, 2023
@github-project-automation github-project-automation bot moved this to Todo 🕑 in Spectre Console Feb 16, 2023
0xced added a commit to 0xced/spectre.console that referenced this issue Feb 16, 2023
0xced added a commit to 0xced/spectre.console that referenced this issue Mar 1, 2023
@github-project-automation github-project-automation bot moved this from Todo 🕑 to Done 🚀 in Spectre Console May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
Status: Done 🚀
Development

Successfully merging a pull request may close this issue.

1 participant