PowerArgs.ArgParser.Parse C# (CSharp) Method

Parse() static private method

static private Parse ( CommandLineArgumentsDefinition Definition, string commandLineArgs ) : ParseResult
Definition CommandLineArgumentsDefinition
commandLineArgs string
return ParseResult
        internal static ParseResult Parse(CommandLineArgumentsDefinition Definition, string[] commandLineArgs)
        {
            var args = commandLineArgs;

            ParseResult result = new ParseResult();

            int argumentPosition = 0;
            for (int i = 0; i < args.Length; i++)
            {
                var token = args[i];

                // this block handles action parameters that must always be the first token
                if (i == 0 && Definition.Actions.Count > 0 && Definition.FindMatchingAction(token) != null)
                {
                    result.ImplicitParameters.Add(0, token);
                    argumentPosition++;
                }
                else if (token.StartsWith("/"))
                {
                    var param = ParseSlashExplicitOption(token);
                    if (result.ExplicitParameters.ContainsKey(param.Key)) throw new DuplicateArgException("Argument specified more than once: " + param.Key);
                    result.ExplicitParameters.Add(param.Key, param.Value);
                    argumentPosition = -1;
                }
                else if (token.StartsWith("-"))
                {
                    string key = token.Substring(1);

                    if (key.Length == 0) throw new ArgException("Missing argument value after '-'");

                    string value;

                    // Handles long form syntax --argName=argValue.
                    if (key.StartsWith("-") && key.Contains("="))
                    {
                        var index = key.IndexOf("=");
                        value = key.Substring(index + 1);
                        key = key.Substring(0, index);
                    }
                    else
                    {
                        if (i == args.Length - 1)
                        {
                            value = "";
                        }
                        else if (IsBool(key, Definition, result))
                        {
                            var next = args[i + 1].ToLower();

                            if (next == "true" || next == "false" || next == "0" || next == "1")
                            {
                                i++;
                                value = next;
                            }
                            else
                            {
                                value = "true";
                            }
                        }
                        else
                        {
                            i++;
                            value = args[i];
                        }
                    }

                    if (result.ExplicitParameters.ContainsKey(key))
                    {
                        throw new DuplicateArgException("Argument specified more than once: " + key);
                    }

                    result.ExplicitParameters.Add(key, value);

                    if(IsArrayOrList(key, Definition, result))
                    {
                        while((i+1) < args.Length)
                        {
                            var nextToken = args[i+1];

                            if(nextToken.StartsWith("/") || nextToken.StartsWith("-"))
                            {
                                break;
                            }
                            else
                            {
                                result.AddAdditionalParameter(key, nextToken);
                                i++;
                            }

                        }
                    }

                    argumentPosition = -1;
                }
                else
                {
                    if (argumentPosition < 0) throw new UnexpectedArgException("Unexpected argument: " + token);

                    var possibleActionContext = result.ImplicitParameters.ContainsKey(0) ? result.ImplicitParameters[0] : null;
                    var potentialListArgument = Definition.FindArgumentByPosition(argumentPosition, possibleActionContext);

                    if (potentialListArgument != null)
                    {
                        bool isArrayOrList = potentialListArgument.ArgumentType.IsArray || potentialListArgument.ArgumentType.GetInterfaces().Contains(typeof(IList));

                        if (isArrayOrList)
                        {
                            // this block does special handling to allow for space separated collections for positioned parameters

                            result.ExplicitParameters.Add(potentialListArgument.DefaultAlias, token);
                            argumentPosition = -1; // no more positional arguments are allowed after this
                            while ((i + 1) < args.Length)
                            {
                                var nextToken = args[i + 1];

                                if (nextToken.StartsWith("/") || nextToken.StartsWith("-"))
                                {
                                    break;
                                }
                                else
                                {
                                    result.AddAdditionalParameter(potentialListArgument.DefaultAlias, nextToken);
                                    i++;
                                }

                            }
                        }
                        else
                        {
                            // not an array or list parameter so add to the implicit parameter collection
                            result.ImplicitParameters.Add(argumentPosition, token);
                            argumentPosition++;
                        }
                    }
                    else
                    {
                        // not an array or list parameter so add to the implicit parameter collection
                        result.ImplicitParameters.Add(argumentPosition, token);
                        argumentPosition++;
                    }
                }
            }

            return result;
        }

Usage Example

示例#1
0
        private ArgAction ParseInternal(Type t, string[] input)
        {
            ArgShortcut.RegisterShortcuts(t);
            ValidateArgScaffold(t);

            var context = new ArgHook.HookContext();

            context.Args        = Activator.CreateInstance(t);
            context.CmdLineArgs = input;

            t.RunBeforeParse(context);
            var specifiedActionProperty = FindSpecifiedAction(t, ref context.CmdLineArgs);

            context.ParserData = ArgParser.Parse(context.CmdLineArgs);
            PopulateProperties(context);

            if (specifiedActionProperty != null)
            {
                var actionPropertyValue = Activator.CreateInstance(specifiedActionProperty.PropertyType);
                var toRestore           = context.Args;
                context.Args = actionPropertyValue;
                PopulateProperties(context);
                context.Args = toRestore;
                specifiedActionProperty.SetValue(context.Args, actionPropertyValue, null);
            }

            if (context.ParserData.ImplicitParameters.Count > 0)
            {
                throw new UnexpectedArgException("Unexpected unnamed argument: " + context.ParserData.ImplicitParameters.First().Value);
            }

            if (context.ParserData.ExplicitParameters.Count > 0)
            {
                throw new UnexpectedArgException("Unexpected named argument: " + context.ParserData.ExplicitParameters.First().Key);
            }

            return(new ArgAction()
            {
                Value = context.Args,
                ActionArgs = specifiedActionProperty != null?specifiedActionProperty.GetValue(context.Args, null) : null,
                                 ActionArgsProperty = specifiedActionProperty
            });
        }
All Usage Examples Of PowerArgs.ArgParser::Parse