Skip to content

Commit 50c638f

Browse files
committed
Add int, double resolvers and update string resolver
1 parent fa5ac09 commit 50c638f

7 files changed

Lines changed: 149 additions & 78 deletions

File tree

CommandLineParser/Abstractions/ICommandLineParser'.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq.Expressions;
45
using System.Text;
56
using MatthiWare.CommandLine.Abstractions.Parsing;

CommandLineParser/CommandLineParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Linq.Expressions;
56
using System.Runtime.CompilerServices;

CommandLineParser/Core/Extensions.cs

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,79 @@ namespace MatthiWare.CommandLine.Core
77
internal static class Extensions
88
{
99

10-
public static IEnumerable<string> SplitOnWhitespace(this string input)
11-
{
12-
if (string.IsNullOrWhiteSpace(input)) yield return null;
10+
//public static IEnumerable<string> SplitOnWhitespace(this string input)
11+
//{
12+
// if (string.IsNullOrWhiteSpace(input)) yield return input;
1313

14-
int len = input.Length;
14+
// int len = input.Length;
1515

16-
int totalRead = 0;
16+
// int totalRead = 0;
1717

18-
while (totalRead < len)
19-
{
20-
var word = FindNextWord(input.AsSpan(totalRead), out int read);
21-
totalRead += read;
18+
// while (totalRead < len)
19+
// {
20+
// var word = FindNextWord(input.AsSpan(totalRead), out int read);
21+
// totalRead += read;
2222

23-
if (word != null)
24-
yield return word;
25-
}
23+
// if (word != null)
24+
// yield return word;
25+
// }
2626

2727

28-
}
28+
//}
2929

30-
private static string FindNextWord(ReadOnlySpan<char> src, out int read)
31-
{
32-
read = 0;
30+
//private static string FindNextWord(ReadOnlySpan<char> src, out int read)
31+
//{
32+
// read = 0;
3333

34-
if (src.IsEmpty) return null;
34+
// if (src.IsEmpty) return null;
3535

36-
int lastIndex = 0;
37-
bool inDoubleQuotes = false;
38-
bool inLiteral = false;
39-
bool exitedDoubleQuotes = false;
40-
bool exitedLiteral = false;
36+
// int lastIndex = 0;
37+
// bool inDoubleQuotes = false;
38+
// bool inLiteral = false;
39+
// bool exitedDoubleQuotes = false;
40+
// bool exitedLiteral = false;
4141

42-
for (int i = 0; i < src.Length; i++)
43-
{
44-
read++;
42+
// for (int i = 0; i < src.Length; i++)
43+
// {
44+
// read++;
4545

46-
if (src[i] == DoubleQuote)
47-
{
48-
inDoubleQuotes = !inDoubleQuotes;
49-
exitedDoubleQuotes = true;
50-
}
51-
else if (src[i] == SingleQuote)
52-
{
53-
inLiteral = !inLiteral;
54-
exitedLiteral = true;
55-
}
46+
// if (src[i] == DoubleQuote)
47+
// {
48+
// inDoubleQuotes = !inDoubleQuotes;
49+
// exitedDoubleQuotes = true;
50+
// }
51+
// else if (src[i] == SingleQuote)
52+
// {
53+
// inLiteral = !inLiteral;
54+
// exitedLiteral = true;
55+
// }
5656

57-
if ((exitedDoubleQuotes || exitedLiteral) && !inDoubleQuotes && !inLiteral)
58-
{
59-
var endOffset = exitedDoubleQuotes ? 1 : 2;
57+
// if ((exitedDoubleQuotes || exitedLiteral) && !inDoubleQuotes && !inLiteral)
58+
// {
59+
// var endOffset = exitedDoubleQuotes ? 1 : 2;
6060

61-
var substr = src.Substring(1, i - lastIndex - endOffset);
61+
// var substr = src.Substring(1, i - lastIndex - endOffset);
6262

63-
lastIndex = i + 1;
63+
// lastIndex = i + 1;
6464

65-
exitedLiteral = false;
66-
exitedDoubleQuotes = false;
65+
// exitedLiteral = false;
66+
// exitedDoubleQuotes = false;
6767

68-
if (!substr.IsEmpty) return substr.ToString();
69-
}
68+
// if (!substr.IsEmpty) return substr.ToString();
69+
// }
7070

71-
if (!inDoubleQuotes && !inLiteral && src[i] == Space)
72-
{
73-
var substr = src.Substring(lastIndex, i - lastIndex);
71+
// if (!inDoubleQuotes && !inLiteral && src[i] == Space)
72+
// {
73+
// var substr = src.Substring(lastIndex, i - lastIndex);
7474

75-
lastIndex = i + 1;
75+
// lastIndex = i + 1;
7676

77-
if (!substr.IsEmpty) return substr.ToString();
78-
}
79-
}
77+
// if (!substr.IsEmpty) return substr.ToString();
78+
// }
79+
// }
8080

81-
return src.ToString();
82-
}
81+
// return src.ToString();
82+
//}
8383

8484
public static ReadOnlySpan<char> Substring(this ReadOnlySpan<char> src, int start, int len)
8585
{
@@ -88,29 +88,29 @@ public static ReadOnlySpan<char> Substring(this ReadOnlySpan<char> src, int star
8888
return src.Slice(start, len);
8989
}
9090

91-
public static string RemoveLiteralsAndQuotes(this ReadOnlySpan<char> value)
92-
{
93-
if (value.IsEmpty) return new string(value.ToArray());
91+
//public static string RemoveLiteralsAndQuotes(this ReadOnlySpan<char> value)
92+
//{
93+
// if (value.IsEmpty) return new string(value.ToArray());
9494

95-
int startTrim = 0, endTrim = 0;
96-
int len = value.Length - 1;
95+
// int startTrim = 0, endTrim = 0;
96+
// int len = value.Length - 1;
9797

98-
if (value.Length == 0) return new string(value.ToArray());
98+
// if (value.Length == 0) return new string(value.ToArray());
9999

100-
if (value[0] == DoubleQuote || value[0] == SingleQuote)
101-
startTrim = 1;
100+
// if (value[0] == DoubleQuote || value[0] == SingleQuote)
101+
// startTrim = 1;
102102

103-
if (value[len] == DoubleQuote || value[len] == SingleQuote)
104-
endTrim = 1;
103+
// if (value[len] == DoubleQuote || value[len] == SingleQuote)
104+
// endTrim = 1;
105105

106-
if (startTrim == 0 && endTrim == 0) return new string(value.ToArray());
106+
// if (startTrim == 0 && endTrim == 0) return new string(value.ToArray());
107107

108-
return new string(value.Substring(startTrim, len - endTrim).ToArray());
109-
}
108+
// return new string(value.Substring(startTrim, len - endTrim).ToArray());
109+
//}
110110

111-
private const char DoubleQuote = '"';
112-
private const char SingleQuote = '\'';
113-
private const char Space = ' ';
111+
//private const char DoubleQuote = '"';
112+
//private const char SingleQuote = '\'';
113+
//private const char Space = ' ';
114114

115115
}
116116
}

