예제 #1
0
        public void ParseCommandLine(string[] args)
        {
            FillDefaultStructuresFromConfig();

            if (_aliases == null)
            {
                _aliases = new Dictionary <string, string>(_defaultCollectionSize, Comparer);
            }
            if (_instances == null)
            {
                _instances = new Dictionary <string, CommandLineArgumentInstance>(_defaultCollectionSize, Comparer);
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            for (int i = 0; i < args.Length; i++)
            {
                string          currentArg = args[i];
                GroupCollection groups     = _pullArgNameRegex.Match(currentArg).Groups;

                if (groups["argType"].Success && groups["argType"].Value.Trim() == "--")
                {
                    _additionalRawArguments = new List <string>(args.Length - i);
                    if (Config.SupportPosititionalParameters && _positionalParameters == null)
                    {
                        _positionalParameters = new List <string>(args.Length - i);
                    }

                    if (i == args.Length - 1)
                    {
                        _additionalRawArguments = new List <string>();
                    }
                    else
                    {
                        for (int j = i + 1; j < args.Length; j++)
                        {
                            _additionalRawArguments.Add(args[j]);
                            if (Config.SupportPosititionalParameters && _positionalParameters != null)
                            {
                                _positionalParameters.Add(args[j]);
                            }
                        }
                        break;
                    }
                }

                if (groups["argName"].Success)
                {
                    List <string> argNames = new List <string>();
                    if (groups.Any(x => x.Name == "singleLetterArg"))
                    {
                        foreach (Capture c in groups["singleLetterArg"].Captures)
                        {
                            argNames.Add(c.Value.Trim());
                        }
                    }

                    argNames.Add(groups["argName"].Value.Trim());

                    foreach (var argName in argNames)
                    {
                        CommandLineArgumentInstance tempInstance = new CommandLineArgumentInstance(argName);

                        if (groups["argValue"].Success)
                        {
                            string valOrVals = groups["argValue"].Value.Trim();
                            if (valOrVals.Contains(',', StringComparison.Ordinal))
                            {
                                tempInstance.Values.AddRange(valOrVals.Split(','));
                            }
                            else
                            {
                                tempInstance.BoolValue = CalculateBooleanValue(valOrVals);
                                tempInstance.Values.Add(valOrVals);
                            }
                        }

                        while (i < args.Length - 1 && !IsArg(args[i + 1]))
                        {
                            string nextArg = args[i + 1].Trim();
                            if (nextArg.Contains(',', StringComparison.Ordinal))
                            {
                                tempInstance.Values.AddRange(nextArg.Split(','));
                            }
                            else
                            {
                                tempInstance.BoolValue = CalculateBooleanValue(nextArg);
                                tempInstance.Values.Add(nextArg);
                            }
                            tempInstance.Values.Add(nextArg);
                            i++;
                        }

                        if (_aliases.TryGetValue(argName, out string cName))
                        {
                            if (_instances.TryGetValue(cName, out CommandLineArgumentInstance instance))
                            {
                                instance.Exists    = true;
                                instance.BoolValue = tempInstance.BoolValue;
                                instance.DataType  = tempInstance.DataType;
                                instance.Values.AddRange(tempInstance.Values);
                            }
                        }
                        else if (_instances.TryGetValue(argName, out CommandLineArgumentInstance instance))
                        {
                            instance.Exists    = true;
                            instance.BoolValue = tempInstance.BoolValue;
                            instance.DataType  = tempInstance.DataType;
                            instance.Values.AddRange(tempInstance.Values);
                        }
                        else
                        {
                            _aliases.Add(argName, argName);
                            tempInstance.Name = argName;

                            CommandLineArgumentInstance newInstance = new CommandLineArgumentInstance(argName);
                            newInstance.Exists    = true;
                            newInstance.BoolValue = tempInstance.BoolValue;
                            newInstance.DataType  = tempInstance.DataType;
                            if (tempInstance.Values.Count > 0)
                            {
                                newInstance.Values.AddRange(tempInstance.Values);
                            }

                            _instances.Add(argName, newInstance);
                        }
                    }
                }
            }

            // process positional arguments
            if (Config.SupportPosititionalParameters)
            {
                int positionalArgCount = 0;
                for (int k = args.Length - 1; k >= 0; k--)
                {
                    if (!IsArg(args[k]))
                    {
                        positionalArgCount++;
                    }
                    else
                    {
                        break;
                    }
                }

                if (_positionalParameters == null)
                {
                    _positionalParameters = new List <string>();
                    for (int l = args.Length - positionalArgCount; l < args.Length; l++)
                    {
                        _positionalParameters.Add(String.IsNullOrWhiteSpace(args[l]) ? String.Empty : args[l].Trim());
                    }
                }
            }
        }