Skip to content

Commit 8e51fcf

Browse files
committed
feat: update CommandLineParser
1 parent afa08dc commit 8e51fcf

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/WeihanLi.Common/Helpers/CommandLineParser.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,32 @@ public static IEnumerable<string> ParseLine(string line, LineParseOptions? optio
101101
return GetValueInternal(args ?? Environment.GetCommandLineArgs(), optionName) ?? defaultValue;
102102
}
103103

104+
/// <summary>
105+
/// Get argument value from arguments
106+
/// </summary>
107+
/// <param name="args">arguments</param>
108+
/// <param name="defaultValue">default argument value when not found</param>
109+
/// <param name="optionName">argument name to get value</param>
110+
/// <returns>argument value</returns>
111+
[return: NotNullIfNotNull(nameof(defaultValue))]
112+
public static string? Val(string[] args, string optionName, string? defaultValue = null)
113+
{
114+
return GetValueInternal(args, optionName) ?? defaultValue;
115+
}
104116
public static bool BooleanVal(string optionName, string[]? args = null, bool defaultValue = default)
105117
{
106118
return GetValueInternal(args ?? Environment.GetCommandLineArgs(), optionName).ToBoolean(defaultValue);
107119
}
120+
121+
public static bool BooleanVal(string[] args, string optionName, bool defaultValue = default)
122+
{
123+
return GetValueInternal(args, optionName).ToBoolean(defaultValue);
124+
}
108125

109126
private static string? GetValueInternal(string[] args, string argumentName)
110127
{
128+
Guard.NotNull(args);
129+
Guard.NotNullOrEmpty(argumentName);
111130
for (var i = 0; i < args.Length; i++)
112131
{
113132
if (args[i] == $"--{argumentName}" || args[i] == $"-{argumentName}")

0 commit comments

Comments
 (0)