public static void Main(string[] args) { var cmd = new Commandline(args); var da = new Dataaccess(); var ui = new UI(); var app = new Controller(cmd, ui, da); app.Run(args); }
public static int Main(string[] args) { if (args.Length > 0 && args[0] == TemplateCreator.Verb) { return(TemplateCreator.Run(args)); } //Cannot continue with statics or in the same class here as otherwise references to other libs (like CommandLineParser) get loaded, which may //not be available when running in a completely different load context of another project when creating templates. return(Commandline.ParseAndExecute(args)); }
private void RenesSerialDataReceived(object sender, SerialDataReceivedEventArgs e) { Commandline.Invoke( new EventHandler( delegate { string s = sPort.ReadExisting(); Feedback.AppendText(s); Feedback.SelectionStart = Feedback.Text.Length; Feedback.ScrollToCaret(); } ) ); }
/// <summary> /// Delete the configuration for the specified command /// </summary> /// <param name="command">string with command</param> public void Delete(string command) { if (string.IsNullOrEmpty(command)) { return; } Commands.Remove(command); Commandline.Remove(command); Argument.Remove(command); RunInbackground.Remove(command); if (MsPaint.Equals(command) || PaintDotNet.Equals(command)) { if (!DeletedBuildInCommands.Contains(command)) { DeletedBuildInCommands.Add(command); } } }
public override void AfterLoad() { base.AfterLoad(); // Check if we need to add MsPaint if (HasPaint && !Commands.Contains(MsPaint) && !DeletedBuildInCommands.Contains(MsPaint)) { Commands.Add(MsPaint); Commandline.Add(MsPaint, PaintPath); Argument.Add(MsPaint, "\"{0}\""); RunInbackground.Add(MsPaint, true); } // Check if we need to add Paint.NET if (HasPaintDotNet && !Commands.Contains(PaintDotNet) && !DeletedBuildInCommands.Contains(PaintDotNet)) { Commands.Add(PaintDotNet); Commandline.Add(PaintDotNet, PaintDotNetPath); Argument.Add(PaintDotNet, "\"{0}\""); RunInbackground.Add(PaintDotNet, true); } }
private static void RunScript(string script, string[] args) { lock (psLocker) { powershellHost = new Host(); powershellEngine = System.Management.Automation.PowerShell.Create(); } try { InitialSessionState initalState = InitialSessionState.CreateDefault(); List <ParameterItem> validCmds = new List <ParameterItem>(); //AddValidCommands Commandline cmdLine = new Commandline(args); if (cmdLine["help"] == "true" || cmdLine["?"] == "true") { AssemblyData assInfo = new AssemblyData(System.Reflection.Assembly.GetExecutingAssembly()); StringBuilder outputBuilder = new StringBuilder(); outputBuilder.AppendLine(); outputBuilder.AppendLine(string.Format("{0} v{1} by {2}", assInfo.Product, assInfo.Version, assInfo.Company)); outputBuilder.AppendLine(assInfo.Copyright); outputBuilder.AppendLine(); outputBuilder.AppendLine(" [-Help]"); outputBuilder.AppendLine(" Show help"); foreach (ParameterItem cmdName in validCmds) { if (cmdName.Mandatory) { outputBuilder.AppendLine(string.Format(" -{0} <{1}>", cmdName.Name, cmdName.Type)); } else { outputBuilder.AppendLine(string.Format(" [-{0} <{1}>]", cmdName.Name, cmdName.Type)); } if (!string.IsNullOrWhiteSpace(cmdName.HelpText)) { outputBuilder.AppendLine(string.Format(" {0}", cmdName.HelpText)); } } if (hideCon) { MessageBox.Show(outputBuilder.ToString(), "Help", MessageBoxButtons.OK, MessageBoxIcon.Question); } else { ConsoleHandler.WriteLine(outputBuilder.ToString()); } return; } Dictionary <string, object> cmdLineArgs = new Dictionary <string, object>(); foreach (string arg in cmdLine.GetKeys()) { ParameterItem paramItem = validCmds.FirstOrDefault(x => String.Equals(x.Name, arg, StringComparison.CurrentCultureIgnoreCase)); if (paramItem != null) { try { object realItem; switch (paramItem.Type) { case "sbyte": realItem = sbyte.Parse(cmdLine[arg]); break; case "byte": realItem = byte.Parse(cmdLine[arg]); break; case "short": realItem = short.Parse(cmdLine[arg]); break; case "ushort": realItem = ushort.Parse(cmdLine[arg]); break; case "int": realItem = int.Parse(cmdLine[arg]); break; case "uint": realItem = uint.Parse(cmdLine[arg]); break; case "ulong": realItem = ulong.Parse(cmdLine[arg]); break; case "long": realItem = long.Parse(cmdLine[arg]); break; case "float": realItem = float.Parse(cmdLine[arg]); break; case "double": realItem = double.Parse(cmdLine[arg]); break; case "decimal": realItem = decimal.Parse(cmdLine[arg]); break; case "char": realItem = char.Parse(cmdLine[arg]); break; case "switch": case "bool": realItem = bool.Parse(cmdLine[arg]); break; case "boolean": realItem = Boolean.Parse(cmdLine[arg]); break; default: realItem = cmdLine[arg]; break; } cmdLineArgs.Add(arg, realItem); } catch (Exception) { string errorString = string.Format("Parameter '-{0}' was not in correct format: '{1}'", arg, paramItem.Type); if (hideCon) { MessageBox.Show(errorString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { ConsoleHandler.WriteLine(errorString); } return; } } else { StringBuilder outputBuilder = new StringBuilder(); outputBuilder.AppendLine(string.Format("Parameter '-{0}' is not valid. Use '-help' to show valid parameters.", arg)); if (hideCon) { MessageBox.Show(outputBuilder.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { ConsoleHandler.WriteLine(outputBuilder.ToString()); } return; } } foreach (ParameterItem paramItem in validCmds.Where(x => x.Mandatory == true)) { if (!cmdLineArgs.ContainsKey(paramItem.Name.ToLower())) { StringBuilder outputBuilder = new StringBuilder(); outputBuilder.AppendLine(string.Format("Parameter '-{0}' of type '{1}' is mandatory.", paramItem.Name, paramItem.Type)); if (hideCon) { MessageBox.Show(outputBuilder.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { ConsoleHandler.WriteLine(outputBuilder.ToString()); } return; } } List <string> modulesToLoad = Common.ProcessManifest(initalState); powershellEngine.Runspace = RunspaceFactory.CreateRunspace(powershellHost, initalState); powershellEngine.Runspace.ApartmentState = System.Threading.ApartmentState.MTA; powershellEngine.Runspace.Open(); RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(powershellEngine.Runspace); try { runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope Process"); } catch { } foreach (string module in modulesToLoad) { try { runSpaceInvoker.Invoke(string.Format("Import-Module {0} -Scope Global", module)); } catch (Exception e) { ConsoleHandler.WriteLine(string.Format("Could import module {0}: {0}", e.Message, module)); } } Pipeline pipeline = powershellEngine.Runspace.CreatePipeline(); Command command = new Command(script, true, true); foreach (KeyValuePair <string, object> cmdLineData in cmdLineArgs) { command.Parameters.Add(cmdLineData.Key, cmdLineData.Value); } pipeline.Commands.Add(command); Collection <PSObject> resultObjects = pipeline.Invoke(); foreach (PSObject resultObject in resultObjects) { ConsoleHandler.WriteLine(resultObject.ToString()); } } catch (Exception e) { ConsoleHandler.WriteLine(string.Format("Internal error: {0}", e.Message)); } finally { lock (psLocker) { powershellEngine.Dispose(); powershellEngine = null; } } }
static void Main(string[] args) { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture; if (System.Diagnostics.Process.GetCurrentProcess().SessionId == 0) { ConsoleHandler.ConsoleLoaded = false; ServiceBase.Run(new ServiceBase[] { new PSService() }); } else { ConsoleHandler.ConsoleLoaded = true; if (!AttachConsole(-1)) { AllocConsole(); } Commandline commandline = new Commandline(args); if (commandline["debug"] == "true") { PSService app = new PSService(); app.StartInternal(); string input = string.Empty; ConsoleHandler.WriteLine(""); ConsoleHandler.WriteLine("Application started in debug mode."); ConsoleHandler.WriteLine(""); while (input.ToLower().Trim() != "quit") { ConsoleHandler.Write("Type 'quit' to stop the application: "); input = ConsoleHandler.ReadLine(); } ConsoleHandler.WriteLine(""); ConsoleHandler.WriteLine("Exiting..."); app.StopInternal(); ConsoleHandler.WriteLine(""); } else if (commandline["install"] == "true") { ConsoleHandler.WriteLine(""); ServiceStartMode startMode = ServiceStartMode.Disabled; switch (commandline["startmode"].ToLower()) { case "automatic": startMode = ServiceStartMode.Automatic; break; case "disabled": startMode = ServiceStartMode.Disabled; break; default: startMode = ServiceStartMode.Manual; break; } ServiceAccount serviceAccount = ServiceAccount.LocalSystem; switch (commandline["account"].ToLower()) { case "localservice": serviceAccount = ServiceAccount.LocalService; break; case "networkservice": serviceAccount = ServiceAccount.NetworkService; break; case "user": serviceAccount = ServiceAccount.User; break; default: serviceAccount = ServiceAccount.LocalSystem; break; } PSService.InstallService(serviceAccount, startMode, commandline["username"], commandline["password"]); ConsoleHandler.WriteLine(""); } else if (commandline["uninstall"] == "true") { ConsoleHandler.WriteLine(""); PSService.UninstallService(); ConsoleHandler.WriteLine(""); } else { ConsoleHandler.WriteLine(""); ConsoleHandler.WriteLine("Usage information:"); ConsoleHandler.WriteLine(""); ConsoleHandler.WriteLine(" /install Installs the service, with optional arguments:"); ConsoleHandler.WriteLine(" /startmode:[automatic|manual|disabled] Start mode for the service"); ConsoleHandler.WriteLine(" /account:[localsystem|localservice|networkservice|user] The account for the service"); ConsoleHandler.WriteLine(" /username:<username> Only if /account:user"); ConsoleHandler.WriteLine(" /password:<password> Only if /account:password"); ConsoleHandler.WriteLine(""); ConsoleHandler.WriteLine(" /uninstall Removes the application as a service in Windows"); ConsoleHandler.WriteLine(""); ConsoleHandler.WriteLine(" /debug Starts the application as a local console application for debug"); ConsoleHandler.WriteLine(""); } } }
Controller(Commandline cmd, UI ui, Dataaccess da) { _cmd = cmd; _ui = ui; _da = da; }