minimum Command Line Parser
This file contains 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
using System.Collections.Generic; | |
namespace Nekomimi.Daimao | |
{ | |
/// <summary> | |
/// parsing comannd line into a dictionary. | |
/// </summary> | |
public static class MiniCommandLine | |
{ | |
/// <summary> | |
/// -key value -> (key, value) | |
/// -key -> (key, null) | |
/// </summary> | |
public static Dictionary<string, string> Command => ParseArguments(System.Environment.GetCommandLineArgs()); | |
// ReSharper disable once MemberCanBePrivate.Global | |
public static Dictionary<string, string> ParseArguments(IReadOnlyList<string> args) | |
{ | |
const string prefixOption = "-"; | |
var parseArguments = new Dictionary<string, string>(); | |
for (var count = 0; count < args.Count; count++) | |
{ | |
var key = args[count]; | |
if (string.IsNullOrEmpty(key) || !key.StartsWith(prefixOption)) | |
{ | |
continue; | |
} | |
string value = null; | |
if (count + 1 < args.Count) | |
{ | |
value = args[count + 1]; | |
if (string.IsNullOrEmpty(value) || value.StartsWith(prefixOption)) | |
{ | |
value = null; | |
} | |
} | |
parseArguments[key.Substring(prefixOption.Length)] = value; | |
} | |
return parseArguments; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example