Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8677


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
Files:
4 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; }
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Starter/StarterForm.cs

    r8563 r8677  
    111111
    112112    /// <summary>
    113     /// Creates a new StarterForm and tries to start application with <paramref name="appName"/> immediately.
    114     /// </summary>
    115     /// <param name="appName">Name of the application</param>
    116     public StarterForm(string appName)
    117       : this() {
    118       var appDesc = (from desc in pluginManager.Applications
    119                      where desc.Name == appName
    120                      select desc).SingleOrDefault();
    121       if (appDesc != null) {
    122         StartApplication(appDesc);
    123       } else {
    124         MessageBox.Show("Cannot start application " + appName + ".",
    125                         "HeuristicLab",
    126                         MessageBoxButtons.OK,
    127                         MessageBoxIcon.Warning);
    128       }
    129     }
    130 
    131     /// <summary>
    132113    /// Creates a new StarterForm and passes the arguments in <paramref name="args"/>.
    133114    /// </summary>
     
    139120
    140121    private void StarterForm_Shown(object sender, EventArgs e) {
    141       foreach (var argument in ArgumentHandling.GetArguments(arguments)) {
    142         if (argument is StartArgument) {
    143           var appDesc = (from desc in pluginManager.Applications
    144                          where desc.Name == argument.Value
    145                          select desc).SingleOrDefault();
    146           if (appDesc != null) {
    147             StartApplication(appDesc);
    148           } else {
    149             MessageBox.Show("Cannot start application " + argument.Value + ".",
    150                             "HeuristicLab",
    151                             MessageBoxButtons.OK,
    152                             MessageBoxIcon.Warning);
    153           }
    154         }
     122      try {
     123        foreach (var argument in ArgumentHandling.GetArguments(arguments)) {
     124          if (argument.Token == Argument.START) {
     125            var appDesc = (from desc in pluginManager.Applications
     126                           where desc.Name == argument.Value
     127                           select desc).SingleOrDefault();
     128            if (appDesc != null) {
     129              StartApplication(appDesc);
     130            } else {
     131              MessageBox.Show("Cannot start application " + argument.Value + ".",
     132                              "HeuristicLab",
     133                              MessageBoxButtons.OK,
     134                              MessageBoxIcon.Warning);
     135            }
     136          }
     137        }
     138      }
     139      catch (AggregateException ex) {
     140        ErrorHandling.ShowErrorDialog(this, "One or more errors occurred while initializing the application.", ex);
    155141      }
    156142    }
Note: See TracChangeset for help on using the changeset viewer.