« Command Line Parser Library » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 25 : Ligne 25 :
</kode>
</kode>


= Parse =
= [https://github.com/commandlineparser/commandline/wiki/Getting-Started#parsing Parsing] =
<kode lang=cs>
<kode lang=cs>
static int Main(string[] args)
static int Main(string[] args)
Ligne 33 : Ligne 33 :
                     options => RunAndReturnExitCode(options),
                     options => RunAndReturnExitCode(options),
                     _ => 1);
                     _ => 1);
</kode>
== [https://github.com/commandlineparser/commandline/wiki/Getting-Started#using-mapresult-in-asyncawait Parsing with async] ==
<kode lang=cs>
public static async Task Main(string[] args)
{
    var result = Parser.Default.ParseArguments<Options>(args)
                      .MapResult(
                          async options => await RunAndReturnExitCodeAsync(options),
                          _ => Task.FromResult(1));
</kode>
</kode>



Version du 26 mars 2020 à 16:17

Documentation

Options

Cs.svg
// créer une classe dont l'instance sera passée à la méthode ParseArguments
class Options
{
    [Usage(ApplicationAlias = "My Application")]
    public static IEnumerable<Example> Examples => new List<Example>()
    {
        new Example(
            "Description of the application",
            new Options { Option1 = "Option1", Option2 = 100 })
    };

    [Option('o', "option1", Required = true, HelpText = "Option1.")]
    public string Option1 { get; set; }

    [Option('t', "option2", HelpText = "Option2.")]
    public int Option2 { get; set; }
}

Parsing

Cs.svg
static int Main(string[] args)
{
    return Parser.Default.ParseArguments<Options>(args)
                 .MapResult(
                     options => RunAndReturnExitCode(options),
                     _ => 1);

Parsing with async

Cs.svg
public static async Task Main(string[] args)
{
    var result = Parser.Default.ParseArguments<Options>(args)
                       .MapResult(
                           async options => await RunAndReturnExitCodeAsync(options),
                           _ => Task.FromResult(1));

Command line

In case of missing or unknown argument or parsing error, the errors are displayed with the usage explanation message and the application exits.
By default options --help and --version are available.
Ps.svg
# option1
-o value
-ovalue
--option1 value
--option1=value