سؤال

قرأت هذا السؤال: محلل سطر الأوامر لـ .NET.

اعتقدت أن هذا ما كنت أبحث عنه ، لكن المكتبة مكتبة محلل سطر الأوامر ليس صديقًا مضغوطًا صديقًا ...

أنا حقًا لا أريد أن أكتب محللًا CL وكنت أتخلص من الغرض الحقيقي من تطبيقي الصغير بسبب هذه التجربة المؤسفة.

هل يعرف شخص ما مكتبة تناسب العمل المدمج؟ (ويفضل مع البساطة والوظائف مثل تلك المذكورة أعلاه)
لا يهم ما إذا كان الإصدار 2 أو 3.5

هل كانت مفيدة؟

المحلول

هذا ما أستخدمه. لقد استعارته من مكان ما ، لكن لست متأكدًا من أين:

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
}

نصائح أخرى

لقد طورت هذا الإطار ، ربما يساعد:

يعد Syscommand إطارًا قويًا عبر المنصات ، لتطوير تطبيقات وحدة التحكم في .NET. بسيط ، آمن من النوع ، مع تأثيرات كبيرة لنمط 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));
        }
    }
}

الاختبارات:

// 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/ لقد استخدمت هذا عدة مرات فقدت العد. ربما يعمل ل CE. إذا لم يكن الأمر كذلك ، فسيوفر نقطة انطلاق رائعة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top