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

Exception when defining a property as Argument in a command and also in one of its sub commands #518

Closed
ernstc opened this issue Oct 12, 2022 · 0 comments · Fixed by #519
Labels

Comments

@ernstc
Copy link
Contributor

ernstc commented Oct 12, 2022

Describe the bug
Defining a property as Argument in a command and also in one of its sub commands, it generates an exception.

System.Reflection.TargetException : Object does not match target type.

  Stack Trace: 
RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
MethodBase.Invoke(Object obj, Object[] parameters)
<>c__DisplayClass2_0.<GetPropertyGetter>b__0(Object obj)
<>c__DisplayClass1_0.<AddArgument>b__1(ParseResult r)
CommandLineApplication.HandleParseResult(ParseResult parseResult)
CommandLineApplication.HandleParseResult(ParseResult parseResult)
CommandLineApplication.Parse(String[] args)
CommandLineApplication.ExecuteAsync(String[] args, CancellationToken cancellationToken)
CommandLineApplication.ExecuteAsync[TApp](CommandLineContext context, CancellationToken cancellationToken)
CommandLineApplication.Execute[TApp](CommandLineContext context)
CommandLineApplication.Execute[TApp](IConsole console, String[] args)
CommandLineApplication.Execute[TApp](String[] args)

To Reproduce
Steps to reproduce the behavior:

  1. Using the version '4.0.1' of the library
  2. Sample code
using McMaster.Extensions.CommandLineUtils;

[Command("console-app")]
[Subcommand(typeof(ConfigureCommand))]
public class Program
{
    static void Main(string[] args) => CommandLineApplication.Execute<Program>(args);
    public int OnExecute() => 1;
}

[Command]
[Subcommand(typeof(ListCommand))]
public class ConfigureCommand
{
    [Argument(0)]
    public string? ArgumentProperty { get; set; }
    public int OnExecute() { Console.WriteLine($"configure: {ArgumentProperty}"); return 0; }
}

[Command]
public class ListCommand
{
    [Argument(0)]
    public string? ArgumentProperty { get; set; }
    public int OnExecute() { Console.WriteLine($"list: {ArgumentProperty}"); return 0; }
}
  1. Run the console application with the arguments configure list any-value

Additional context
There is a work around for this bug, but only if it's possible to define a base class that contains the argument property for both commands: configure and list.

public class CommandWithArgument
{
    [Argument(0)]
    public string? ArgumentProperty { get; set; }
}

[Command]
[Subcommand(typeof(ListCommand))]
public class ConfigureCommand : CommandWithArgument
{
    public int OnExecute() { Console.WriteLine($"configure: {ArgumentProperty}"); return 0; }
}

[Command]
public class ListCommand : CommandWithArgument
{
    public int OnExecute() { Console.WriteLine($"list: {ArgumentProperty}"); return 0; }
}
@ernstc ernstc added the bug label Oct 12, 2022
@natemcmaster natemcmaster linked a pull request Nov 19, 2022 that will close this issue
natemcmaster pushed a commit that referenced this issue Nov 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant