Example #1
0
        public ArgumentDef(
            char shortName,
            string longName,
            Type dataType            = null,
            ArgumentType argType     = ArgumentType.Positional,
            ArgumentStatus argStatus = ArgumentStatus.Required,
            sbyte consumption        = 0
            )
        {
            if (shortName == nullChar && longName == emptyString)
            {
                throw new Exception("Argument must have a ShortName, LongName, or both.");
            }

            if (argType == ArgumentType.Positional && consumption == 0)
            {
                throw new Exception("Invalid number of values for positional argument '{0}'.  Positional arguments must be represented by at least one value.");
            }

            if (dataType == null)
            {
                dataType = typeof(object);
            }

            this._dataType   = null;
            this.ShortName   = shortName;
            this.LongName    = longName;
            this.ArgType     = argType;
            this.ArgStatus   = argStatus;
            this.Consumption = consumption;

            this.DataType = dataType;
        }
Example #2
0
 public ArgumentDef(
     string longName,
     Type dataType            = null,
     ArgumentType argType     = ArgumentType.Positional,
     ArgumentStatus argStatus = ArgumentStatus.Required,
     sbyte consumption        = 0
     ) : this(nullChar, longName, dataType, argType, argStatus, consumption)
 {
 }
Example #3
0
 public ArgumentDef(
     char shortName,
     Type dataType            = null,
     ArgumentType argType     = ArgumentType.Positional,
     ArgumentStatus argStatus = ArgumentStatus.Required,
     sbyte consumption        = 0
     ) : this(shortName, emptyString, dataType, argType, argStatus, consumption)
 {
 }
Example #4
0
 public static ArgumentDef AddArgument(
     string longName,
     Type dataType            = default(Type),
     ArgumentType argType     = ArgumentType.Positional,
     ArgumentStatus argStatus = ArgumentStatus.Required,
     sbyte consumption        = 0
     )
 {
     return(AddArgument(ArgumentDef.nullChar, longName, dataType, argType, argStatus, consumption));
 }
Example #5
0
 public static ArgumentDef AddArgument(
     char shortName,
     Type dataType            = default(Type),
     ArgumentType argType     = ArgumentType.Positional,
     ArgumentStatus argStatus = ArgumentStatus.Required,
     sbyte consumption        = 0
     )
 {
     return(AddArgument(shortName, ArgumentDef.emptyString, dataType, argType, argStatus, consumption));
 }
Example #6
0
        } /* end ErrorCount */

/* ---------------------------------------------------------------------------
 * private method ParseInfoRequest(sym)
 * ---------------------------------------------------------------------------
 * infoRequest :
 *   HELP | VERSION | LICENSE
 *   ;
 * ------------------------------------------------------------------------ */

        private static ArgumentToken ParseInfoRequest(ArgumentToken sym)
        {
            switch (sym)
            {
            case ArgumentToken.HELP:
                status = ArgumentStatus.HelpRequested;
                break;

            case ArgumentToken.VERSION:
                status = ArgumentStatus.VersionRequested;
                break;

            case ArgumentToken.LICENSE:
                status = ArgumentStatus.LicenseRequested;
                break;
            } /* end switch */

            return(ArgumentLexer.NextToken());
        } /* end ParseInfoRequest */
Example #7
0
        } /* end ArgumentParser */

/* ---------------------------------------------------------------------------
 * method ParseOptions()
 * ---------------------------------------------------------------------------
 * options :
 *   infoRequest | compilationRequest
 *   ;
 * ------------------------------------------------------------------------ */

        public static ArgumentStatus ParseOptions(string[] args)
        {
            ArgumentToken sym;

            ArgumentLexer.InitWithArgs(args);
            sourceFile = null;
            errorCount = 0;
            optionSet  = 0;

            sym = ArgumentLexer.NextToken();

            if (ArgumentLexer.IsInfoRequest(sym))
            {
                sym = ParseInfoRequest(sym);
            }
            else if (ArgumentLexer.IsCompilationRequest(sym))
            {
                sym = ParseCompilationRequest(sym);
            }
            else if (sym == ArgumentToken.END_OF_INPUT)
            {
                ReportMissingSourceFile();
            } /* end if */

            while (sym != ArgumentToken.END_OF_INPUT)
            {
                ReportExcessArgument(ArgumentLexer.LastArg());
                sym = ArgumentLexer.NextToken();
            } /* end while */

            if (errorCount > 0)
            {
                status = ArgumentStatus.ErrorsEncountered;
            } /* end if */

            return(status);
        } /* end ParseOptions */
Example #8
0
        public static ArgumentDef AddArgument(
            char shortName,
            string longName,
            Type dataType            = default(Type),
            ArgumentType argType     = ArgumentType.Positional,
            ArgumentStatus argStatus = ArgumentStatus.Required,
            sbyte consumption        = 0
            )
        {
            ArgumentDef def = new ArgumentDef(shortName, longName, dataType, argType, argStatus, consumption);

            int _;

            if (def.ShortName != ArgumentDef.nullChar && defsByShort.TryGetValue(def.ShortName, out _))
            {
                throw new Exception(string.Format("Cannot add new argument with duplicate short name '{0}'", def.ShortName));
            }

            if (def.LongName != ArgumentDef.emptyString && defsByLong.TryGetValue(def.LongName, out _))
            {
                throw new Exception(string.Format("Cannot add new argument with duplicate long name \"{0}\"", def.LongName));
            }

            if (def.ArgType == ArgumentType.Positional && def.ArgStatus == ArgumentStatus.Required && optionalPositionalDefined)
            {
                throw new Exception("Cannot add new required positional arguments after optional positional arguments.");
            }

            if (def.ArgType == ArgumentType.Positional && def.ArgStatus == ArgumentStatus.Required && greedyArgDefined)
            {
                throw new Exception("Cannot add new required positional arguments after greedy arguments.");
            }

            defs.Add(def);

            if (def.Consumption < 0)
            {
                greedyArgDefined = true;
            }

            if (def.ArgType == ArgumentType.Positional)
            {
                positionalDefs.Add(def);

                if (def.ArgStatus == ArgumentStatus.Optional)
                {
                    optionalPositionalDefined = true;
                }
            }

            if (def.ArgStatus == ArgumentStatus.Required)
            {
                requiredDefs.Add(def);
            }

            if (def.ShortName != ArgumentDef.nullChar)
            {
                defsByShort[def.ShortName] = defs.Count - 1;
            }

            if (def.LongName != ArgumentDef.emptyString)
            {
                defsByLong[def.LongName] = defs.Count - 1;
            }

            return(def);
        }