Skip to content

Commit 8d7ea7a

Browse files
committed
refactored argument parsing
1 parent 398b91f commit 8d7ea7a

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/Format.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LineCount;
2+
3+
public enum Format
4+
{
5+
Normal,
6+
Raw,
7+
Json
8+
}

src/LineCountData.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@ namespace LineCount;
55

66
public class LineCountData
77
{
8-
public required string? Filter { get; init; }
9-
public required Regex? LineFilter { get; init; }
10-
public required string? ExcludeFilter { get; init; }
11-
public required Regex? ExcludeLineFilter { get; init; }
8+
public string? Filter { get; }
9+
public Regex? LineFilter { get; }
10+
public string? ExcludeFilter { get; }
11+
public Regex? ExcludeLineFilter { get; }
1212

1313
public FilterType FilterType { get; } = (FilterType)(-1);
1414
public required bool ListFiles { get; init; }
15+
public required Format Format { get; init; }
1516

1617
public static readonly TimeSpan TimeOut = TimeSpan.FromMilliseconds(500);
1718

18-
[SetsRequiredMembers]
19-
public LineCountData(string? filter, string? lineFilter, string? filterNot, string? lineFilterNot, bool listFiles)
19+
public LineCountData(string? filter, string? lineFilter, string? filterNot, string? lineFilterNot)
2020
{
2121
Filter = filter;
2222
ExcludeFilter = filterNot;
23-
ListFiles = listFiles;
24-
23+
2524
if (lineFilter is null)
2625
{
2726
FilterType = lineFilterNot is null ? FilterType.None : FilterType.FilteredExcept;
@@ -36,7 +35,5 @@ public LineCountData(string? filter, string? lineFilter, string? filterNot, stri
3635
{
3736
ExcludeLineFilter = new Regex(lineFilterNot, RegexOptions.Singleline | RegexOptions.Compiled, TimeOut);
3837
}
39-
40-
ListFiles = listFiles;
4138
}
4239
}

src/Program.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.CommandLine;
2+
using System.CommandLine.Invocation;
23
using LineCount;
34
using LineCount.Logging;
45

@@ -9,6 +10,7 @@
910
var exceptFilterOption = new Option<string>(["-x", "--exclude-filter"], "A glob-pattern for files not to include.");
1011
var exceptLineFilterOption = new Option<string>(["-w", "--exlude-line-filter"], "A RegEx for the lines not to count.");
1112
var listFilesOption = new Option<bool>("--list", "Whether to list the files as they are being processed.");
13+
var formatOption = new Option<Format>("--format", "The output format of the result.");
1214
var excludeDirectoriesOption = new Option<string[]>("--exclude-directories", "A list of directories to exclude.")
1315
{
1416
Arity = ArgumentArity.OneOrMore,
@@ -30,11 +32,27 @@
3032
rootCommand.AddOption(exceptFilterOption);
3133
rootCommand.AddOption(exceptLineFilterOption);
3234
rootCommand.AddOption(listFilesOption);
35+
rootCommand.AddOption(formatOption);
3336

34-
rootCommand.SetHandler(async (path, filter, lineFilter, exceptFilter, exceptLineFilter, excludeDirectories, excludeFilesOption, listFiles) =>
35-
{
36-
LineCountData data = new LineCountData(filter, lineFilter, exceptFilter, exceptLineFilter, listFiles);
37-
var result = await LineCount.LineCount.Run(path, data, excludeDirectories, excludeFilesOption);
37+
rootCommand.SetHandler(async (InvocationContext context) =>
38+
{
39+
var filter = context.ParseResult.GetValueForOption(filterOption);
40+
var lineFilter = context.ParseResult.GetValueForOption(lineFilterOption);
41+
var exceptFilter = context.ParseResult.GetValueForOption(exceptFilterOption);
42+
var exceptLineFilter = context.ParseResult.GetValueForOption(exceptLineFilterOption);
43+
var listFiles = context.ParseResult.GetValueForOption(listFilesOption);
44+
var format = context.ParseResult.GetValueForOption(formatOption);
45+
var excludeDirectories = context.ParseResult.GetValueForOption(excludeDirectoriesOption);
46+
var excludeFiles = context.ParseResult.GetValueForOption(excludeFilesOption);
47+
var path = context.ParseResult.GetValueForArgument(pathArgument);
48+
49+
LineCountData data = new LineCountData(filter, lineFilter, exceptFilter, exceptLineFilter)
50+
{
51+
ListFiles = listFiles,
52+
Format = format
53+
};
54+
55+
var result = await LineCount.LineCount.Run(path, data, excludeDirectories ?? [], excludeFiles ?? []);
3856

3957
if(listFiles)
4058
{
@@ -45,6 +63,6 @@
4563
report => Logger.LogReport(report),
4664
error => Logger.LogError(error)
4765
);
48-
}, pathArgument, filterOption, lineFilterOption, exceptFilterOption, exceptLineFilterOption, excludeDirectoriesOption, excludeFilesOption, listFilesOption);
66+
});
4967

5068
return await rootCommand.InvokeAsync(args);

0 commit comments

Comments
 (0)