Skip to content

Option Ordering

Matthias Beerens edited this page Apr 11, 2021 · 1 revision

Option Ordering

Order options in the command line

Configure

Add the OptionOrder attribute or Order Fluent API Method.

Options are sorted from lowest to highest.

Example using attributes

using MatthiWare.CommandLine;
using MatthiWare.CommandLine.Core.Attributes;

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

    var result = parser.Parse(args);

    var options = result.Result;

    Console.WriteLine($"Input: {options.InputPath}");
    Console.WriteLine($"Output: {options.OutputPath}");

    Console.ReadKey();
}

public class OrderedOptions
{
    [OptionOrder(0)]
    public string InputPath { get; set; }

    [OptionOrder(1)]
    public string OutputPath { get; set; }
}

Example using Fluent API

using MatthiWare.CommandLine;
using MatthiWare.CommandLine.Core.Attributes;

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

    parser
        .Configure(_ => _.InputPath)
        .Order(0);

    parser
        .Configure(_ => _.OutputPath)
        .Order(1);

    var result = parser.Parse(args);

    var options = result.Result;

    Console.WriteLine($"Input: {options.InputPath}");
    Console.WriteLine($"Output: {options.OutputPath}");

    Console.ReadKey();
}

public class OrderedOptions
{
    public string InputPath { get; set; }

    public string OutputPath { get; set; }
}

Usage

Input:
> Example.exe "input path" "output path"

Output:
Input: Input path
Output: output path