From 314ace69ff5d8813ec8f74149c43df757c6971eb Mon Sep 17 00:00:00 2001 From: Marcelo Grossi Date: Mon, 3 Oct 2016 11:50:18 +0100 Subject: [PATCH 1/2] naive change to allow for cmd line args naive change to allow for cmd line args --- src/BigRunner.ConsoleApp/ArgsParser.cs | 87 +++++++++++++++++ .../BigRunner.ConsoleApp.csproj | 1 + src/BigRunner.ConsoleApp/Program.cs | 94 ++++++++++++++----- 3 files changed, 157 insertions(+), 25 deletions(-) create mode 100644 src/BigRunner.ConsoleApp/ArgsParser.cs diff --git a/src/BigRunner.ConsoleApp/ArgsParser.cs b/src/BigRunner.ConsoleApp/ArgsParser.cs new file mode 100644 index 0000000..68ad823 --- /dev/null +++ b/src/BigRunner.ConsoleApp/ArgsParser.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BigRunner.ConsoleApp +{ + public class ArgsParser + { + private List unidentifiedArgs; + private Dictionary identifiedArgs; + public ArgsParser(string[] args) + { + unidentifiedArgs = new List(); + identifiedArgs = new Dictionary(); + Parse(args); + } + + private void Parse(string[] args) + { + string curr = String.Empty; + foreach (var arg in args) + { + if (IsCommand(arg)) + { + if (!String.IsNullOrWhiteSpace(curr)) + { + // add value-less command to dictionary + identifiedArgs.Add(curr, String.Empty); + } + curr = GetKey(arg); + } + else + { + if (String.IsNullOrWhiteSpace(curr)) + { + // if value without command was informed it goes to special values list + unidentifiedArgs.Add(arg); + } + else + { + identifiedArgs.Add(curr, arg); + curr = String.Empty; + } + } + } + // add stand-alone option if it was the last cmd informed + if (!String.IsNullOrWhiteSpace(curr)) + { + identifiedArgs.Add(curr, String.Empty); + } + } + + private bool IsCommand(string arg) + { + return arg.StartsWith("-") || arg.StartsWith("--"); + } + + private string GetKey(string arg) + { + if (!IsCommand(arg)) + return arg; + else + if (arg.StartsWith("-")) + return arg.Substring(1); + else + return arg.Substring(2); + } + + public bool HasArg(string arg) + { + return identifiedArgs.ContainsKey(GetKey(arg)); + } + + public string GetArg(string arg) + { + if (!HasArg(arg)) + throw new ArgumentException(String.Format("Argument %s not found.", arg)); + return identifiedArgs[GetKey(arg)]; + } + + public List GetArgs() + { + return unidentifiedArgs; + } + } +} diff --git a/src/BigRunner.ConsoleApp/BigRunner.ConsoleApp.csproj b/src/BigRunner.ConsoleApp/BigRunner.ConsoleApp.csproj index 2723943..ffbb8b5 100644 --- a/src/BigRunner.ConsoleApp/BigRunner.ConsoleApp.csproj +++ b/src/BigRunner.ConsoleApp/BigRunner.ConsoleApp.csproj @@ -41,6 +41,7 @@ + diff --git a/src/BigRunner.ConsoleApp/Program.cs b/src/BigRunner.ConsoleApp/Program.cs index 82bcae3..a4aad43 100644 --- a/src/BigRunner.ConsoleApp/Program.cs +++ b/src/BigRunner.ConsoleApp/Program.cs @@ -50,11 +50,27 @@ Make notes on console screen Console.WriteLine("\t2. To terminate the console. Please press ESC key"); } - /// - /// Input connection string data - /// - /// Returns sql connection if okay, otherwise user will continue entering - private static SqlConnection InputConnectionStringData() + + /// + /// Print argument help + /// + private static void PrintHelp() + { + /********************************************** + Make notes on console screen + **********************************************/ + Console.WriteLine("Usage: BigRunner.ConsoleApp.exe -s -c -l [big script to be run.sql]"); + Console.WriteLine("Command line arguments:"); + Console.WriteLine("\t-s\tEnable silent mode. Will run the script and exit the application."); + Console.WriteLine("\t-c\tConnection string."); + Console.WriteLine("\t-l\tOutput log file."); + } + + /// + /// Input connection string data + /// + /// Returns sql connection if okay, otherwise user will continue entering + private static SqlConnection InputConnectionStringData() { var sqlConnection = new SqlConnection(); @@ -320,6 +336,8 @@ private static string CalElapsedTime(Stopwatch stopWatch) /// The parameters from command line are passed into this method static void Main(string[] args) { + ArgsParser argsParser = new ArgsParser(args); + /********************************************** Initialize these necessary input parameters **********************************************/ @@ -328,19 +346,42 @@ Initialize these necessary input parameters TextWriter logger = null; StreamReader reader = null; - PrintNotes(); - sqlConnection = InputConnectionStringData(); - reader = InputBigSqlScriptFilePathData(); - enabledLogToFile = InputEnabledLogToFileData(); - if (enabledLogToFile) - { - logger = InputLogFilePathData(); - } - - /********************************************** + // silent mode (works off of command line arguments) + if (argsParser.HasArg("s")) + { + try + { + sqlConnection = new SqlConnection(argsParser.GetArg("c")); + sqlConnection.Open(); + reader = new StreamReader(argsParser.GetArgs().FirstOrDefault()); + enabledLogToFile = argsParser.HasArg("l"); + if (enabledLogToFile) + { + logger = new StreamWriter(argsParser.GetArg("l")); + } + } + catch (Exception ex) + { + + } + } + else + { + PrintNotes(); + + sqlConnection = InputConnectionStringData(); + reader = InputBigSqlScriptFilePathData(); + enabledLogToFile = InputEnabledLogToFileData(); + if (enabledLogToFile) + { + logger = InputLogFilePathData(); + } + } + + /********************************************** Measure time of running sql script **********************************************/ - var stopWatch = new Stopwatch(); + var stopWatch = new Stopwatch(); stopWatch.Start(); try @@ -526,17 +567,20 @@ Stop watch and write the elapsed time to stopWatch.Stop(); Console.WriteLine(CalElapsedTime(stopWatch)); - /********************************************** + /********************************************** Wait until user enter ENTER key to exit **********************************************/ - while (true) - { - var consoleKey = Console.ReadKey().Key; - if (consoleKey == ConsoleKey.Escape) - { - break; - } - } + if (!argsParser.HasArg("s")) + { + while (true) + { + var consoleKey = Console.ReadKey().Key; + if (consoleKey == ConsoleKey.Escape) + { + break; + } + } + } } } } From b220e671c86274d310f23bd5dd8a3147ed6f87c7 Mon Sep 17 00:00:00 2001 From: Marcelo Grossi Date: Mon, 3 Oct 2016 11:55:40 +0100 Subject: [PATCH 2/2] add print help --- src/BigRunner.ConsoleApp/Program.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/BigRunner.ConsoleApp/Program.cs b/src/BigRunner.ConsoleApp/Program.cs index a4aad43..2e4bc67 100644 --- a/src/BigRunner.ConsoleApp/Program.cs +++ b/src/BigRunner.ConsoleApp/Program.cs @@ -360,9 +360,10 @@ Initialize these necessary input parameters logger = new StreamWriter(argsParser.GetArg("l")); } } - catch (Exception ex) + catch { - + PrintHelp(); + return; } } else