CommandLineApplication is a package library in C# programming language, which is used for creating command-line applications with minimal code. It provides an easy-to-use interface for parsing command-line arguments and executing commands.
Example 1: A simple command-line application that takes two numbers as command-line arguments and returns their sum.
using CommandLineApplication;
var app = new CommandLineApplication();
app.Command("add", command => { command.Description = "Adds two numbers";
var num1 = command.Argument("num1", "The first number"); var num2 = command.Argument("num2", "The second number");
command.OnExecute(() => { Console.WriteLine($"The sum of {num1.Value} and {num2.Value} is {num1.Value + num2.Value}"); }); });
app.Execute(args);
This code creates a command-line application that includes a command "add" which takes two integer arguments "num1" and "num2". On executing the command, the application returns the sum of the two numbers.
Example 2: A command-line application that generates a random password of specified length.
using CommandLineApplication;
var app = new CommandLineApplication();
app.Command("generate-password", command => { command.Description = "Generates random password of specified length";
var length = command.Argument("length", "The length of the password");
command.OnExecute(() => { const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var random = new Random(); var password = new string(Enumerable.Repeat(chars, length.Value) .Select(s => s[random.Next(s.Length)]).ToArray()); Console.WriteLine($"The generated password is: {password}"); }); });
app.Execute(args);
This code creates a command-line application that includes a command "generate-password" which takes an integer argument "length". On executing the command, the application generates a random password of the specified length using the characters in the "chars" string.
The package library used in both examples is CommandLineApplication, which can be installed via NuGet manager in Visual Studio.
C# (CSharp) CommandLineApplication - 60 examples found. These are the top rated real world C# (CSharp) examples of CommandLineApplication extracted from open source projects. You can rate examples to help us improve the quality of examples.