CommandLineParser/Core/Parsing/ResolverFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public ResolverFactory()
1515
{
1616
Register<string, StringResolver>();
1717
Register<bool, BoolResolver>();
18+
Register<int, IntResolver>();
19+
Register<double, DoubleResolver>();
1820
}
1921

2022
public bool Contains<T>()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Text;
5+
using MatthiWare.CommandLine.Abstractions.Models;
6+
using MatthiWare.CommandLine.Abstractions.Parsing;
7+
8+
namespace MatthiWare.CommandLine.Core.Parsing.Resolvers
9+
{
10+
public class DoubleResolver : ICommandLineArgumentResolver<double>
11+
{
12+
public bool CanResolve(ArgumentModel model)
13+
{
14+
if (!model.HasValue) return false;
15+
16+
return TryResolve(model, out _);
17+
}
18+
19+
public double Resolve(ArgumentModel model)
20+
{
21+
TryResolve(model, out double result);
22+
23+
return result;
24+
}
25+
26+
private bool TryResolve(ArgumentModel model, out double result)
27+
{
28+
if (!model.HasValue)
29+
{
30+
result = 0;
31+
return false;
32+
}
33+
34+
return double.TryParse(model.Value, NumberStyles.AllowExponent | NumberStyles.Number | NumberStyles.Float, CultureInfo.InvariantCulture, out result);
35+
}
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using MatthiWare;
5+
using MatthiWare.CommandLine.Abstractions.Models;
6+
using MatthiWare.CommandLine.Abstractions.Parsing;
7+
8+
namespace MatthiWare.CommandLine.Core.Parsing.Resolvers
9+
{
10+
public class IntResolver : ICommandLineArgumentResolver<int>
11+
{
12+
public bool CanResolve(ArgumentModel model)
13+
{
14+
if (!model.HasValue) return false;
15+
16+
return TryResolve(model, out _);
17+
}
18+
19+
public int Resolve(ArgumentModel model)
20+
{
21+
TryResolve(model, out int result);
22+
23+
return result;
24+
}
25+
26+
private bool TryResolve(ArgumentModel model, out int result)
27+
{
28+
if (!model.HasValue)
29+
{
30+
result = 0;
31+
return false;
32+
}
33+
34+
return int.TryParse(model.Value, out result);
35+
}
36+
}
37+
}

CommandLineParser/Core/Parsing/Resolvers/StringResolver.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,9 @@ namespace MatthiWare.CommandLine.Core.Parsing.Resolvers
99
{
1010
internal class StringResolver : ICommandLineArgumentResolver<string>
1111
{
12-
public bool CanResolve(ArgumentModel model)
13-
{
14-
if (string.IsNullOrWhiteSpace(model.Value) || !model.HasValue) return true;
15-
16-
string value = (model.Value ?? string.Empty).Trim();
17-
18-
return value.SplitOnWhitespace().Count() == 1;
19-
}
12+
public bool CanResolve(ArgumentModel model) => true;
2013

2114
public string Resolve(ArgumentModel model)
22-
=> model.HasValue ? model.Value.AsSpan().RemoveLiteralsAndQuotes() : null;
15+
=> model.HasValue ? model.Value : null;
2316
}
2417
}

0 commit comments

Comments
 (0)