Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/21/12 14:41:56 (12 years ago)
Author:
jkarder
Message:

#1926: refactored argument handling infrastructure based on the changes suggested by ascheibe in comment:7:ticket:1926

Location:
trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ArgumentHandling
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ArgumentHandling/ArgumentHandling.cs

    r8563 r8677  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    2627namespace HeuristicLab.PluginInfrastructure {
    2728  public static class ArgumentHandling {
    28     public const string StartToken = "start";
    29 
    3029    public static IArgument[] GetArguments(string[] args) {
    3130      var arguments = new HashSet<IArgument>();
     31      var exceptions = new List<Exception>();
     32
    3233      foreach (var entry in args) {
    3334        var argument = ParseArgument(entry);
    3435        if (argument != null && argument.Valid) arguments.Add(argument);
     36        else exceptions.Add(new ArgumentException(string.Format("The argument \"{0}\" is invalid.", entry)));
    3537      }
     38
     39      if (exceptions.Any()) throw new AggregateException("One or more arguments are invalid.", exceptions);
    3640      return arguments.ToArray();
    3741    }
    3842
    3943    private static Argument ParseArgument(string entry) {
    40       var regex = new Regex("^/[a-z]+(:[A-Za-z0-9 ]+)?$");
     44      var regex = new Regex(@"^/[a-z]+(:[A-Za-z0-9\s]+)?$");
    4145      if (!regex.IsMatch(entry)) return null;
    4246      entry = entry.Remove(0, 1);
     
    4448      string key = parts[0];
    4549      string value = parts.Length == 2 ? parts[1].Trim() : string.Empty;
    46       switch (key) {
    47         case StartToken: return new StartArgument(value);
    48         default: return null;
    49       }
     50      return new Argument(key.ToLower(), value);
    5051    }
    5152  }
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ArgumentHandling/Arguments.cs

    r8563 r8677  
    2121
    2222namespace HeuristicLab.PluginInfrastructure {
    23   public abstract class Argument : IArgument {
     23  public class Argument : IArgument {
     24    public const string START = "start";
     25
     26    public string Token { get; private set; }
    2427    public string Value { get; private set; }
    25     public abstract bool Valid { get; }
     28    public bool Valid { get { return !string.IsNullOrEmpty(Value); } }
    2629
    27     protected Argument(string value) {
    28       this.Value = value;
     30    public Argument(string token, string value) {
     31      Token = token;
     32      Value = value;
    2933    }
    3034
    3135    public override bool Equals(object obj) {
    3236      if (obj == null || this.GetType() != obj.GetType()) return false;
    33       return this.Value == ((Argument)obj).Value;
     37      var other = (Argument)obj;
     38      return this.Token == other.Token && this.Value == other.Value;
    3439    }
    3540
     
    3843    }
    3944  }
    40 
    41   public class StartArgument : Argument {
    42     public override bool Valid {
    43       get { return !string.IsNullOrEmpty(Value); }
    44     }
    45 
    46     public StartArgument(string value) : base(value) { }
    47   }
    4845}
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ArgumentHandling/IArgument.cs

    r8563 r8677  
    2222namespace HeuristicLab.PluginInfrastructure {
    2323  public interface IArgument {
     24    string Token { get; }
    2425    string Value { get; }
    2526    bool Valid { get; }
Note: See TracChangeset for help on using the changeset viewer.