public void Execute(CommandExecutionContext context, string[] args) { _optionsParser.Parse(args); if (string.IsNullOrEmpty(Topic)) { Help(context); } else { var command = context.CommandLocator.FindCommand(Topic); if (command != null) { command.Help(context); } else { var topic = context.HelpLocator.FindTopic(Topic); if (topic != null) { topic.Help(context); } else { UltraConsole.WriteLine("Unknown topic: {0}", Topic); Help(context); } } } }
/// <summary> /// The entry point for the application. /// </summary> /// <param name="args">Command line arguments.</param> internal static void Main(string[] args) { try { var context = new CommandExecutionContext( new CommandLocator(Assembly.GetExecutingAssembly()), new HelpTopicLocator(Assembly.GetExecutingAssembly()) ); var command = (args.Length == 0 ? new MenuCommand() : context.CommandLocator.FindCommand(args)) ?? new HelpCommand(); command.Execute(context, args); } catch (CommandLineException e) { UltraConsole.WriteLine("________________________________________________________________________________"); UltraConsole.WriteLine("ERROR: " + e); UltraConsole.WriteLine("________________________________________________________________________________"); if (string.IsNullOrEmpty(e.HelpTopic)) { UltraConsole.WriteLine("Try help: database.exe help {0}", e.HelpTopic); } else { UltraConsole.WriteLine("Try help: database.exe help"); } UltraConsole.PressAnyKeyToContinue(); } catch (Exception e) { UltraConsole.WriteLine("________________________________________________________________________________"); UltraConsole.WriteLine("ERROR: " + e); UltraConsole.WriteLine("________________________________________________________________________________"); UltraConsole.PressAnyKeyToContinue(); } }
internal static IPromptedCommand GetCommand(string mode, CommandExecutionContext context) { mode = mode.ToLower(CultureInfo.CurrentCulture); var command = context.CommandLocator.FindCommand(mode) as IPromptedCommand; if (command == null) { throw new InvalidOperationException(string.Format("Invalid mode ('{0}')", mode)); } return command; }
public void Help(CommandExecutionContext context) { var examples = context.CommandLocator.FindAllCommands().SelectMany(command => ReflectionHelper.GetAttributesValue<ExampleAttribute, string>(command, desc => desc.Example, desc => desc.ShowOnDefaultHelp)).OrderBy(e => e); UltraConsole.WriteParagraph(context.ApplicationDescription); UltraConsole.WriteHeading("Usage: "); UltraConsole.WriteList( string.Format("{0} help", context.ApplicationExeFileName), string.Format("{0} COMMAND [arguments] [options]", context.ApplicationExeFileName) ); UltraConsole.WriteHeading("Examples: "); UltraConsole.WriteList(examples.Select(ex => ex.Replace("ApplicationExeFileName", context.ApplicationExeFileName))); UltraConsole.WriteHeading("Further help: "); UltraConsole.WriteColumns(string.Format("{0} help commands", context.ApplicationExeFileName), "Lists all available commands"); UltraConsole.WriteColumns(string.Format("{0} help COMMAND", context.ApplicationExeFileName), "Get help for a particular command"); }
protected override void Execute(CommandExecutionContext context) { while (true) { var mode = UltraConsole.PromptForOptions("Please select a command:", new Dictionary<string, string> { { "i", "[I]nstall" }, { "s", "[S]tatus" }, { "e", "[E]xit"} }); if (mode == "e") return; var command = GetCommand(mode, context); command.ExecutePrompted(context); } }
public void Help(CommandExecutionContext context) { var name = ReflectionHelper.GetAttributeValue<NameAttribute, string>(this, desc => desc.Name, "[No name given]"); var description = ReflectionHelper.GetAttributeValue<DescriptionAttribute, string>(this, desc => desc.Description, "[No description given]"); var usage = ReflectionHelper.GetAttributeValue<DefinitionAttribute, string>(this, desc => desc.Definition, "[No definition given]"); var examples = ReflectionHelper.GetAttributesValue<ExampleAttribute, string>(this, desc => desc.Example).OrderBy(e => e); UltraConsole.WriteHeading(name); UltraConsole.Indent().WriteParagraph(description); UltraConsole.WriteHeading("Usage: "); UltraConsole.Indent().WriteParagraph(usage); UltraConsole.WriteHeading("Examples: "); UltraConsole.WriteList(examples); UltraConsole.WriteHeading("Options: "); OptionsParser.WriteOptions(System.Console.Out); UltraConsole.WriteHeading("Arguments: "); OptionsParser.WriteArguments(System.Console.Out); }
protected override void Execute(CommandExecutionContext context) { if (string.IsNullOrEmpty(ConnectionString) && ConnectionStringBuilder != null) { ConnectionString = ConnectionStringBuilder.ToString(); } if (string.IsNullOrEmpty(ConnectionString) && ConnectionStringBuilder != null) { ConnectionString = "Server=(local);Database=BindableCMS;trusted_connection=yes;"; } try { // Validate the connection string SqlDatabaseHelper.ValidateConnectionStringOrThrow(ConnectionString); // Ensure the database exists - if not, create it UltraConsole.WriteHeading("Database upgrade"); UltraConsole.WriteParagraph(" Connection string: {0}", ConnectionString); UltraConsole.WriteLine("Ensuring database exists..."); var manager = new ApplicationDatabase(ConnectionString); if (!manager.DoesDatabaseExist()) { UltraConsole.WriteLine("Database does not exist. "); return; } UltraConsole.WriteLine("Database exists."); UltraConsole.WriteHeading("Getting information about current setup..."); using (UltraConsole.Indent()) { UltraConsole.WriteColumns("Database version:", manager.GetCurrentVersion().ToString()); UltraConsole.WriteColumns("Can be upgraded to:", manager.GetApplicationVersion().ToString()); } // Display the result UltraConsole.WriteLine(ConsoleColor.Green, "Success"); } catch (FormatException ex) { UltraConsole.WriteLine(ConsoleColor.Red, "Connection string was invalid: {0}", ex.Message); return; } }
public void Help(CommandExecutionContext context) { var commands = context.CommandLocator.FindAllCommands().Select( cmd => new { Name = ReflectionHelper.GetAttributeValue<NameAttribute, string>(cmd, n => n.Name, ""), Description = ReflectionHelper.GetAttributeValue<DescriptionAttribute, string>(cmd, n => n.Description, ""), }) .OrderBy(cmd => cmd.Name); UltraConsole.WriteParagraph("The following commands are available for {0}.", context.ApplicationTitle); UltraConsole.WriteHeading("Commands: "); UltraConsole.WriteTable(commands, table => { table.ShowHeaders = false; table.AddColumn("Command", cmd => cmd.Name); table.AddColumn("Description", cmd => cmd.Description); } ); UltraConsole.WriteHeading("Further help: "); UltraConsole.WriteColumns(string.Format("{0} help COMMAND", context.ApplicationExeFileName), "Get help for a particular command"); }
protected override void Execute(CommandExecutionContext context) { if (string.IsNullOrEmpty(ConnectionString) && ConnectionStringBuilder != null) { ConnectionString = ConnectionStringBuilder.ToString(); } if (string.IsNullOrEmpty(ConnectionString) && ConnectionStringBuilder != null) { ConnectionString = "Server=(local);Database=BindableCMS;trusted_connection=yes;"; } try { SqlDatabaseHelper.ValidateConnectionStringOrThrow(ConnectionString); UltraConsole.WriteHeading("Database upgrade"); UltraConsole.WriteParagraph(" Connection string: {0}", ConnectionString); UltraConsole.WriteLine("Ensuring database exists..."); var manager = new ApplicationDatabase(ConnectionString); if (!manager.DoesDatabaseExist()) { UltraConsole.WriteLine("Database does not exist. "); if (!NoCreate) { UltraConsole.WriteLine("Creating database..."); manager.CreateDatabase(); UltraConsole.WriteLine("Database created"); } else { return; } } else { UltraConsole.WriteLine("Database exists."); } UltraConsole.WriteHeading("Granting access to user '{0}'", UserAccount); manager.GrantAccessToLogin(UserAccount); UltraConsole.WriteHeading("Getting information about current setup..."); using (UltraConsole.Indent()) { UltraConsole.WriteColumns("Current database version:", manager.GetCurrentVersion().ToString()); UltraConsole.WriteColumns("Will be upgraded to:", manager.GetApplicationVersion().ToString()); } UltraConsole.WriteHeading("Performing upgrade..."); var result = manager.PerformUpgrade(); UltraConsole.WriteParagraph("Upgraded from {0} to {1}", result.OriginalVersion, result.UpgradedVersion); if (result.Scripts != null && result.Scripts.Count() > 0) { UltraConsole.WriteHeading("The following scripts were executed:"); UltraConsole.WriteTable( result.Scripts, table => table.AddColumn("Version", script => script.VersionNumber) ); } if (result.Successful) { UltraConsole.WriteLine(ConsoleColor.Green, "Database upgrade was successful."); } else { UltraConsole.WriteLine(ConsoleColor.Red, "Database upgrade failed. Please see the list of scripts that were executed above and the error below."); UltraConsole.WriteLine(result.Error.ToString()); } } catch (FormatException ex) { UltraConsole.WriteLine(ConsoleColor.Red, "Connection string was invalid: {0}", ex.Message); } }
protected abstract void Execute(CommandExecutionContext context);
public void Execute(CommandExecutionContext context, string[] args) { OptionsParser.Parse(args); Execute(context); }