Creating a Quick CLI

Deaf School Topiary Park Hours 7AM - 11PM Alcohol is prohibited clean up after your pet

I had some recent concerns that if anyone wanted to get going with using DasBlog Core in a production environment that initializing the project would require too much underlying knowledge of the application. In response I started creating a command line interface (CLI) that might be used to help configure the site, and I found this interesting CLI utility that helped me get the console interactions developed in minutes.

CommandLineUtils

CommandLineUtils is a fork of Microsoft.Extensions.CommandLineUtils, which is no longer under active development. Nate McMaster has taken this fork and appears to be active making improvements to the underlying ideas.

So in my head I want to design a command that will, lets say, create a new theme called “business”. It might look like this:

dasblog-core createtheme business

So I create a new .NET Core command line project Assembly Name defined as dasblog-core, then I installed the CommandLineUtils NuGet package into my project.

The beauty of this project is the simplicity of the code, for this one command I implemented the following:

var app = new CommandLineApplication
{
     Name = "dasblog-core",
     Description = "Configure DasBlog Core from the CLI.",
};
app.HelpOption(inherited: true); app.Command("createtheme", createthemeCmd => {
     createthemeCmd.Description = "Creates a new theme based on the default dasblog theme";
     var val = createthemeCmd.Argument("value", "Name of the new theme").IsRequired();
     createthemeCmd.OnExecute(() =>
     {
         // Execute command here...
         Console.WriteLine($"New theme created: '{val.Value}' ");
     });
});

app.OnExecute(() => {
     Console.WriteLine("Specify a subcommand");
     app.ShowHelp();
     return 1; });

Of course it also supports subcommands, for example, I also have code that allows you to execute the following:

dasblog-core config root https://www.poppastring.com

It is really straightforward! There is good documentation for API reference, and tutorials are also sample to look at here.



Comment Section

Comments are closed.