Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit 6c2ad8f

Browse files
committed
Add an error for unsupported flags
1 parent 7bca246 commit 6c2ad8f

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

UniqueFileGenerator/ArgParser.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
namespace UniqueFileGenerator;
22

3-
public sealed record ParsedArguments(uint FileCount, Dictionary<string, string> FlagValueMap);
3+
/// <summary>
4+
/// A DTO for passing parsed arguments from the user.
5+
/// </summary>
6+
public sealed record ParsedArguments(
7+
uint FileCount,
8+
IReadOnlyDictionary<string, string> FlagValueMap);
49

10+
/// <summary>
11+
/// Contains logic for parsing arguments passed from the user.
12+
/// </summary>
513
public static class ArgParser
614
{
715
private static readonly IReadOnlyList<string> SupportedFlags =
@@ -13,6 +21,10 @@ public static class ArgParser
1321
"-d" // Delay
1422
};
1523

24+
/// <summary>
25+
/// Parsed arguments passed from the user.
26+
/// </summary>
27+
/// <param name="args"></param>
1628
public static ParsedArguments ParseArgs(string[] args)
1729
{
1830
if (args.Length == 0)
@@ -38,7 +50,7 @@ public static ParsedArguments ParseArgs(string[] args)
3850
nameof(fileCount), Resources.FileCountInvalidZero);
3951
}
4052

41-
var argDict = new Dictionary<string, string>();
53+
var argDict = new Dictionary<string, string>(SupportedFlags.Count);
4254

4355
// Iterate through the args. Any non-flag arg is considered a value for the last-processed flag.
4456
// If there are multiple args for any such flag, they will be combined in a single string.
@@ -58,6 +70,12 @@ public static ParsedArguments ParseArgs(string[] args)
5870
// Treat the arg as a value for the current flag.
5971
else
6072
{
73+
if (IsInFlagFormat(thisArg))
74+
{
75+
throw new InvalidOperationException(
76+
string.Format(Resources.FlagInvalid, thisArg));
77+
}
78+
6179
if (string.IsNullOrWhiteSpace(currentFlag))
6280
{
6381
throw new InvalidOperationException(
@@ -74,4 +92,16 @@ public static ParsedArguments ParseArgs(string[] args)
7492

7593
return new ParsedArguments(fileCount, argDict);
7694
}
95+
96+
/// <summary>
97+
/// Determines if the given text in the format of a command line flag argument (e.g., "-e").
98+
/// </summary>
99+
/// <param name="text"></param>
100+
/// <returns></returns>
101+
private static bool IsInFlagFormat(string text)
102+
{
103+
return !string.IsNullOrWhiteSpace(text) &&
104+
text.Length == 2 &&
105+
text[0] == '-';
106+
}
77107
}

UniqueFileGenerator/Resources.Designer.cs

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UniqueFileGenerator/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,8 @@
221221
<value>The file creation delay must be 0 or higher.</value>
222222
<comment/>
223223
</data>
224+
<data name="FlagInvalid" xml:space="preserve">
225+
<value>The argument flag "{0}" is not supported.</value>
226+
<comment>{0}: A user-supplied command line argument</comment>
227+
</data>
224228
</root>

0 commit comments

Comments
 (0)