Beispiel #1
0
        public virtual bool consume(VirtualArray<string> vArgs, ParsedArgs pArgs)
        {
            if (!isConsumeable(vArgs))
            {
                return false;
            }

            pArgs.add(name, getValue(vArgs));

            if (argCountIsRemainderOfArgs)
            {
                vArgs.moveStart(vArgs.endIndexExclusive);
            }
            else
            {
                vArgs.moveStartBy(argCount + 1);
            }
            return true;
        }
        public virtual ParsedArgs parseArgs(string[] args)
        {
            // setup stuff
            ParsedArgs pArgs = new ParsedArgs();
            int orderedArgDefIx = 0;
            initArgDefs();
            VirtualArray<string> vArgs = new VirtualArray<string>(args);
            //

            while (vArgs.length > 0)
            {
                bool argConsumed = false;
                for (int i = 0; !argConsumed && i < labeledArgDefs.Count; i++)
                {
                    ArgDef def = labeledArgDefs[i];
                    argConsumed = def.consume(vArgs, pArgs);

                    // test crap
                    if (argConsumed)
                    {
                        Console.WriteLine(string.Format("[TEMP DEBUG] LArg '{0}' was consumed", def.name));
                    }
                    //

                    if (def.errorOccured())
                    {
                        pArgs.addErrorMessages(def.getErrorMessages());
                        vArgs.moveStart(vArgs.endIndexExclusive); // this exits the outer while loop
                    }
                }
                if (!argConsumed)
                {
                    if (orderedArgDefIx >= orderedArgDefs.Count)
                    {
                        pArgs.addErrorMessage("Encountered more args than expected.");
                        break;
                    }

                    ArgDef oDef = orderedArgDefs[orderedArgDefIx];
                    argConsumed = oDef.consume(vArgs, pArgs);
                    orderedArgDefIx++;

                    if (oDef.errorOccured())
                    {
                        pArgs.addErrorMessages(oDef.getErrorMessages());
                        vArgs.moveStart(vArgs.endIndexExclusive); // this exits the outer while loop
                    }
                    else if (!argConsumed)
                    {

                        pArgs.addErrorMessage(string.Format("Unable to use arg '{0}'", vArgs[0]));
                        vArgs.moveStart(vArgs.endIndexExclusive); // this exits the outer while loop
                    }

                    // test crap
                    else
                    {
                        Console.WriteLine(string.Format("[Temp Debug] OArg '{0}' was consumed", oDef.name));
                    }
                    //
                }
            } // end while

            if (pArgs.errorOccured())
            {
                if (printErrorMessageOnInvalidArgs)
                {
                    foreach (string message in pArgs.getErrorMessages())
                    {
                        Console.WriteLine(message);
                    }
                }
                if (printUsageOnInvalidArgs)
                {
                    Console.WriteLine(getUsageString());
                }
            }
            else
            {
                finishArgDefs(pArgs);
            }
            return pArgs;
        }