Example #1
0
    /// <summary>
    /// Parse the args into an Option object
    /// </summary>
    /// <param name="args">the args param to Main</param>
    /// <param name="options">the parsed arguements, valid if return is ture</param>
    /// <param name="why">an error message if parsing fails and the return is false</param>
    /// <returns>true if parsing succeeded, false otherwise</returns>
    internal static bool TryParseOptions(string[] args, out Options options, out string why)
    {
      #region CodeContracts
      Contract.Requires(Contract.ForAll(args, arg => arg != null));
      Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options) != null);
      Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options).Project != null);
      Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options).Solution != null);
      Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options).ClousotXML != null);
      Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options).Output != OutputOption.unintialized);
      Contract.Ensures(Contract.Result<bool>() || Contract.ValueAtReturn(out why) != null);
      // there should be a contract for the output option, but it's complicated
      //Contract.Ensures(
      //    !Contract.Result<bool>()
      // || ((Contract.ValueAtReturn(out options).Output == OutputOption.git && Contract.ValueAtReturn(out options).GitRoot != null) 
      //    ^ (Contract.ValueAtReturn(out options).Output == OutputOption.inplace))
      //);
      #endregion CodeContracts

      options = new Options();
      why = null;

      for (var i = 0; i < args.Length; i++)
      {
        var arg = args[i];
        if (IsOption(arg, out arg))
        {
          switch (arg)
          {
            case "project":
              if (i == args.Length - 1)
              {
                why = "The last argument can't be a keyword";
                return false;
              }
              options.Project = args[++i];
              break;

            case "source":
              if (i == args.Length - 1)
              {
                why = "The last argument can't be a keyword";
                return false;
              }
              options.SourceFile = args[++i];
              break;

            case "break":
              System.Diagnostics.Debugger.Launch();
              break;

            case "solution":
              if (i == args.Length - 1)
              {
                why = "The last argument can't be a keyword";
                return false;
              }
              options.Solution = args[++i];
              break;

            case "output":
              if (i == args.Length - 1)
              {
                why = "The last argument can't be a keyword";
                return false;
              }
              OutputOption oo;
              if (Enum.TryParse(args[++i], true, out oo)) 
              {
                options.Output = oo;
              }
              else
              {
                why = "Unrecognized output option: " + args[i];
                return false;
              }
              break;

            case "gitroot":
              if (i == args.Length - 1)
              {
                why = "The last argument can't be a keyword";
                return false;
              }
              options.GitRoot = args[++i];
              if (!Directory.Exists(options.GitRoot))
              {
                why = "git root directory must exist";
                return false;
              }
              break;

            default:
              options = null;
              why = "Unrecognized option " + arg;
              RBLogger.Error("Invalid option {0}", arg);
              return false;
          }
        }
        else
        {
          if (options.ClousotXML != null)
          {
            why = "Cannot express two (or more) .xml files";
            return false;
          }
          else
          {
            options.ClousotXML = args[0];
          }
        }
      }

      return options.CheckRequiredArguments(ref why);
    }
Example #2
0
        /// <summary>
        /// Parse the args into an Option object
        /// </summary>
        /// <param name="args">the args param to Main</param>
        /// <param name="options">the parsed arguements, valid if return is ture</param>
        /// <param name="why">an error message if parsing fails and the return is false</param>
        /// <returns>true if parsing succeeded, false otherwise</returns>
        internal static bool TryParseOptions(string[] args, out Options options, out string why)
        {
            #region CodeContracts
            Contract.Requires(Contract.ForAll(args, arg => arg != null));
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out options) != null);
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out options).Project != null);
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out options).Solution != null);
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out options).ClousotXML != null);
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out options).Output != OutputOption.unintialized);
            Contract.Ensures(Contract.Result <bool>() || Contract.ValueAtReturn(out why) != null);
            // there should be a contract for the output option, but it's complicated
            //Contract.Ensures(
            //    !Contract.Result<bool>()
            // || ((Contract.ValueAtReturn(out options).Output == OutputOption.git && Contract.ValueAtReturn(out options).GitRoot != null)
            //    ^ (Contract.ValueAtReturn(out options).Output == OutputOption.inplace))
            //);
            #endregion CodeContracts

            options = new Options();
            why     = null;

            for (var i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                if (IsOption(arg, out arg))
                {
                    switch (arg)
                    {
                    case "project":
                        if (i == args.Length - 1)
                        {
                            why = "The last argument can't be a keyword";
                            return(false);
                        }
                        options.Project = args[++i];
                        break;

                    case "source":
                        if (i == args.Length - 1)
                        {
                            why = "The last argument can't be a keyword";
                            return(false);
                        }
                        options.SourceFile = args[++i];
                        break;

                    case "break":
                        System.Diagnostics.Debugger.Launch();
                        break;

                    case "solution":
                        if (i == args.Length - 1)
                        {
                            why = "The last argument can't be a keyword";
                            return(false);
                        }
                        options.Solution = args[++i];
                        break;

                    case "output":
                        if (i == args.Length - 1)
                        {
                            why = "The last argument can't be a keyword";
                            return(false);
                        }
                        OutputOption oo;
                        if (Enum.TryParse(args[++i], true, out oo))
                        {
                            options.Output = oo;
                        }
                        else
                        {
                            why = "Unrecognized output option: " + args[i];
                            return(false);
                        }
                        break;

                    case "gitroot":
                        if (i == args.Length - 1)
                        {
                            why = "The last argument can't be a keyword";
                            return(false);
                        }
                        options.GitRoot = args[++i];
                        if (!Directory.Exists(options.GitRoot))
                        {
                            why = "git root directory must exist";
                            return(false);
                        }
                        break;

                    default:
                        options = null;
                        why     = "Unrecognized option " + arg;
                        RBLogger.Error("Invalid option {0}", arg);
                        return(false);
                    }
                }
                else
                {
                    if (options.ClousotXML != null)
                    {
                        why = "Cannot express two (or more) .xml files";
                        return(false);
                    }
                    else
                    {
                        options.ClousotXML = args[0];
                    }
                }
            }

            return(options.CheckRequiredArguments(ref why));
        }