コード例 #1
0
ファイル: Grep.cs プロジェクト: KerwinMa/revolver
    public override CommandResult Run()
    {
      if (string.IsNullOrEmpty(Regex))
        return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("regex"));

      if (string.IsNullOrEmpty(Input))
        return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("input"));

      var options = RegexOptions.Compiled;
      if (!CaseSensitive)
        options |= RegexOptions.IgnoreCase;

      var regex = new Regex(Regex, options);

      var lines = Formatter.SplitLines(Input);
      var outputLines = new List<string>();

      foreach (var line in lines)
      {
        var isMatch = regex.IsMatch(line);
        if (isMatch ^ NotMatching)
          outputLines.Add(line);
      }

      var output = new StringBuilder();
      outputLines.ForEach(x => Formatter.PrintLine(x, output));
      return new CommandResult(CommandStatus.Success, output.ToString());
    }