Enables two-way serialization for the CommandLineParser library, allowing an options object to be serialized back into a command-line string.
$ dotnet add package OutWit.Common.CommandLineOutWit.Common.CommandLine is a lightweight library designed to simplify serialization and deserialization of command-line arguments in .NET applications. By leveraging attributes from the CommandLine library, it enables seamless mapping of object properties to command-line options and vice versa.
The library allows converting objects into command-line argument strings based on the OptionAttribute.
using OutWit.Common.CommandLine;
using CommandLine;
public class Options
{
[Option('n', "name", Required = true, HelpText = "The name of the user.")]
public string Name { get; set; } = string.Empty;
[Option('a', "age", HelpText = "The age of the user.")]
public int Age { get; set; }
[Option('v', "verbose", HelpText = "Enable verbose mode.")]
public bool Verbose { get; set; }
}
var options = new Options
{
Name = "John",
Age = 30,
Verbose = true
};
string commandLine = options.SerializeCommandLine();
Console.WriteLine(commandLine);
// Output: --name John --age 30 --verbose
Easily parse command-line arguments into strongly typed objects.
string[] args = { "--name", "John", "--age", "30", "--verbose" };
var options = args.DeserializeCommandLine<Options>();
Console.WriteLine(options.Name); // Output: John
Console.WriteLine(options.Age); // Output: 30
Console.WriteLine(options.Verbose); // Output: True
The library uses OptionAttribute from the CommandLine library to define mappings between object properties and command-line options. Supported configurations include:
Install the package via NuGet:
Install-Package OutWit.Common.CommandLine
SerializeCommandLinepublic static string SerializeCommandLine<T>(this T me)
OptionAttribute annotations.string representing the serialized command-line arguments.DeserializeCommandLinepublic static T DeserializeCommandLine<T>(this string me)
T.T populated with parsed values.public static T DeserializeCommandLine<T>(this string[] me)
T.T populated with parsed values.Licensed under the Apache License, Version 2.0. See LICENSE.
If you use OutWit.Common.CommandLine in a product, a mention is appreciated (but not required), for example: "Powered by OutWit.Common.CommandLine (https://ratner.io/)".
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
You may not: