Template for Command Line Interface (CLI) tool in C#
Download the dotnet-install.sh script
curl -sSL https://dot.net/v1/dotnet-install.sh > dotnet-install.sh
chmod +x dotnet-install.sh./dotnet-install.sh --runtime dotnet --channel 8.0 --version latest
or download and install from Microsoft
Install with dotnet-install.sh script:
./dotnet-install.sh --channel 8.0Test that you can run the dotnet CLI (command line interface)
~/.dotnet/dotnet --version
~/.dotnet/dotnet new console --helpInstall DocFX for docs generation:
dotnet tool install -g docfxConfigure project:
source configure.shOpen the project in Visual Studio Code:
code .Build:
# build
dotnet build
# create alias for easy calling
alias cli-cs=cli-cs/bin/Debug/net8.0/cli-csRun the compiled program:
echo "John" > name.txt
cli-cs greet name.txt
cli-cs greet --language es name.txt
cli-cs greet -l bg name.txtRun via the dotnet CLI without building:
echo "John" > name.txt
dotnet run --project cli-cs -- greet name.txt
dotnet run --project cli-cs -- greet --language es name.txt
dotnet run --project cli-cs -- greet -l bg name.txtdotnet test cli-cs-tests# build
docfx docfx/docfx.json
# serve
docfx docfx/docfx.json --serve --open-browserdotnet new sln --name cli-cs# add cli project
dotnet new console --name cli-cs --framework net8.0
dotnet sln add cli-cs
# add packages
dotnet add cli-cs package System.CommandLine --prerelease
# add test project
dotnet new xunit --name cli-cs-tests
dotnet sln add cli-cs-tests
# add reference to the main project
dotnet add cli-cs-tests reference cli-csmkdir -p docfx
cd docfx
docfx init --yes