When creating a console application you often want to be able to use command line arguments to control what the application does.
In this post we explore how we can use System.CommandLine to build a command line application.
System.CommandLine is an open source .net command line parser.
As of this writing the package is in pre-release with version 2.0.0-beta1.21216.1. This means that to install it you will need to include the --prerelease
flag when installing with dotnet add package
.
There are a number of features in this tool and I encourage you to visit the NuGet page or the GitHub site for the latest information.
Once you have configured the command line with the available commands, options, and arguments you can run your application and pass in the arguments. CommandLine will parse the tokens provided on the command line and evaluate them against the configured command line.
Create new project from template
Using the dotnet new
command, create a application based upon the console template.
$ dotnet new console --output SimpleCmdLine/src --name SimpleCmdLine
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on SimpleCmdLine/src/SimpleCmdLine.csproj...
Determining projects to restore...
Restored /Users/iesoftwaredeveloper/repos/SimpleCmdLine/src/SimpleCmdLine.csproj (in 66 ms).
Restore succeeded.
$
Once the dotnet new
command has completed you will should have a new directory called SimpleCmdLine
in the current directory. Change to the new directory and then the src
directory and then list the files that were created.
$ cd SimpleCmdLine
$ cd src
$ ls
Program.cs SimpleCmdLine.csproj
$
You can see that the template created two files. The first is the project file SimpleCmdLine.csproj
and the other is the main file for the project called Program.cs
.
Initialize source repository
Now is a good time to initialize your source repository. If you need more information on how to do this you can read Initialize a Source Code Repository Using git and dotnet
Add an initial tag
Now that we have added the base template code to the repository I want to quickly tag it so that we can easily return to this point in the code if we want to. There are a number of ideas on how to name a tag. You can use whatever format you find useful. I am going to use the tag "initial" to indicate that this is the intial base code. After creating a tag it is not automatically pushed to a remote. We will verify that the tag was created and then push it to the remote.
$ git tag -a initial -m "Initial dotnet console template"
$ git tag
initial
$ git push origin --tags
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 179 bytes | 179.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:iesoftwaredeveloper/SimpleCmdLine.git
* [new tag] initial -> initial
$
The repository is now in a great initial state. The console project is all base code that is provided to us by the dotnet template and the repository has one branch (main
) and one tag (initial
) to preserve it in all of its glory for posterity.
Verify Initial build and run
Before we go any further we should make sure that the code we have will build and run successfully. There is not reason it should not since this is just boilerplate template code. Ideally you would do this before you commit and push to a repository. Since I am confident the template was going to work without issues I created the repository before verifying.
# cd to the directory with the source.
$ cd src
$ dotnet build
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
SimpleCmdLine -> /Users/iesoftwaredeveloper/repos/SimpleCmdLine/src/bin/Debug/net5.0/SimpleCmdLine.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.97
$ dotnet run
Hello World!
$
The build completed without any warnings or errors. That is a good sign. Running the application also works as expected. We are off to a good start.
You must be logged in to see the comments. Log in now!