Skip to content

Commit 792a0be

Browse files
committed
Feat: Refactor and improve CLI parser functionality
Renamed builder and related interfaces for consistency and clarity. Adjusted regex patterns and handling for better argument parsing, including quoted strings. Updated example command and enhanced `TestCommand` to print parameter values.
1 parent f069d13 commit 792a0be

File tree

8 files changed

+26
-17
lines changed

8 files changed

+26
-17
lines changed

src/CodeOfChaos.CliArgsParser.Contracts/ICliArgsParserBuilder.cs renamed to src/CodeOfChaos.CliArgsParser.Contracts/ICliParserBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace CodeOfChaos.CliArgsParser;
66
// ---------------------------------------------------------------------------------------------------------------------
77
// Code
88
// ---------------------------------------------------------------------------------------------------------------------
9-
public interface ICliArgsParserBuilder {
10-
ICliArgsParserBuilder AddServices(IServiceProvider provider);
11-
ICliArgsParserBuilder AddServices(Func<IServiceProvider> provider);
9+
public interface ICliParserBuilder {
10+
ICliParserBuilder AddServices(IServiceProvider provider);
11+
ICliParserBuilder AddServices(Func<IServiceProvider> provider);
1212

1313
ICliParser Build();
1414
}

src/CodeOfChaos.CliArgsParser/CliParser.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ public partial class CliParser : ICliParser {
1717
// Methods
1818
// -----------------------------------------------------------------------------------------------------------------
1919
internal CliParser() { }
20-
public static CliArgsParserBuilder FromBuilder() {
21-
return new CliArgsParserBuilder();
20+
public static CliParserBuilder FromBuilder() {
21+
return new CliParserBuilder();
2222
}
2323

2424
public ValueTask ExecuteAsync(string[] args, CancellationToken ct = default) => ExecuteAsync(ArgsInputHelper.ToOneLine(args), ct);
2525
public async ValueTask ExecuteAsync(string input, CancellationToken ct = default) {
26+
input = input.Replace("\\\"", "\"");
2627
if (input.Contains("&&")) {
2728
IEnumerable<Task> tasks = input
2829
.Split("&&")
@@ -33,7 +34,7 @@ public async ValueTask ExecuteAsync(string input, CancellationToken ct = default
3334

3435
string[] tokens = FindEmptySpacesRegex.Split(input);
3536
string commandName = tokens[0];
36-
string parameterInput = ArgsInputHelper.ToOneLine(tokens.Skip(1));
37+
string parameterInput = string.Join(" ", tokens.Skip(1));
3738

3839
if (!CommandProvider.TryGetCommand(commandName, out Type? commandType)) throw new Exception("no Command found");
3940

src/CodeOfChaos.CliArgsParser/CliArgsParserBuilder.cs renamed to src/CodeOfChaos.CliArgsParser/CliParserBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CodeOfChaos.CliArgsParser;
99
// ---------------------------------------------------------------------------------------------------------------------
1010
// Code
1111
// ---------------------------------------------------------------------------------------------------------------------
12-
public partial class CliArgsParserBuilder : ICliArgsParserBuilder {
12+
public partial class CliParserBuilder : ICliParserBuilder {
1313
private Func<IServiceProvider>? ServiceProvider { get; set; }
1414
private CommandProvider CommandProvider { get; set; } = new();
1515

@@ -19,19 +19,19 @@ public partial class CliArgsParserBuilder : ICliArgsParserBuilder {
1919
// -----------------------------------------------------------------------------------------------------------------
2020
// Methods
2121
// -----------------------------------------------------------------------------------------------------------------
22-
public ICliArgsParserBuilder AddServices(IServiceProvider provider) {
22+
public ICliParserBuilder AddServices(IServiceProvider provider) {
2323
ServiceProvider = () => provider;
2424
return this;
2525
}
2626

27-
public ICliArgsParserBuilder AddServices(Func<IServiceProvider> provider) {
27+
public ICliParserBuilder AddServices(Func<IServiceProvider> provider) {
2828
ServiceProvider = provider;
2929
return this;
3030
}
3131

32-
public ICliArgsParserBuilder AddCommandsFromAssembly<TEntrypoint>() => AddCommandsFromAssembly(typeof(TEntrypoint).Assembly);
32+
public ICliParserBuilder AddCommandsFromAssembly<TEntrypoint>() => AddCommandsFromAssembly(typeof(TEntrypoint).Assembly);
3333

34-
public ICliArgsParserBuilder AddCommandsFromAssembly(Assembly assembly) {
34+
public ICliParserBuilder AddCommandsFromAssembly(Assembly assembly) {
3535
Type[] types = assembly.GetTypes();
3636
Type? staticDictionaryType = types.FirstOrDefault(t => FindCommandDictionary.IsMatch(t.Name) && t.IsClass);
3737
var commandsDictionary = staticDictionaryType?.GetField("Commands")?.GetValue(null) as Dictionary<string, Type>;

src/CodeOfChaos.CliArgsParser/ParameterDictionary.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ public partial class ParameterDictionary : IParameterDictionary {
1212
private uint _positionalCounter;
1313
private uint _quotedStringCounter;
1414

15-
[GeneratedRegex("""(?:(?<keyValue>(?<key>--[\w\-_]+|-[\w\-_])\s*=\s*(?<value>"[^"]*"|[^ ]+)))|(?<flag>(?:--[\w\-_]+|-[\w\-_])(?=\s|$))|(?<quotedString>"(?<quoted>[^"]*)")|(?<positional>\S+)""")]
15+
[GeneratedRegex("""
16+
(?<keyValue>(?<key>--[\w\-_]+|-[\w\-_])\s*=\s*(?<value>\\?"[^"]*\\?"))
17+
|(?<flag>(?:--[\w\-_]+|-[\w\-_])(?=\s|$))
18+
|(?<quotedString>\\?"(?<quoted>[^"]*)\\?")
19+
|(?<positional>\S+)
20+
""", RegexOptions.IgnorePatternWhitespace)]
1621
private static partial Regex GatherValuesRegex { get; }
1722

1823
// -----------------------------------------------------------------------------------------------------------------

src/Sample.CodeOfChaos.CliArgsParser/Progam.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Sample.CodeOfChaos.CliArgsParser;
1010
// ---------------------------------------------------------------------------------------------------------------------
1111
public static class Program {
1212
public static async Task Main(string[] args) {
13-
var parser = CliParser.FromBuilder()
13+
ICliParser parser = CliParser.FromBuilder()
1414
.AddCommandsFromAssembly<TestCommand>()
1515
.Build();
1616

src/Sample.CodeOfChaos.CliArgsParser/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"Run Example": {
44
"commandName": "Project",
5-
"commandLineArgs": "example --test=\"Some Value\" -v"
5+
"commandLineArgs": "test-command --test-string=\"Some Value\" --test-int=2 --test-bool --test-required-string=alpha"
66
}
77
}
88
}

src/Sample.CodeOfChaos.CliArgsParser/TestCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace Sample.CodeOfChaos.CliArgsParser;
1414
[CliData, AutoName]
1515
public partial class TestCommand : ICliCommand<TestCommandParameters> {
1616
public async ValueTask ExecuteAsync(TestCommandParameters parameters, CancellationToken ct = default) {
17-
throw new NotImplementedException();
17+
Console.WriteLine(parameters.TestString);
18+
Console.WriteLine(parameters.TestInt);
19+
Console.WriteLine(parameters.TestBool);
20+
Console.WriteLine(parameters.TestRequiredString);
1821
}
1922
}

src/Sample.CodeOfChaos.CliArgsParser/TestCommandParameters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace Sample.CodeOfChaos.CliArgsParser;
88
// ---------------------------------------------------------------------------------------------------------------------
99
// Code
1010
// ---------------------------------------------------------------------------------------------------------------------
11-
public record TestCommandParameters : ICliParameters{
12-
[CliData, AutoName] public string TestString { get; init; }
11+
public record TestCommandParameters : ICliParameters {
12+
[CliData, AutoName] public string TestString { get; init; } = string.Empty;
1313
[CliData, AutoName] public int TestInt { get; init; }
1414
[CliData, AutoName] public bool TestBool { get; init; }
1515
[CliData, AutoName] public required string TestRequiredString { get; init; }

0 commit comments

Comments
 (0)