/// <summary>
        /// Parses command line arguments into positional arguments, switches and options.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        /// <param name="markedAsOptions">Optional switches to be treated as options separated with '|', ',' or ' ' character.</param>
        public CommandLineArgumentsEx(IEnumerable <string> args = null, string markedAsOptions = null)
        {
            Raw = args?.ToArray() ?? new string[] { };
            string[] marked = markedAsOptions != null?markedAsOptions.Split('|', ',', ' ') : new string[]
            {
            };
            IsEmpty = !Raw.Any();
            var e          = (Raw as IEnumerable <string>).GetEnumerator();
            var positional = new List <string>();
            var switches   = new List <string>();
            var options    = new Dictionary <string, string>();

            while (e.MoveNext())
            {
                var match = RxElement.Match(e.Current);
                if (match.Success)
                {
                    var key = match.Groups[1].Value;
                    if (marked.Any() && marked.Contains(key, StringComparer.Ordinal))
                    {
                        if (e.MoveNext() && !options.ContainsKey(key))
                        {
                            options.Add(key, e.Current);
                        }
                    }
                    else
                    {
                        switches.Add(key);
                    }
                }
                else
                {
                    positional.Add(e.Current);
                }
            }
            Positional = new CommandLinePositionalArgumentCollection(positional);
            Switches   = new CommandLineSwitchCollection(switches);
            Options    = new CommandLineOptionCollection(options);
        }
Exemple #2
0
        /// <summary>
        /// Parses command line arguments into parameters and options.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        public CommandLine(string[] args)
        {
            IsEmpty = true;
            List <string> parameterList = null;

            foreach (var arg in args)
            {
                var match = RxElement.Match(arg);
                if (match.Success)
                {
                    IsEmpty = false;
                    var @switch  = match.Groups[1].Value;
                    var bareText = match.Groups[2].Value;
                    var split    = bareText.Split(new[] { '=' }, 2);
                    var key      = split[0];
                    var value    = split.Length > 1 ? split[1] : null;
                    if (@switch.Length < 1)   // parameters are arguments prefixed
                    {
                        if (parameterList == null)
                        {
                            parameterList = new List <string>();
                        }
                        parameterList.Add(arg);
                        HasParameters = true;
                    }
                    else   // options are prefixed
                    {
                        OptionCollection[key] = value;
                        HasOptions            = true;
                    }
                }
            }
            if (parameterList != null)
            {
                ParameterCollection = parameterList.ToArray();
            }
        }