Add RootCommand
Now that we have the package included in our project we need to add it to our console application.
Open Program.cs
and add using System.CommandLine
to the top of the file.
And just so we can be sure we are using the package we will create a RootCommand
that does nothing at this point.
Our file should now look like this.
using System;
using System.CommandLine;
namespace SimpleCmdLine
{
class Program
{
static void Main(string[] args)
{
var rootCommand = new RootCommand();
Console.WriteLine("Hello World!");
}
}
}
It doesn't do anything new particularly. Just to make sure we didn't mess anything up we can make sure it runs properly.
$ dotnet build
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
Restored /Users/iesoftwaredeveloer/repos/SimpleCmdLine/src/SimpleCmdLine.csproj (in 163 ms).
SimpleCmdLine -> /Users/iesoftwaredeveloper/repos/SimpleCmdLine/src/bin/Debug/net5.0/SimpleCmdLine.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.61
$ dotnet run
Hello World!
$
Phew, Success! If something had gone wrong at this point it would be a long day.
Using default options
When creating a command line parser there are a couple of options that
Now we can add a simple option to see how we can add an option that takes a single argument.
Modify the Program.cs
to now include the following.
using System.CommandLine.Invocation;
Add a description for the root command.
var rootCommand = new RootCommand()
{
Description = "Console app to demonstrate System.CommandLine"
};
And then create a handler for the root command.
public static void RootCmd()
{
Console.WriteLine("Hello World!");
}
Assign the handler and invoke it.
rootCommand.Handler = CommandHandler.Create(RootCmd);
return rootCommand.Invoke(args);
After all of the changes to Program.cs
here is how it looks now.
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
namespace SimpleCmdLine
{
class Program
{
static int Main(string[] args)
{
var rootCommand = new RootCommand()
{
Description = "Console app to demonstrate System.CommandLine"
};
rootCommand.Handler = CommandHandler.Create(RootCmd);
return rootCommand.Invoke(args);
}
public static void RootCmd()
{
Console.WriteLine("Hello World!");
}
}
}
If we now execute the application with dotnet run
we get...
$ dotnet run
Hello World!
Don't fret. This is exactly what we were expecting. We didn't really do much other than change the way that we execute the application. It is now invoked from the handler assigned by the RootCommand instead of within Main
.
So what did we gain? Not much yet, but we do get some free options now. Let's execute dotnet run -- --help
and see what we get.
$ dotnet run -- --help
SimpleCmdLine
Console app to demonstrate System.CommandLine
Usage:
SimpleCmdLine [options]
Options:
--version Show version information
-?, -h, --help Show help and usage information
$
We did't add any --help
option in our code. CommandLine provides this option as part of the functionality. There are a few other items of note in the output.
The description of the application is output along with the name of the application. There is a usage line that describes how to format the command.
Under the Options
we also have another option that is added for us. We can ask it to show the version information.
$ dotnet run -- --version
1.0.0
$
You must be logged in to see the comments. Log in now!