Search code examples
compact-frameworkparsingwindows-cecommand-line-arguments

C# Compact-Framework friendly command line parser


I read this question: Command Line Parser for .NET.

I thought that was what I was looking for, but the library Command Line Parser Library is not Compact framework friendly...

I REALLY don't want to write a CL parser and I have been drifting away from the real purpose of my little app because of this unfortunate trial.

Does someone know of a library that fits the compact-framework? (preferably with simplicity and functionality like the one mentioned above)
Does not matter whether version 2 or 3.5


Solution

  • This is what I'm using. I borrowed it from somewhere, but not sure where:

    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
    }