Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-interactive mode #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/BigRunner.ConsoleApp/ArgsParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BigRunner.ConsoleApp
{
public class ArgsParser
{
private List<string> unidentifiedArgs;
private Dictionary<string, string> identifiedArgs;
public ArgsParser(string[] args)
{
unidentifiedArgs = new List<string>();
identifiedArgs = new Dictionary<string, string>();
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<string> GetArgs()
{
return unidentifiedArgs;
}
}
}
1 change: 1 addition & 0 deletions src/BigRunner.ConsoleApp/BigRunner.ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgsParser.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
95 changes: 70 additions & 25 deletions src/BigRunner.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,27 @@ Make notes on console screen
Console.WriteLine("\t2. To terminate the console. Please press ESC key");
}

/// <summary>
/// Input connection string data
/// </summary>
/// <returns>Returns sql connection if okay, otherwise user will continue entering</returns>
private static SqlConnection InputConnectionStringData()

/// <summary>
/// Print argument help
/// </summary>
private static void PrintHelp()
{
/**********************************************
Make notes on console screen
**********************************************/
Console.WriteLine("Usage: BigRunner.ConsoleApp.exe -s -c <connection string> -l <output log file> [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.");
}

/// <summary>
/// Input connection string data
/// </summary>
/// <returns>Returns sql connection if okay, otherwise user will continue entering</returns>
private static SqlConnection InputConnectionStringData()
{
var sqlConnection = new SqlConnection();

Expand Down Expand Up @@ -320,6 +336,8 @@ private static string CalElapsedTime(Stopwatch stopWatch)
/// <param name="args">The parameters from command line are passed into this method</param>
static void Main(string[] args)
{
ArgsParser argsParser = new ArgsParser(args);

/**********************************************
Initialize these necessary input parameters
**********************************************/
Expand All @@ -328,19 +346,43 @@ 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
{
PrintHelp();
return;
}
}
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
Expand Down Expand Up @@ -526,17 +568,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;
}
}
}
}
}
}
Expand Down