Skip to content

Commit 3e620b7

Browse files
author
Peter
committed
Added strong DateOnly type
1 parent 01bfd89 commit 3e620b7

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/KingpinNet/CommandLineItem.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ internal T ConvertToType(string value)
147147
}
148148
else if (ValueType == ValueType.Date)
149149
{
150-
if (DateTime.TryParseExact(value, new[] { "yyyy.MM.dd", "yyyy-MM-dd", "yyyy/MM/dd" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
151-
return (T)Convert.ChangeType(result, typeof(DateTime));
150+
if (typeof(T) == typeof(DateOnly) && DateOnly.TryParseExact(value, new[] { "yyyy.MM.dd", "yyyy-MM-dd", "yyyy/MM/dd" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out var resultDate))
151+
return (T)Convert.ChangeType(resultDate, typeof(T));
152+
else if (DateTime.TryParseExact(value, new[] { "yyyy.MM.dd", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy/MM/ddTHH:mm:ss", "yyyy-MM-ddTHH:mm:ss", "yyyy.MM.ddTHH:mm:ss" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
153+
return (T)Convert.ChangeType(result, typeof(T));
152154
else
153155
throw new ArgumentException($"'{value}' is not a date", nameof(value));
154156
}

src/KingpinNet/ValueType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ internal static ValueType Convert(Type type)
2626
return ValueType.Int;
2727
if (type == typeof(Int64))
2828
return ValueType.Long;
29+
if (type == typeof(DateOnly))
30+
return ValueType.Date;
2931
if (type == typeof(DateTime))
3032
return ValueType.Date;
3133
if (type == typeof(TimeSpan))

test/UnitTests/ParserTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,21 @@ public void ParseDurationSuccess()
253253
// Assert
254254
Assert.Equal("1:00:00", result.Result["time"]);
255255
}
256+
[Fact]
257+
public void ParseDateSuccess()
258+
{
259+
// Arrange
260+
string[] args = new[] { "--date=2026-12-27" };
261+
var application = new KingpinApplication(consoleMock.Object);
262+
var flag = application.Flag<DateOnly>("date", "");
263+
264+
// Act
265+
var subject = new Parser(application);
266+
var result = subject.Parse(args);
256267

268+
// Assert
269+
flag.Value.Equals(DateOnly.Parse("2026-12-27"));
270+
}
257271
[Fact]
258272
public void ParseDurationSuccessWithDays()
259273
{

0 commit comments

Comments
 (0)