-
Notifications
You must be signed in to change notification settings - Fork 3
How to use the library in your project
Download the Nuget package here: https://www.nuget.org/packages/CommandCore.Library. You can also use the following commands to download using various Nuget clients:
Using Powershell
Install-Package CommandCore.Library -Version <package-version>
Using dotnet CLI
dotnet add package CommandCore.Library --version <package-version>
CommandCore.Library
is the main package. However, it depends on another package called CommandCore.LightIoC
, which provides IoC capabilities such as dependency injection, and it is part of CommandCore project for now, and they are both versioned in the same way until CommandCore.LightIoC
is extracted into a separate Github repo; and after that, they will be versioned and maintained separately. When you download CommanddCore.Library
, Nuget will download the dependant libraries automatically for you.
CommandCore is meant to be simple to start off with. Therefore, there won't be much to learn about it. The following example will explain everything you need to know to get started and be proficient with this library. You can also download CommandCore.TestConsole
application to run your first application. The source codes of that project can be found in the library.
So you have downloaded the dependencies and let's start with a simple command: (Note: CommandCore treats the subcommands (verb) and option name in a case sensitive fashion. Hence, --name and --Name are not the same things. Check out the sample code below to read more on this.
helloworld.exe add --firstname tarik --lastname guney --haslicense -a 33
If we dissect the command call above, these are the pieces of it:
-
add
: The verb of the command. It is also known assubcommand
. -
--name
and--lastname
: These are the parameter names, which are also known as options. -
tarik
andguney
: These are the arguments or parameter values. -
-a
is an alias for--age
, so you can either specify the age with--age 33
or-a 33
-
--haslicense
is a flag, which by default has a value oftrue
. You don't need to specify its values explicitly.
When it comes to parameter names, CommandCore follows a strict format. You must use --
suffix for the parameter names or -
for the parameter aliases as shown in the sample call above.
You can represent and parse the command above with the following classes:
// VerbName attribute is not required and may be omitted. However, then the name of the class name,
// which is *Add* here must match the verb passed as part of the command line arguments,
// and it is case sensitive, meaning that Add and add are two different commands.
// This may change in the future, for now, this is how it works.
[VerbName("add")]
public class Add : VerbBase<AddOptions>
{
public VerbView Run(){
return AddView(Options);
}
}
public class AddView : VerbViewBase<AddOptions>
{
public override void RenderResponse(){
Console.WriteLine(
$"FirstName: {_options!.FirstName}\n" +
$"Last Name: {_options!.LastName}\n" +
$"Has License: {_options!.HasLicense}\n" +
$"Age: {_options.Age}");
}
}
public class AddOptions : VerbOptionsBase
{
// OptionName attribute is optional and if it does not exist, the name of the property will be used by default.
// However, option names are case sensitive so firstname and FirstName are not the same things.
[OptionName("firstname")]
public string FirstName {get;set;}
[OptionName("lastname")]
public string LastName {get;set;}
// CommandCore supports various types like Boolean, and it automatically
// converts them to their corresponding types specified with the Options properties.
[OptionName("haslicense")]
public bool? HasLicense {get;set;}
[OptionName("age", Alias="a")]
public int Age {get;set;}
}
To activate this library, you need to add the following code to the Main
function in Program.cs
file:
public static int Main(string[] args)
{
var commandCoreApp = new CommandCoreApp();
return commandCoreApp.Parse(args);
}
That's all, and it