Changeset 8677 for trunk/sources
- Timestamp:
- 09/21/12 14:41:56 (12 years ago)
- 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 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; } -
trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Starter/StarterForm.cs
r8563 r8677 111 111 112 112 /// <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.Applications119 where desc.Name == appName120 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>132 113 /// Creates a new StarterForm and passes the arguments in <paramref name="args"/>. 133 114 /// </summary> … … 139 120 140 121 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); 155 141 } 156 142 }
Note: See TracChangeset
for help on using the changeset viewer.