/// <summary>
        /// Parses the command-line arguments "args" into the global flag variables.  Returns true
        /// if there were no errors.
        /// </summary>
        /// <param name="args">Consumed ("captured" and possibly modified) by the method.</param>
        public bool Parse([Captured] string[]/*!*/ args)
        {
            Contract.Requires(cce.NonNullElements(args));

              // save the command line options for the log files
              Environment += "Command Line Options: " + args.Concat(" ");
              args = cce.NonNull((string[])args.Clone());  // the operations performed may mutate the array, so make a copy
              var ps = new CommandLineParseState(args, ToolName);

              while (ps.i < args.Length) {
            cce.LoopInvariant(ps.args == args);
            string arg = args[ps.i];
            Contract.Assert(arg != null);
            ps.s = arg.Trim();

            bool isOption = ps.s.StartsWith("-") || ps.s.StartsWith("/");
            int colonIndex = ps.s.IndexOf(':');
            if (0 <= colonIndex && isOption) {
              ps.hasColonArgument = true;
              args[ps.i] = ps.s.Substring(colonIndex + 1);
              ps.s = ps.s.Substring(0, colonIndex);
            } else {
              ps.i++;
              ps.hasColonArgument = false;
            }
            ps.nextIndex = ps.i;

            if (isOption) {
              if (!ParseOption(ps.s.Substring(1), ps)) {
            if (Path.DirectorySeparatorChar == '/' && ps.s.StartsWith("/"))
              this._files.Add(arg);
            else
              ps.Error("unknown switch: {0}", ps.s);
              }
            } else {
              this._files.Add(arg);
            }

            ps.i = ps.nextIndex;
              }

              if (HelpRequested) {
            Usage();
              } else if (AttrHelpRequested) {
            AttributeUsage();
              } else if (ps.EncounteredErrors) {
            Console.WriteLine("Use /help for available options");
              }

              if (ps.EncounteredErrors) {
            return false;
              } else {
            this.ApplyDefaultOptions();
            return true;
              }
        }
Exemple #2
0
 protected void InvalidArgumentError(string name, CommandLineParseState ps)
 {
     ps.Error("Invalid argument \"{0}\" to option {1}", ps.args[ps.i], name);
 }