Question

Je lis cette question: ligne de commande Parser pour .NET .

Je pensais que c'était ce que je cherchais, mais la bibliothèque ligne de commande Parser Library n'est pas Compact cadre convivial ...

vraiment ne veulent pas écrire un analyseur CL et je suis se détourne du but réel de ma petite application à cause de ce procès malheureux.

Quelqu'un sait d'une bibliothèque qui correspond à la structure compacte? (De préférence avec la simplicité et la fonctionnalité comme celui mentionné ci-dessus)
Peu importe si la version 2 ou 3,5

Était-ce utile?

La solution

est ce que j'utilise. Je l'ai emprunté quelque part, mais ne savez pas où:

using System.Collections.Specialized;
using System.Text.RegularExpressions;

/// <summary>
/// Parses the command line arguments into a name/value collection
/// </summary>
public class CommandLineArgumentParser
{
    #region Fields
    private StringDictionary parameters;
    #endregion

    #region Constructors
    /// <summary>
    ///     Initializes a new instance of the <see cref="CommandLineArgumentParser"/> class.
    /// </summary>
    /// <param name="args">command-line arguments
    /// </param>
    public CommandLineArgumentParser(string[] args)
    {
        this.parameters = new StringDictionary();
        Regex spliter = new Regex(@"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        Regex remover = new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        string parameter = null;
        string[] parts;

        // Valid parameters forms:
        // {-,/,--}param{ ,=,:}((",')value(",'))
        // Examples: 
        // -param1 value1 --param2 /param3:"Test-:-work" 
        //   /param4=happy -param5 '--=nice=--'
        foreach (string txt in args)
        {
            // Look for new parameters (-,/ or --) and a
            // possible enclosed value (=,:)
            parts = spliter.Split(txt, 3);

            switch (parts.Length)
            {
                // Found a value (for the last parameter 
                // found (space separator))
                case 1:
                    if (parameter != null)
                    {
                        if (!this.parameters.ContainsKey(parameter))
                        {
                            parts[0] = remover.Replace(parts[0], "$1");

                            this.parameters.Add(parameter, parts[0]);
                        }

                        parameter = null;
                    }

                    // else Error: no parameter waiting for a value (skipped)
                    break;

                // Found just a parameter
                case 2:
                    // The last parameter is still waiting. 
                    // With no value, set it to true.
                    if (parameter != null)
                    {
                        if (!this.parameters.ContainsKey(parameter))
                        {
                            this.parameters.Add(parameter, "true");
                        }
                    }

                    parameter = parts[1];
                    break;

                // Parameter with enclosed value
                case 3:
                    // The last parameter is still waiting. 
                    // With no value, set it to true.
                    if (parameter != null)
                    {
                        if (!this.parameters.ContainsKey(parameter))
                        {
                            this.parameters.Add(parameter, "true");
                        }
                    }

                    parameter = parts[1];

                    // Remove possible enclosing characters (",')
                    if (!this.parameters.ContainsKey(parameter))
                    {
                        parts[2] = remover.Replace(parts[2], "$1");
                        this.parameters.Add(parameter, parts[2]);
                    }

                    parameter = null;
                    break;
            }
        }

        // In case a parameter is still waiting
        if (parameter != null)
        {
            if (!this.parameters.ContainsKey(parameter))
            {
                this.parameters.Add(parameter, "true");
            }
        }
    }
    #endregion

    #region Properties
    /// <summary>
    /// Gets a count of command line arguments
    /// </summary>
    public int Count
    {
        get
        {
            return this.parameters.Count;
        }
    }

    /// <summary>
    /// Gets the value with the given parameter name
    /// </summary>
    /// <param name="param">name of the parameter</param>
    /// <returns>the value of the parameter</returns>
    public string this[string param]
    {
        get
        {
            return this.parameters[param];
        }
    }
    #endregion
}

Autres conseils

J'ai développé ce cadre, peut-être aide:

Le SysCommand est un puissant cadre multi-plateforme, pour développer des applications de la console dans .NET. Est simple, le type de sécurité, et avec de grandes influences du modèle MVC.

https://github.com/juniorgasparotto/SysCommand

namespace Example.Initialization.Simple
{
    using SysCommand.ConsoleApp;

    public class Program
    {
        public static int Main(string[] args)
        {
            return App.RunApplication();
        }
    }

    // Classes inheriting from `Command` will be automatically found by the system
    // and its public properties and methods will be available for use.
    public class MyCommand : Command
    {
        public void Main(string arg1, int? arg2 = null)
        {
            if (arg1 != null)
                this.App.Console.Write(string.Format("Main arg1='{0}'", arg1));
            if (arg2 != null)
                this.App.Console.Write(string.Format("Main arg2='{0}'", arg2));
        }

        public void MyAction(bool a)
        {
            this.App.Console.Write(string.Format("MyAction a='{0}'", a));
        }
    }
}

Tests:

// auto-generate help
$ my-app.exe help

// method "Main" typed
$ my-app.exe --arg1 value --arg2 1000

// or without "--arg2"
$ my-app.exe --arg1 value

// actions support
$ my-app.exe my-action -a

http://commandline.codeplex.com/ Je l'ai utilisé tant de fois que je » ai perdu le compte. Peut-être que cela fonctionne pour CE. Sinon, il va fournir un point de départ fantastique.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top