-
-
Notifications
You must be signed in to change notification settings - Fork 254
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
Convention based api #31
Comments
I looked briefly at https://github.com/shovonnn/artisancommandline. From what I can see, it looks like there are a bunch of changes that would need to happen to support this. Would be good to nail down what exactly you're looking to support. Based on your sample, it seems like you would need at least the following
var app = new CommandLineApplication();
app.Use<MyProgram>();
app.Execute(args);
class Program
{
[Subcommand("delete")]
public int OnDelete()
{ }
[Subcommand("list")]
public int OnList()
{ }
}
class Program
{
public int OnExecute([Option("--name")] string name)
{ }
}
Yup...System.Reflection is a pain in netstandard1.x. Usually you have to drop down use |
@shovonnn can you provide more feedback on this issue? Did my response summarize well the things you would like added to this library? |
yeah it does. sorry i have been very busy last few days.
var res = methodInfo.Invoke(obj, handlerParams.ToArray());
if (res.GetType() == typeof(Task<int>))
{
var task = (Task<int>)res;
System.Console.CancelKeyPress += (sender, e) =>
{
cts?.Cancel();
Task.WaitAll(new[] { task }, 10000);
};
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
{
cts?.Cancel();
Task.WaitAll(new[] { task }, 10000);
};
Task.WaitAll(task);
return task.Result;
}
else
return (int)res; |
@shovonnn so, some of these conventions are ones that I plan to make default. Plus, some of these conventions would break existing apps if they were made default. That said, it is my intention to make it possible for you to write your own contentions, and let users opt-in to them. For 2.1.0, my goal was to write what I thought was a basic approach to mapping a CLR type to Here's an early example of what this might look like. For example, you could implement proposal 1 (every public property is an option) like this: public class PropertiesAsOptionsConvention : ICommandLineAppConvention
{
public void Apply(ConventionCreationContext context)
{
foreach (var prop in context.TargetType.GetProperties())
{
var option = context.Application.Option(
template: "--" + prop.Name,
description: prop.Name,
optionType: CommandOptionType.SingleValue);
context.RegisterParsingCompleteAction((instance, parseContext) =>
{
if (option.HasValue())
{
prop.SetMethod.Invoke(instance, new object[] { option.Value() });
}
});
}
}
}
[ParsingConvention(typeof(PropertiesAsOptionsConvention))]
public class Program
{
} The API needs work, so expect some churn here, but let me know what you think. |
I spent some time refactoring the way attribute binding works. Although I don't plan to implement all of the conventions suggested here, I have made API public that should allow you to implement these kinds of features yourself. Using the new type |
By the way, just to prove to myself that this convention API is flexible enough to implement this, I made this a sample. CommandLineUtils/samples/Conventions/MethodConventions.cs Lines 11 to 14 in d6ce61d
|
i have added an extension method to CommandLineApplication to support convention based command handling. see here https://github.com/shovonnn/artisancommandline
i tried to create a pull request for this repo. but it wont compile. few too many error. maybe cause of netcore1.x
i am on a tight schedule at the moment. so can't work much.
The text was updated successfully, but these errors were encountered: