public static bool CheckArgs(int args_num, int args_corr, string verbose = "no")
{
//Log("Testing CheckArgs");
if (verbose == "no")
{
if (args_num == args_corr)
{
return true;
}
else
{
return false;
}
}
else
{
Console.WriteLine("Checking number of parameters....");
if (args_num == args_corr)
{
Console.WriteLine("Correct number of parameters provided.");
Console.WriteLine();
return true;
}
else
{
Console.WriteLine("Wrong number of parameters provided. Terminating.");
Console.WriteLine();
return false;
}
}
}
// Checking n. of arguments
if (CheckArgs(args.Length, 3))
{
Log("Checking n. of arguments...");
Log("Correct number of parameters provided");
}
else
{
Log("Wrong amount of parameters provided.");
System.Environment.Exit(1);
}
public static void ReportArgs(string[] args_in, string verbose = "no")
{
// Name: ReportArgs
// Description: Reports on provided script args.
// Package: Script Wrappers
// Input: args (argument array)
// Output: None, prints to stdout
// Usage: ReportArgs();
// Notes: Its better to add a \n in the end.
Log("Testing ReportArgs");
int i = 0;
if (verbose == "no")
{
foreach (string j in args_in)
{
i++;
Console.WriteLine("Parameter {0}: {1}",i, j);
}
}
else
{
Console.WriteLine("Reporting on script parameters:");
foreach (string j in args_in)
{
i++;
Console.WriteLine("Parameter {0}: {1}",i, j);
}
Console.WriteLine();
}
}
public static void ScriptParams([System.Runtime.CompilerServices.CallerFilePath] string filepath = "", string verbose = "no")
{
// Name: ScriptParams
// Description: Reports on script path, and dir
// Package: Script Wrappers
// Input: None
// Output: None, prints to stdout
// Usage: ScriptParams();
// Notes: Its better to add a \n in the end.
Log("Testing ScriptParams");
if (verbose == "no")
{
Console.WriteLine("Reporting on script params.");
string filename = Path.GetFileName(filepath);
string dirpath = Path.GetDirectoryName(filepath);
Console.WriteLine("Script full path : {0}", filepath);
Console.WriteLine("Script name: {0}", filename);
Console.WriteLine("Script location: {0}", dirpath);
Console.WriteLine();
}
else
{
string filename = Path.GetFileName(filepath);
string dirpath = Path.GetDirectoryName(filepath);
Console.WriteLine("Script full path : {0}", filepath);
Console.WriteLine("Script name: {0}", filename);
Console.WriteLine("Script location: {0}", dirpath);
}
}
public static List<string> ScriptParamsList([System.Runtime.CompilerServices.CallerFilePath] string filepath = "", string verbose = "no")
{
var retList = new List<string>();
string filename = Path.GetFileName(filepath);
string dirpath = Path.GetDirectoryName(filepath);
retList.Add(filepath);
retList.Add(filename);
retList.Add(dirpath);
return retList;
}
private static void Log(string text,
[CallerFilePath] string file = "",
[CallerMemberName] string member = "",
[CallerLineNumber] int line = 0)
{
Console.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
}
private static void LogFile(string text,
string logfile,
[CallerFilePath] string file = "",
[CallerMemberName] string member = "",
[CallerLineNumber] int line = 0)
{
using (StreamWriter writer = new StreamWriter(logfile, append: true))
{
writer.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
}
//Console.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
}
using System;
using System.IO;
using System.Runtime.CompilerServices;
class Program
{
/////////////////////////////////////////////////////////////////
// CSHARP NOTES:
// '' vs "" -> '' indicates a single character, "" indicates a string.
/// <summary>
/// This class performs an important function.
///
/// Name: CheckArgs
/// Type: Util / Script Wrapper
/// Function: Tests the provided number against each other.
/// Used to test whether the user has provided the
/// correct number of arguments to the script.
/// Input: <int>, <int>
/// args.Length, <int>
/// Output: true / false
/// Ex. Usage:
///
///
///
///
/// Notes:
///
///
///
/// Tags: #wrappers #util #csharp
///
/// </summary>
public static bool CheckArgs(int args_num, int args_corr, string verbose = "no")
{
Log("Testing CheckArgs");
if (verbose == "no")
{
if (args_num == args_corr)
{
return true;
}
else
{
return false;
}
}
else
{
Console.WriteLine("Checking number of parameters....");
if (args_num == args_corr)
{
Console.WriteLine("Correct number of parameters provided.");
Console.WriteLine();
return true;
}
else
{
Console.WriteLine("Wrong number of parameters provided. Terminating.");
Console.WriteLine();
return false;
}
}
}
public static void ReportArgs(string[] args_in, string verbose = "no")
{
Log("Testing ReportArgs");
int i = 0;
if (verbose == "no")
{
foreach (string j in args_in)
{
i++;
Console.WriteLine("Parameter {0}: {1}",i, j);
}
}
else
{
Console.WriteLine("Reporting on script parameters:");
foreach (string j in args_in)
{
i++;
Console.WriteLine("Parameter {0}: {1}",i, j);
}
Console.WriteLine();
}
}
public static void ScriptParams([System.Runtime.CompilerServices.CallerFilePath] string filepath = "", string verbose = "no")
{
Log("Testing ScriptParams");
if (verbose == "no")
{
Console.WriteLine("Reporting on script params.")
string filename = Path.GetFileName(filepath);
string dirpath = Path.GetDirectoryName(filepath);
Console.WriteLine("Script full path : {0}", filepath);
Console.WriteLine("Script name: {0}", filename);
Console.WriteLine("Script location: {0}", dirpath);
Console.WriteLine()
}
else
{
string filename = Path.GetFileName(filepath);
string dirpath = Path.GetDirectoryName(filepath);
Console.WriteLine("Script full path : {0}", filepath);
Console.WriteLine("Script name: {0}", filename);
Console.WriteLine("Script location: {0}", dirpath);
}
}
private static void Log(string text,
[CallerFilePath] string file = "",
[CallerMemberName] string member = "",
[CallerLineNumber] int line = 0)
{
Console.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
}
static void LogInfo([System.Runtime.CompilerServices.CallerMemberName] string methodName = "", [System.Runtime.CompilerServices.CallerFilePath] string filepath = "", [System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) {
Console.WriteLine("Welcome to c# Corener!");
Console.WriteLine("Method Name: " + methodName);
Console.WriteLine("FilePath: " + filepath);
Console.WriteLine("Line Numer: " + lineNumber);
Console.ReadLine();
}
static void Main(string[] args)
{
string nameofscript = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
Console.WriteLine("ScriptName: {0}", nameofscript);
string file_in = null;
string file_ot = null;
int length_in = 0;
int start_in = 0;
// Otherway to check arguments - Using func 1
if (CheckArgs(args.Length, 4))
{
Console.WriteLine("Correct number of parameters provided");
}
else
{
Console.WriteLine("Wrong amount of parameters provided.");
System.Environment.Exit(1);
}
if(args.Length == 4)
{
file_in = args[0];
file_ot = args[3];
length_in = int.Parse(args[2]);
start_in = int.Parse(args[1]);
//int length_in = Convert.ToInt32(args[0]);
//int c = (int)args[0];
//start_in = args
//length_in
Console.WriteLine(file_in);
Console.WriteLine(file_ot);
Console.WriteLine(start_in);
Console.WriteLine(length_in);
}
else
{
Console.WriteLine("what?");
}
ReportArgs(args, "yes");
ScriptParams();
LogInfo();
Log("Testing StreamWriter");
StreamWriter writerAbsolutePath = new StreamWriter(file_ot);
Log("Testing StreamReader");
using (StreamReader reader = new StreamReader(file_in))
{
while (true)
{
Log("Testing Line");
string line = reader.ReadLine();
string subl = null;
if (line == null)
{
break;
}
try
{
subl = line.Substring(start_in, length_in);
}
catch (Exception e)
{
//Console.WriteLine("Problem with substring: {0}", e);
subl = String.Empty;
}
//Console.WriteLine(line); // Use line.
Console.WriteLine(subl); // Use line.
writerAbsolutePath.WriteLine(subl);
}
}
writerAbsolutePath.Close();
}
}