How to pass a collection of objects as command option
mirsahibmovsumov opened this issue · 3 comments
mirsahibmovsumov commented
There is a CLI command like below
[Command("service bind projects")]
public class BindProjects: ICommand
{
[CommandOption("projects", IsRequired = true)]
public IList<UpdateUserProjectDto> Projects { get; set; }
[CommandOption("userId", IsRequired = true)]
public string UserId { get; set; }
public async ValueTask ExecuteAsync(IConsole console)
{
var command = new BindProjectsCommand { Projects = Projects, UserId = UserId };
// execute command and show result
}
}
public class UpdateUserProjectDto
{
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public bool IsBound { get; set; }
}
I wanna pass a list of UpdateUserProjectDto as Projects but I cannot do it.
Could you suggest to me how to add this?
Tyrrrz commented
You can either add a static Parse(string value)
method on UpdateUserProjectDto
or create a converter deriving from ArgumentValueConverter
and specify it in the attribute.
First approach is easier:
public class UpdateUserProjectDto
{
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public bool IsBound { get; set; }
public static UpdateUserProjectDto Parse(string value)
{
// Parse the value from string here
var projectId = ...;
var projectName = ...;
var isBond = ...;
return new UpdateUserProjectDto{ ProjectId = projectId, ProjectName = projectName, IsBound = isBond };
}
}
mirsahibmovsumov commented
Thanks a lot
It seems there's no generic way to pass a collection of objects
Tyrrrz commented
Nope, only if they have Parse(...)
.