Unosquare
    Show / Hide Table of Contents

    Class ArgumentParser

    Provides methods to parse command line arguments.

    Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors).

    Inheritance
    Object
    ArgumentParser
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Swan.Parsers
    Syntax
    public class ArgumentParser
    Examples

    The following example shows how to parse CLI arguments into objects.

    class Example 
    {
        using System;
        using Swan.Parsers;
    
        static void Main(string[] args)
        {
            // parse the supplied command-line arguments into the options object
            var res = Runtime.ArgumentParser.ParseArguments(args, out var options);
        }
    
        class Options
        {
            [ArgumentOption('v', "verbose", HelpText = "Set verbose mode.")]
            public bool Verbose { get; set; }
    
            [ArgumentOption('u', Required = true, HelpText = "Set user name.")]
            public string Username { get; set; }
    
            [ArgumentOption('n', "names", Separator = ',',
            Required = true, HelpText = "A list of files separated by a comma")]
            public string[] Files { get; set; }
    
            [ArgumentOption('p', "port", DefaultValue = 22, HelpText = "Set port.")]
            public int Port { get; set; }
    
            [ArgumentOption("color", DefaultValue = ConsoleColor.Red,
            HelpText = "Set a color.")]
            public ConsoleColor Color { get; set; }
        }
    }

    The following code describes how to parse CLI verbs.

    class Example2 
    {
        using Swan;
        using Swan.Parsers;
    
        static void Main(string[] args)
        {
            // create an instance of the VerbOptions class
            var options = new VerbOptions();
    
            // parse the supplied command-line arguments into the options object
            var res = Runtime.ArgumentParser.ParseArguments(args, options);
    
            // if there were no errors parsing
            if (res)
            {
                if(options.Run != null)
                {
                    // run verb was selected
                }
    
                if(options.Print != null)
                {
                    // print verb was selected
                }
            }
    
            // flush all error messages
            Terminal.Flush();
        }
    
        class VerbOptions
        {
            [VerbOption("run", HelpText = "Run verb.")]
            public RunVerbOption Run { get; set; }
    
            [VerbOption("print", HelpText = "Print verb.")]
            public PrintVerbOption Print { get; set; }
        }
    
        class RunVerbOption
        {
            [ArgumentOption('o', "outdir", HelpText = "Output directory",
            DefaultValue = "", Required = false)]
            public string OutDir { get; set; }
        }
    
        class PrintVerbOption
        {
            [ArgumentOption('t', "text", HelpText = "Text to print",
            DefaultValue = "", Required = false)]
            public string Text { get; set; }
        }
    }

    Constructors

    ArgumentParser()

    Initializes a new instance of the ArgumentParser class.

    Declaration
    public ArgumentParser()

    ArgumentParser(ArgumentParserSettings)

    Initializes a new instance of the ArgumentParser class, configurable with ArgumentParserSettings using a delegate.

    Declaration
    public ArgumentParser(ArgumentParserSettings parseSettings)
    Parameters
    Type Name Description
    ArgumentParserSettings parseSettings

    The parse settings.

    Properties

    Current

    Gets the current.

    Declaration
    public static ArgumentParser Current { get; }
    Property Value
    Type Description
    ArgumentParser

    The current.

    Settings

    Gets the instance that implements ArgumentParserSettings in use.

    Declaration
    public ArgumentParserSettings Settings { get; }
    Property Value
    Type Description
    ArgumentParserSettings

    The settings.

    Methods

    ParseArguments<T>(IEnumerable<String>, T)

    Parses a string array of command line arguments constructing values in an instance of type T.

    Declaration
    public bool ParseArguments<T>(IEnumerable<string> args, T instance)
    Parameters
    Type Name Description
    IEnumerable<String> args

    The arguments.

    T instance

    The instance.

    Returns
    Type Description
    Boolean

    true if was converted successfully; otherwise, false.

    Type Parameters
    Name Description
    T

    The type of the options.

    ParseArguments<T>(IEnumerable<String>, out T)

    Parses a string array of command line arguments constructing values in an instance of type T.

    Declaration
    public bool ParseArguments<T>(IEnumerable<string> args, out T instance)
    Parameters
    Type Name Description
    IEnumerable<String> args

    The arguments.

    T instance

    The instance.

    Returns
    Type Description
    Boolean

    true if was converted successfully; otherwise, false.

    Type Parameters
    Name Description
    T

    The type of the options.

    Comments

    Back to top Copyright © 2017-2019 Unosquare