- Timestamp:
- 09/21/12 14:41:56 (12 years ago)
- 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 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 26 27 namespace HeuristicLab.PluginInfrastructure { 27 28 public static class ArgumentHandling { 28 public const string StartToken = "start";29 30 29 public static IArgument[] GetArguments(string[] args) { 31 30 var arguments = new HashSet<IArgument>(); 31 var exceptions = new List<Exception>(); 32 32 33 foreach (var entry in args) { 33 34 var argument = ParseArgument(entry); 34 35 if (argument != null && argument.Valid) arguments.Add(argument); 36 else exceptions.Add(new ArgumentException(string.Format("The argument \"{0}\" is invalid.", entry))); 35 37 } 38 39 if (exceptions.Any()) throw new AggregateException("One or more arguments are invalid.", exceptions); 36 40 return arguments.ToArray(); 37 41 } 38 42 39 43 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]+)?$"); 41 45 if (!regex.IsMatch(entry)) return null; 42 46 entry = entry.Remove(0, 1); … … 44 48 string key = parts[0]; 45 49 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); 50 51 } 51 52 } -
trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ArgumentHandling/Arguments.cs
r8563 r8677 21 21 22 22 namespace 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; } 24 27 public string Value { get; private set; } 25 public abstract bool Valid { get;}28 public bool Valid { get { return !string.IsNullOrEmpty(Value); } } 26 29 27 protected Argument(string value) { 28 this.Value = value; 30 public Argument(string token, string value) { 31 Token = token; 32 Value = value; 29 33 } 30 34 31 35 public override bool Equals(object obj) { 32 36 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; 34 39 } 35 40 … … 38 43 } 39 44 } 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 }48 45 } -
trunk/sources/HeuristicLab.PluginInfrastructure/3.3/ArgumentHandling/IArgument.cs
r8563 r8677 22 22 namespace HeuristicLab.PluginInfrastructure { 23 23 public interface IArgument { 24 string Token { get; } 24 25 string Value { get; } 25 26 bool Valid { get; }
Note: See TracChangeset
for help on using the changeset viewer.