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

Commit 7bca246

Browse files
committed
Nomenclature improvements
1 parent ff4636e commit 7bca246

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

UniqueFileGenerator/ArgParser.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
namespace UniqueFileGenerator;
22

3-
public record Arguments(uint Count, Dictionary<string, string> argDict);
3+
public sealed record ParsedArguments(uint FileCount, Dictionary<string, string> FlagValueMap);
44

55
public static class ArgParser
66
{
77
private static readonly IReadOnlyList<string> SupportedFlags =
8-
new List<string>() { "-p", "-e", "-o", "-s", "-d" };
8+
new List<string>() {
9+
"-p", // Prefix
10+
"-e", // Extension
11+
"-o", // Output directory
12+
"-s", // Size
13+
"-d" // Delay
14+
};
915

10-
public static Arguments ParseArgs(string[] args)
16+
public static ParsedArguments ParseArgs(string[] args)
1117
{
1218
if (args.Length == 0)
1319
throw new ArgumentException(Resources.FileCountMissing, nameof(args));
@@ -17,12 +23,12 @@ public static Arguments ParseArgs(string[] args)
1723
var fileCountText = argQueue.Dequeue().Replace(",", "");
1824
if (!uint.TryParse(fileCountText, out var fileCount))
1925
{
20-
// If all characters are digits, then a number that was too high was provided.
26+
// Parsing failed. If all characters are digits,
27+
// then a number that was too large was provided.
2128
if (fileCountText.All(char.IsDigit))
2229
throw new InvalidOperationException(Resources.FileCountTooHigh);
2330

24-
// Otherwise, some invalid string was provided --
25-
// e.g., negative number, letters, symbols, etc.
31+
// Otherwise, some invalid characters were provided.
2632
throw new InvalidOperationException(Resources.FileCountInvalidRange);
2733
}
2834

@@ -41,7 +47,6 @@ public static Arguments ParseArgs(string[] args)
4147
{
4248
var thisArg = argQueue.Dequeue();
4349

44-
// If this is a supported flag.
4550
if (SupportedFlags.Contains(thisArg))
4651
{
4752
// Flags cannot be used twice.
@@ -50,7 +55,7 @@ public static Arguments ParseArgs(string[] args)
5055

5156
currentFlag = thisArg;
5257
}
53-
// Otherwise, consider this a value for the current flag.
58+
// Treat the arg as a value for the current flag.
5459
else
5560
{
5661
if (string.IsNullOrWhiteSpace(currentFlag))
@@ -67,6 +72,6 @@ public static Arguments ParseArgs(string[] args)
6772
}
6873
}
6974

70-
return new Arguments(fileCount, argDict);
75+
return new ParsedArguments(fileCount, argDict);
7176
}
7277
}

UniqueFileGenerator/Settings.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,30 @@ public sealed class Settings
4848
public bool IsLargeSize => SizeInBytes > 100_000_000;
4949
public bool IsLongDelay => FileCreationDelayMs > 60_000; // 1m
5050

51-
public Settings(Arguments args)
51+
public Settings(ParsedArguments parsedArgs)
5252
{
53-
FileCount = args.Count;
53+
FileCount = parsedArgs.FileCount;
5454

55-
var argDict = args.argDict;
55+
var args = parsedArgs.FlagValueMap;
5656

5757
// Only add a post-prefix space when the last character is not alphanumeric.
58-
Prefix = argDict.ContainsKey("-p")
59-
? argDict["-p"] + (IsLastCharAlphanumeric(argDict["-p"]) ? " " : string.Empty)
58+
Prefix = args.ContainsKey("-p")
59+
? args["-p"] + (IsLastCharAlphanumeric(args["-p"]) ? " " : string.Empty)
6060
: string.Empty;
6161

62-
Extension = argDict.ContainsKey("-e")
63-
? EnforceStartingPeriod(argDict["-e"])
62+
Extension = args.ContainsKey("-e")
63+
? EnforceStartingPeriod(args["-e"])
6464
: string.Empty;
6565

66-
OutputDirectory = (argDict.ContainsKey("-o") ? Path.Combine(".", argDict["-o"])
67-
: Path.Combine(".", "output"))
66+
OutputDirectory = (args.ContainsKey("-o") ? Path.Combine(".", args["-o"])
67+
: Path.Combine(".", "output"))
6868
+ Path.DirectorySeparatorChar;
6969

7070
// Parse the requested file size, if provided.
7171
// TODO: Accept sizes in formats like "30KB" or "10.4MB"
72-
if (argDict.ContainsKey("-s"))
72+
if (args.ContainsKey("-s"))
7373
{
74-
if (int.TryParse(argDict["-s"], out var parsedSize))
74+
if (int.TryParse(args["-s"], out var parsedSize))
7575
{
7676
SizeInBytes = parsedSize switch
7777
{
@@ -90,8 +90,8 @@ public Settings(Arguments args)
9090
}
9191

9292
// Parse the file creation delay, if provided.
93-
if (argDict.ContainsKey("-d") &&
94-
int.TryParse(argDict["-d"], out var parsedDelay))
93+
if (args.ContainsKey("-d") &&
94+
int.TryParse(args["-d"], out var parsedDelay))
9595
{
9696
FileCreationDelayMs = parsedDelay switch
9797
{

0 commit comments

Comments
 (0)