Created
April 11, 2018 12:34
-
-
Save JamisonWhite/38e3674f2bd8c5a59fdb5fcc419c5bc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CommandLineHelper | |
{ | |
/// <summary> | |
/// Get value from command line | |
/// </summary> | |
/// <remarks> | |
/// Example: | |
/// GetCommandLineValue(args, "--START", out _startDate, DateTime.MinValue); | |
/// </remarks> | |
/// <param name="args"></param> | |
/// <param name="key"></param> | |
/// <param name="value"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static bool GetValue<T>(IReadOnlyList<string> args, string key, out T value, T defaultValue) | |
{ | |
return GetValue(args, new List<string>() { key }, out value, defaultValue); | |
} | |
/// <summary> | |
/// Get value from command line | |
/// </summary> | |
/// <remarks> | |
/// Example: | |
/// GetCommandLineValue(args, {"-s", "--START"}, out _startDate, DateTime.MinValue); | |
/// </remarks> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="args"></param> | |
/// <param name="keys"></param> | |
/// <param name="value"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static bool GetValue<T>(IReadOnlyList<string> args, List<string> keys, out T value, T defaultValue) | |
{ | |
foreach (var key in keys) | |
{ | |
for (var i = 0; i < args.Count; i++) | |
{ | |
if (args[i].Equals(key, StringComparison.OrdinalIgnoreCase)) | |
{ | |
//get the text | |
string text; | |
if (typeof(T) == typeof(bool)) | |
{ | |
text = "true"; | |
} | |
else if (i + 1 < args.Count) | |
{ | |
text = args[i + 1]; | |
} | |
else | |
{ | |
continue; | |
} | |
//convert text to value's type | |
var tc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)); | |
value = (T)tc.ConvertFromString(null, CultureInfo.InvariantCulture, text); | |
return true; | |
} | |
} | |
} | |
value = defaultValue; | |
return false; | |
} | |
/// <summary> | |
/// Get value from command line | |
/// </summary> | |
/// <remarks> | |
/// Example: | |
/// GetCommandLineValue(args, "--START", out _startDate, DateTime.MinValue); | |
/// </remarks> | |
/// <param name="args"></param> | |
/// <param name="key"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static T GetValue<T>(IReadOnlyList<string> args, string key, T defaultValue) | |
{ | |
return GetValue(args, new List<string>() { key }, defaultValue); | |
} | |
/// <summary> | |
/// Get value from command line | |
/// </summary> | |
/// <remarks> | |
/// Example: | |
/// GetCommandLineValue(args, {"-s", "--START"}, out _startDate, DateTime.MinValue); | |
/// </remarks> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="args"></param> | |
/// <param name="keys"></param> | |
/// <param name="defaultValue"></param> | |
/// <returns></returns> | |
public static T GetValue<T>(IReadOnlyList<string> args, List<string> keys, T defaultValue) | |
{ | |
foreach (var key in keys) | |
{ | |
for (var i = 0; i < args.Count; i++) | |
{ | |
if (args[i].Equals(key, StringComparison.OrdinalIgnoreCase)) | |
{ | |
//get the text | |
string text; | |
if (typeof(T) == typeof(bool)) | |
{ | |
text = "true"; | |
} | |
else if (i + 1 < args.Count) | |
{ | |
text = args[i + 1]; | |
} | |
else | |
{ | |
continue; | |
} | |
//convert text to value's type | |
var tc = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)); | |
var value = (T)tc.ConvertFromString(null, CultureInfo.InvariantCulture, text); | |
return value; | |
} | |
} | |
} | |
return defaultValue; | |
} | |
/// <summary> | |
/// Check common cries for help | |
/// </summary> | |
/// <remarks> | |
/// ?, /?, -?, -help, --help | |
/// </remarks> | |
/// <param name="args"></param> | |
/// <returns></returns> | |
public static bool ShowHelp(IReadOnlyList<string> args) | |
{ | |
if ( | |
args.Contains("?") || | |
args.Contains("--HELP", StringComparer.InvariantCultureIgnoreCase) || | |
args.Contains("-HELP", StringComparer.InvariantCultureIgnoreCase) || | |
args.Contains("/?") || | |
args.Contains("-?") | |
) | |
{ | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment