Esempio n. 1
0
        private static bool IsBool(string key, PowerArgs.ArgHook.HookContext context, ParseResult resultContext)
        {
            var match = context.Definition.FindMatchingArgument(key, true);

            if (match == null)
            {
                var possibleActionContext = resultContext.ImplicitParameters.ContainsKey(0) ? resultContext.ImplicitParameters[0] : null;

                if (possibleActionContext == null)
                {
                    return(false);
                }
                else
                {
                    var actionContext = context.Definition.FindMatchingAction(possibleActionContext, true);
                    if (actionContext == null)
                    {
                        return(false);
                    }

                    match = actionContext.FindMatchingArgument(key, true);
                    if (match == null)
                    {
                        return(false);
                    }
                }
            }

            return(match.ArgumentType == typeof(bool));
        }
Esempio n. 2
0
        private static bool IsBool(string key, PowerArgs.ArgHook.HookContext context)
        {
            var match = context.Definition.FindMatchingArgument(key, true);

            if (match == null)
            {
                return(false);
            }

            return(match.ArgumentType == typeof(bool));
        }
Esempio n. 3
0
        internal static ParseResult Parse(PowerArgs.ArgHook.HookContext context)
        {
            var args = context.CmdLineArgs;

            ParseResult result = new ParseResult();

            int argumentPosition = 0;

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

                if (i == 0 && context.Definition.Actions.Count > 0 && context.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 a special case --arg-name- where we have a trailing -
                    // it's a shortcut way of disabling an option
                    if (key.StartsWith("-") && key.EndsWith("-"))
                    {
                        key = key.Substring(1, key.Length - 2);
                        if (IsBool(key, context))
                        {
                            var redefinedArgs = new List <string>(args);
                            redefinedArgs.Insert(i + 1, "false");
                            args = redefinedArgs.ToArray();
                        }
                    }
                    // Handles long form syntax --argName=argValue.
                    if (key.StartsWith("-") && key.Contains("="))
                    {
                        var index = key.IndexOf("=");
                        value = key.Substring(index + 1);
                        key   = key.Substring(1, index - 1);
                    }
                    else
                    {
                        if (key.StartsWith("-"))
                        {
                            key = key.Substring(1);
                        }
                        if (i == args.Length - 1)
                        {
                            value = "";
                        }
                        else if (IsBool(key, context))
                        {
                            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);
                    argumentPosition = -1;
                }
                else
                {
                    if (argumentPosition < 0)
                    {
                        throw new UnexpectedArgException("Unexpected argument: " + token);
                    }
                    result.ImplicitParameters.Add(argumentPosition, token);
                    argumentPosition++;
                }
            }

            return(result);
        }