public void Parse_should_find_named_arguments_with_no_value() { // arrange var args = new[] { "-help", "-me" }; // act ArgumentSet set = ArgumentSet.Parse(args); // assert Assert.AreEqual(2, set.NamedArgs.Count()); Assert.AreEqual(null, set.GetByName("help")); Assert.AreEqual(null, set.GetByName("me")); }
public void Parse_should_match_multiple_named_arguments_and_values() { // arrange var args = new[] { "-help", "migrate", "-c", "connection" }; // act ArgumentSet set = ArgumentSet.Parse(args); // assert Assert.AreEqual(2, set.NamedArgs.Count()); Assert.AreEqual("migrate", set.GetByName("help")); Assert.AreEqual("connection", set.GetByName("c")); }
public void GetByName_should_throw_KeyNotFoundException_if_name_doesnt_exist() { // arrange var args = new[] { "blah" }; ArgumentSet set = ArgumentSet.Parse(args); // act Assert.Throws <KeyNotFoundException>(() => set.GetByName("help")); }
public void GetByName_should_return_value_of_argument_with_given_name() { // arrange var args = new[] { "-help", "migrate" }; ArgumentSet set = ArgumentSet.Parse(args); // act string value = set.GetByName("help"); // assert const string expectedValue = "migrate"; Assert.AreEqual(expectedValue, value); }
/// <summary> /// Primary Program Execution /// </summary> private void Run(string[] args) { string executableName = Process.GetCurrentProcess().ProcessName + ".exe"; ArgumentSet allArguments = ArgumentSet.Parse(args); var helpWriter = new CommandHelpWriter(_logger); bool showHelp = allArguments.ContainsName("help"); string commandName = showHelp ? allArguments.GetByName("help") : allArguments.AnonymousArgs.FirstOrDefault(); ICommand command = null; if (commandName != null) { command = _commandRepo.GetCommand(commandName); } if (command == null) { if (showHelp) { // no command name was found, show the list of available commands WriteAppUsageHelp(executableName); helpWriter.WriteCommandList(_commandRepo.Commands); } else { // invalid command name was given _logger.WriteError("'{0}' is not a DotNetMigrations command.", commandName); _logger.WriteLine(string.Empty); _logger.WriteError("See '{0} -help' for a list of available commands.", executableName); } } if (showHelp && command != null) { // show help for the given command helpWriter.WriteCommandHelp(command, executableName); } else if (command != null) { command.Log = _logger; var commandArgumentSet = ArgumentSet.Parse(args.Skip(1).ToArray()); IArguments commandArgs = command.CreateArguments(); commandArgs.Parse(commandArgumentSet); if (commandArgs.IsValid) { var timer = new Stopwatch(); timer.Start(); try { command.Run(commandArgs); } catch (Exception ex) { //_logger.WriteLine(string.Empty); if (_logFullErrors) { _logger.WriteError(ex.ToString()); } else { WriteShortErrorMessages(ex); } if (Debugger.IsAttached) { throw; } } finally { timer.Stop(); _logger.WriteLine(string.Empty); _logger.WriteLine(string.Format("Command duration was {0}.", decimal.Divide(timer.ElapsedMilliseconds, 1000).ToString( "0.0000s"))); } } else { // argument validation failed, show errors WriteValidationErrors(command.CommandName, commandArgs.Errors); _logger.WriteLine(string.Empty); helpWriter.WriteCommandHelp(command, executableName); } } if (_keepConsoleOpen) { Console.WriteLine(); Console.WriteLine(" > Uh-oh. It looks like you didn't run me from a console."); Console.WriteLine(" > Did you double-click me?"); Console.WriteLine(" > I don't like to be clicked."); Console.WriteLine(" > Please open a command prompt and run me by typing " + executableName + "."); Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.Read(); } }