コード例 #1
0
        private ParseOutcome ProcessOption(IEnumerable<IOption> options, string arg, Queue<string> argQueue, IParserResult result)
        {
            string[] optionArgs = null;
            var optionDetails = options as IList<IOption> ?? options.ToList();
            var optionNames = optionDetails.Select(o => o.Name);
            var colonPos = arg.IndexOf(':');
            if (colonPos < 0)
            {
                var colonOptionName = GetOptionName(optionNames, arg.Substring(1));
                var colonOptionDefinition = optionDetails.FirstOrDefault(o => System.String.Compare(o.Name, colonOptionName, System.StringComparison.OrdinalIgnoreCase) == 0);
                if (colonOptionDefinition != null)
                {
                    colonOptionName = colonOptionDefinition.Name;
                    optionArgs = ExtractOptionArgs(argQueue, colonOptionDefinition);
                }

                return result.OptionExtracted(colonOptionName, optionArgs ?? new string[] { });
            }

            var option = arg.Substring(1, colonPos - 1);
            var optionDefinition = optionDetails.FirstOrDefault(o => string.Compare(o.Name, option, StringComparison.OrdinalIgnoreCase) == 0);
            optionArgs = optionDefinition == null || optionDefinition.ParameterCount > 1
                ? arg.Substring(colonPos + 1).Split(',')
                : new[] {arg.Substring(colonPos + 1)};
            return result.OptionExtracted(GetOptionName(optionNames, option), optionArgs);
        }
コード例 #2
0
        private ParseOutcome ProcessShortOption(List<IOption> shortOptions, string arg, Queue<string> argQueue, IParserResult result)
        {
            var option = shortOptions.FirstOrDefault(o => o.Name == GetShortOptionName(arg));
            if (option != null)
            {
                if (option.ParameterCount == 0 || option.IsBoolean)
                {
                    foreach (var optionChar in arg)
                    {
                        if (result.OptionExtracted(optionChar.ToString(CultureInfo.InvariantCulture), new string[] {}) == ParseOutcome.Halt)
                            return ParseOutcome.Halt;
                    }
                    return ParseOutcome.Continue;
                }

                if (arg.Length > 1)
                    return result.OptionExtracted(GetShortOptionName(arg), GetShortOptionArg(arg));

                if (ParameterAvailable(argQueue))
                    return result.OptionExtracted(GetShortOptionName(arg), new string[] { });

                string[] optionArgs;
                if (argQueue.Count > 0)
                    optionArgs = argQueue.Dequeue().Split(',');
                else
                    optionArgs = new string[] {};
                return result.OptionExtracted(option.Name, optionArgs);
            }

            if (arg.Length > 1)
                return result.OptionExtracted(GetShortOptionName(arg), GetShortOptionArg(arg));

            return result.OptionExtracted(arg, new string[] { });
        }
コード例 #3
0
        private ParseOutcome GetOption(IParserResult result, Queue<string> argQueue, string arg, IEnumerable<IOption> options, IEnumerable<IPositionalArgument> positionals)
        {
            var embeddedOptionName = GetOptionName(arg);
            var option = options.FirstOrDefault(o => System.String.Compare(o.Name, embeddedOptionName, System.StringComparison.OrdinalIgnoreCase) == 0);
            string optionName;
            string[] optionArgs;
            if (option != null)
            {
                optionName = option.Name;
                optionArgs = ExtractOptionArgs(arg, argQueue, option);
            }
            else
            {
                var pos = positionals.FirstOrDefault(p => System.String.Compare(p.ParameterName, embeddedOptionName, System.StringComparison.OrdinalIgnoreCase) == 0);
                if (pos != null)
                    optionName = pos.ParameterName;
                else
                    optionName = embeddedOptionName;
                optionArgs = ExtractOptionArgs(arg, argQueue, null);
            }

            return result.OptionExtracted(optionName, optionArgs);
        }
コード例 #4
0
        private ParseOutcome ProcessLongOption(List<IOption> longOptions, string arg, Queue<string> argQueue, IParserResult result)
        {
            string optionName;
            var equalPos = arg.IndexOf('=');
            if (equalPos < 0)
            {
                optionName = arg;

                var noEqualOption = longOptions.FirstOrDefault(o => o.Name == optionName);
                if (noEqualOption != null && noEqualOption.ParameterCount > 0 && ParameterAvailable(argQueue))
                {
                    var args = noEqualOption.ParameterCount == 1 ? new [] { argQueue.Dequeue() } : argQueue.Dequeue().Split(',');
                    return result.OptionExtracted(optionName, args);
                }

                return result.OptionExtracted(optionName, new string[] { });
            }

            optionName = arg.Substring(0, equalPos);
            var option = longOptions.FirstOrDefault(o => o.Name == optionName);
            var valuePart = arg.Substring(equalPos + 1);
            var optionArgs = option != null && option.ParameterCount == 1 ? new []{ valuePart } : valuePart.Split(',');
            return result.OptionExtracted(optionName, optionArgs);
        }