11namespace 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>
513public 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}
0 commit comments