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

Feature Request: Cross object property binding for parser. #105

Open
nathan130200 opened this issue Jan 2, 2019 · 1 comment
Open

Feature Request: Cross object property binding for parser. #105

nathan130200 opened this issue Jan 2, 2019 · 1 comment

Comments

@nathan130200
Copy link

nathan130200 commented Jan 2, 2019

Since i have two classes, i need to parse CLI args to specfic classes:

public class Class1 
{
  public bool SomeCondition { get; set; }
  public string SomeText { get; set; }
}

public class Class2 
{
  public int SomeNumber { get; set; }
}

public class RootClass
{
  public Class1 C1 { get; set; } = new Class1();
  public Class2 C2 { get; set; } = new Class2();
}

My feature request is:
Allow to parse CLI Arguments into RootClass and sub classes properties like:

var parser = new FluentCommandLineParser<RootClass>();
parser.Setup(x => x.C1.SomeCondition)
  .As("c1-some-condition")
  .SetDefault(true);

parser.Setup(x => x.C1.SomeText)
  .As("c1-some-text")
  .Required();

parser.Setup(x => x.C2.SomeNumber)
  .As("c2-some-number")
  .SetDefault(10);

Currently if i try to do this, causes exception.

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

   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at Fclp.Internals.CommandLineOption`1.Bind(ParsedOption value)
   at Fclp.FluentCommandLineParser.ParseOptions(IEnumerable`1 options, List`1 parsedOptions, CommandLineParserResult result)
   at Barberians.Program.Main(String[] args) in C:\Users\Nathan Ferreira\Desktop\Barberians\Barberians\Program.cs:line 28

Since C1 and C2 instances are not null.

In real code:

var parser = new FluentCommandLineParser<BarberiansOptions>()
	.UseOwnOptionPrefix("+")
	.MakeCaseInsensitive()
	.DisableShortOptions();

parser.Setup(x => x.Discord.Token)
	.As("ds_token")
	.Required();

var result = parser.Parse(args); // exception thrown here.
if (result.HasErrors)
{
	Console.WriteLine(result.ErrorText);
	Console.ReadKey();
}
@nathan130200 nathan130200 changed the title Feature Request: Cross object binding Feature Request: Cross object property binding for parser. Jan 2, 2019
@siywilliams
Copy link
Member

I did not see that use-case coming !

I think there needs to be a bit of extra smartness around the reflection usage as the property it is trying to set the value on does not exist on the generic type FluentCommandLineParser is constructed with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants