示例#1
0
        internal RawArguments <T> Parse(string[] args, Separators separators)
        {
            List <string> commands = Utils.GetCommandNames <T>(bindingFlags);

            for (int i = 0; i < args.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(args[i]))
                {
                    continue;
                }

                if ((args[i].Matches(@"^-[a-zA-Z0-9_]=\w+$") || args[i].Matches(@"^--[a-zA-Z0-9_-]{2,}=\w+$")) && separators.HasFlag(Separators.Equals) ||
                    (args[i].Matches(@"^-[a-zA-Z0-9_]:\w+$") || args[i].Matches(@"^--[a-zA-Z0-9_-]{2,}:\w+$")) && separators.HasFlag(Separators.Colon))
                {
                    string key   = args[i].TrimStart('-').Split(':')[0].Split('=')[0];
                    string value = args[i].Split(':').Last().Split('=').Last();

                    TryAddKeyValuePair(key, value);
                }
                else if (args[i].Matches(@"^-[a-zA-Z0-9_]$") || args[i].Matches(@"^--[a-zA-Z0-9_-]{2,}$"))
                {
                    string key = args[i].TrimStart('-');

                    int intTest;
                    if (!booleanKeys.Contains(key) && i < args.Length - 1 &&
                        (!args[i + 1].StartsWith("-") || int.TryParse(args[i + 1], out intTest)))
                    {
                        TryAddKeyValuePair(key, args[i + 1]);
                        i++;
                    }
                    else
                    {
                        flags.Add(key);
                    }
                }
                else if (args[i].Matches(@"^-[a-zA-Z0-9_]{2,}$"))
                {
                    // Multiple flags
                    flags.AddRange(
                        args[i].ToCharArray().Select(c => c.ToString())
                        );
                }
                else
                {
                    if (commands.Contains(args[i]))
                    {
                        // We caught a command name, stop parsing
                        Command      = args[i];
                        CommandIndex = i;

                        return(this);
                    }
                    else
                    {
                        // No commands with this name, add it to the positional arguments
                        positionalArguments.Add(args[i]);
                    }
                }
            }
            return(this);
        }