As the command line code base grows larger, how do you encapsulate your commands, options and handlers? #2181
VigneshwarMoorthie
started this conversation in
General
Replies: 2 comments 2 replies
-
I've been deriving classes from System.CommandLine.Command and handling all the System.CommandLine specifics in those. The constructor of each such class calls SetHandler with a delegate that retrieves the argument and option values from System.CommandLine and passes them to an Invoke method in a "handler" class that does not depend on System.CommandLine at all. |
Beta Was this translation helpful? Give feedback.
2 replies
-
For this reason, we made DotMake.CommandLine which makes using System.CommandLine very easy and quick. Please check it out. /*
Command hierarchy in below example is:
TestApp
└╴level-1
└╴level-2
*/
[CliCommand(Description = "A root cli command with nested children")]
public class RootWithNestedChildrenCliCommand
{
[CliOption(Description = "Description for Option1")]
public string Option1 { get; set; } = "DefaultForOption1";
[CliArgument(Description = "Description for Argument1")]
public string Argument1 { get; set; } = "DefaultForArgument1";
public void Run(CliContext context)
{
if (context.IsEmptyCommand())
context.ShowHierarchy();
else
context.ShowValues();
}
[CliCommand(Description = "A nested level 1 sub-command")]
public class Level1SubCliCommand
{
[CliOption(Description = "Description for Option1")]
public string Option1 { get; set; } = "DefaultForOption1";
[CliArgument(Description = "Description for Argument1")]
public string Argument1 { get; set; }
public void Run(CliContext context)
{
context.ShowValues();
}
[CliCommand(Description = "A nested level 2 sub-command")]
public class Level2SubCliCommand
{
[CliOption(Description = "Description for Option1")]
public string Option1 { get; set; } = "DefaultForOption1";
[CliArgument(Description = "Description for Argument1")]
public string Argument1 { get; set; }
public void Run(CliContext context)
{
context.ShowValues();
}
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
My command line project is getting bigger and I am looking for an efficient way to separate out the commands and their handlers.
My plan was to create a class that inherits the command base class to create a command.
And then create another class which implements the command handler interface.
However I delcare the option in the command class and cannot retrieve it during the GetValueForOption method call in the command handler class?
Beta Was this translation helpful? Give feedback.
All reactions