예제 #1
0
    public override CommandResult Run()
    {

      if (!Add && !Remove && !Shuffle && !OrderList)
        return new CommandResult(CommandStatus.Failure, "Missing one of -a -r -s -o required flag");

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

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

      if (string.IsNullOrEmpty(Element) && !Shuffle && !OrderList)
        return new CommandResult(CommandStatus.Failure, Constants.Messages.MissingRequiredParameter.FormatWith("element"));

      if (Shuffle && OrderList)
        return new CommandResult(CommandStatus.Failure, "Cannot use -s and -o together");

      var elements = new List<string>();
      elements.AddRange(Input.Split(new[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries));

      if (Add)
      {
        if (!elements.Contains(Element))
          elements.Add(Element);
      }
      else if (Remove)
      {
        if (elements.Contains(Element))
          elements.Remove(Element);
      }
      else if (Shuffle)
      {
        System.Random random;
        
        if(_randomSeed != null)
          random = new System.Random(_randomSeed.Value);
        else
          random = new System.Random();

        elements = (from element in elements
                    orderby random.Next()
                    select element).ToList();
      }
      else if (OrderList)
      {
        elements = (from element in elements
                    orderby element
                    select element).ToList();
      }
      else
        return new CommandResult(CommandStatus.Failure, "Unknown operation");

      if (ReverseOrder)
        elements.Reverse();

      return new CommandResult(CommandStatus.Success, string.Join(Delimiter, elements.ToArray()));
    }