MatthiWare.CommandLineParser 0.1.2

Command Line Parser for .Net Core written in .Net Standard.

     Configuration is done using an option class and/or fluent api.
     This library allows to add commands with their own set of options as well.

     For sample app and documentation visit: https://github.com/MatthiWare/CommandLineParser.Core

There is a newer version of this package available.
See the version list below for details.
Install-Package MatthiWare.CommandLineParser -Version 0.1.2
dotnet add package MatthiWare.CommandLineParser --version 0.1.2
<PackageReference Include="MatthiWare.CommandLineParser" Version="0.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MatthiWare.CommandLineParser --version 0.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

CommandLineParser

A simple, light-weight and strongly typed commandline parser made in .Net standard.

Configuration

Using model class with attributes
using MatthiWare.CommandLine;
using MatthiWare.CommandLine.Core.Attributes;

static int Main(string[] args)
{
    var parser = new CommandLineParser<OptionsModel>();

    var parseResult = parser.Parse(args);

    if (result.HasErrors)
    {
        Console.Error.WriteLine(result.Error);
        Console.ReadKey();

        return -1;
    }

    var options = result.Result;

    Console.WriteLine($"Message: {options.Message}");
    Console.WriteLine($"Port: {options.Port}");

    Console.ReadKey();

    return 0;
}

public class OptionsModel
{
    [Required, Name("-m", "--message")]
    public string Message { get; set; } // Mandatory

    [Name("-p", "--port"), DefaultValue(8080)]
    public int Port { get; set; } // Optional
}

Warning: Attributes will be overwritten by fluent api if both are configured

Using model class and Fluent API
using MatthiWare.CommandLine;

static int Main(string[] args)
{
    var parser = new CommandLineParser<OptionsModel>();

    parser.Configure(opt => opt.Message)
            .Name("-m", "--message")
            .Required();

        parser.Configure(opt => opt.Port)
            .Name("-p", "--port")
            .Default(8080);

    var parseResult = parser.Parse(args);

    if (result.HasErrors)
    {
        Console.Error.WriteLine(result.Error);
        Console.ReadKey();

        return -1;
    }

    var options = result.Result;

    Console.WriteLine($"Message: {options.Message}");
    Console.WriteLine($"Port: {options.Port}");

    Console.ReadKey();

    return 0;
}

public class OptionsModel
{
    public string Message { get; set; } // Mandatory

    public int Port { get; set; } // Optional
}

Warning: Attributes will be overwritten by fluent api if both are configured

Commands

using MatthiWare.CommandLine;

static int Main(string[] args)
{
    var parser = new CommandLineParser<OptionsModel>();

    parser.Configure(opt => opt.Message)
            .Name("-m", "--message")
            .Required();

        parser.Configure(opt => opt.Port)
            .Name("-p", "--port")
            .Default(8080);

    var parseResult = parser.Parse(args);

    var startCmd = parser.AddCommand<CommandOptions>()
        .Name("-s", "--start")
        .Required()
        .OnExecuting(parsedCmdOption => Console.WriteLine($"Starting server using verbose option: {parsedCmdOption.Verbose}"));

    startCmd.Configure(cmd => cmd.Verbose) // configures the command options can also be done using attributes
        .Required()
        .Name("-v", "--verbose");

    if (result.HasErrors)
    {
        Console.Error.WriteLine(result.Error);
        Console.ReadKey();

        return -1;
    }

    foreach (var cmdResult in result.CommandResults)
    {
        cmdResult.ExecuteCommand(); // executes the command handler that is configured above. 
    }

    var options = result.Result;

    Console.WriteLine($"Message: {options.Message}");
    Console.WriteLine($"Port: {options.Port}");

    Console.ReadKey();

    return 0;
}

public class OptionsModel
{
    public string Message { get; set; } // Mandatory

    public int Port { get; set; } // Optional
}

public class CommandOptions
{
    public bool Verbose { get; set; }
}

CommandLineParser

A simple, light-weight and strongly typed commandline parser made in .Net standard.

Configuration

Using model class with attributes
using MatthiWare.CommandLine;
using MatthiWare.CommandLine.Core.Attributes;

static int Main(string[] args)
{
    var parser = new CommandLineParser<OptionsModel>();

    var parseResult = parser.Parse(args);

    if (result.HasErrors)
    {
        Console.Error.WriteLine(result.Error);
        Console.ReadKey();

        return -1;
    }

    var options = result.Result;

    Console.WriteLine($"Message: {options.Message}");
    Console.WriteLine($"Port: {options.Port}");

    Console.ReadKey();

    return 0;
}

public class OptionsModel
{
    [Required, Name("-m", "--message")]
    public string Message { get; set; } // Mandatory

    [Name("-p", "--port"), DefaultValue(8080)]
    public int Port { get; set; } // Optional
}

Warning: Attributes will be overwritten by fluent api if both are configured

Using model class and Fluent API
using MatthiWare.CommandLine;

static int Main(string[] args)
{
    var parser = new CommandLineParser<OptionsModel>();

    parser.Configure(opt => opt.Message)
            .Name("-m", "--message")
            .Required();

        parser.Configure(opt => opt.Port)
            .Name("-p", "--port")
            .Default(8080);

    var parseResult = parser.Parse(args);

    if (result.HasErrors)
    {
        Console.Error.WriteLine(result.Error);
        Console.ReadKey();

        return -1;
    }

    var options = result.Result;

    Console.WriteLine($"Message: {options.Message}");
    Console.WriteLine($"Port: {options.Port}");

    Console.ReadKey();

    return 0;
}

public class OptionsModel
{
    public string Message { get; set; } // Mandatory

    public int Port { get; set; } // Optional
}

Warning: Attributes will be overwritten by fluent api if both are configured

Commands

using MatthiWare.CommandLine;

static int Main(string[] args)
{
    var parser = new CommandLineParser<OptionsModel>();

    parser.Configure(opt => opt.Message)
            .Name("-m", "--message")
            .Required();

        parser.Configure(opt => opt.Port)
            .Name("-p", "--port")
            .Default(8080);

    var parseResult = parser.Parse(args);

    var startCmd = parser.AddCommand<CommandOptions>()
        .Name("-s", "--start")
        .Required()
        .OnExecuting(parsedCmdOption => Console.WriteLine($"Starting server using verbose option: {parsedCmdOption.Verbose}"));

    startCmd.Configure(cmd => cmd.Verbose) // configures the command options can also be done using attributes
        .Required()
        .Name("-v", "--verbose");

    if (result.HasErrors)
    {
        Console.Error.WriteLine(result.Error);
        Console.ReadKey();

        return -1;
    }

    foreach (var cmdResult in result.CommandResults)
    {
        cmdResult.ExecuteCommand(); // executes the command handler that is configured above. 
    }

    var options = result.Result;

    Console.WriteLine($"Message: {options.Message}");
    Console.WriteLine($"Port: {options.Port}");

    Console.ReadKey();

    return 0;
}

public class OptionsModel
{
    public string Message { get; set; } // Mandatory

    public int Port { get; set; } // Optional
}

public class CommandOptions
{
    public bool Verbose { get; set; }
}

Release Notes

Adds documentation.

Dependencies

This package has no dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.2.4 46 11/5/2019
0.2.3 144 7/21/2019
0.2.2 163 3/2/2019
0.2.1 155 1/18/2019
0.2.0 165 1/12/2019
0.2.0-alpha 115 12/30/2018
0.1.3 166 12/10/2018
0.1.2 142 12/3/2018
0.1.1 132 11/28/2018
Show less