|
1 | 1 | // Copyright (c) Nate McMaster. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
| 4 | +using McMaster.Extensions.CommandLineUtils.Abstractions; |
4 | 5 | using System; |
5 | 6 | using System.Globalization; |
6 | | -using System.Collections.Generic; |
| 7 | +using System.Threading.Tasks; |
7 | 8 | using Xunit; |
8 | | -using McMaster.Extensions.CommandLineUtils.Abstractions; |
9 | 9 |
|
10 | 10 | namespace McMaster.Extensions.CommandLineUtils.Tests |
11 | 11 | { |
12 | | - using System.Linq; |
13 | | - |
14 | 12 | public class ValueParserProviderCustomTests |
15 | 13 | { |
16 | 14 |
|
@@ -193,6 +191,75 @@ public void CustomParsersAreAutomaticallySingleValues() |
193 | 191 | optionMapper.GetOptionType(typeof(double), app.ValueParsers)); |
194 | 192 | } |
195 | 193 |
|
| 194 | + |
| 195 | + [Command()] |
| 196 | + [Subcommand("subcommand", typeof(CustomParserProgramAttributesSubCommand))] |
| 197 | + private class CustomParserProgramAttributes |
| 198 | + { |
| 199 | + [Option("-a")] |
| 200 | + public DateTimeOffset MainDate { get; } |
| 201 | + } |
| 202 | + |
| 203 | + [Command()] |
| 204 | + private class CustomParserProgramAttributesSubCommand |
| 205 | + { |
| 206 | + [Option("-b")] |
| 207 | + public DateTimeOffset SubDate { get; } |
| 208 | + |
| 209 | + public Task<int> OnExecute(CommandLineApplication app) |
| 210 | + { |
| 211 | + return Task.FromResult(1); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + [Fact] |
| 216 | + public void CustomParsersAreAvailableToSubCommands() |
| 217 | + { |
| 218 | + var expectedDate = new DateTimeOffset(2018, 02, 16, 21, 30, 33, 45, TimeSpan.FromHours(10)); |
| 219 | + |
| 220 | + |
| 221 | + var app = new CommandLineApplication<CustomParserProgramAttributes>(); |
| 222 | + app.ValueParsers.AddOrReplace(new MyDateTimeOffsetParser()); |
| 223 | + app.Conventions.UseDefaultConventions(); |
| 224 | + |
| 225 | + var args = new[] { "-a", expectedDate.ToString("O"), "subcommand", "-b", expectedDate.AddSeconds(123456).ToString("O") }; |
| 226 | + |
| 227 | + var result = app.Execute(args); |
| 228 | + |
| 229 | + Assert.Equal(1, result); |
| 230 | + Assert.Equal(expectedDate, app.Model.MainDate); |
| 231 | + Assert.Equal(expectedDate.AddSeconds(123456), ((CommandLineApplication<CustomParserProgramAttributesSubCommand>)app.Commands[0]).Model.SubDate); |
| 232 | + } |
| 233 | + |
| 234 | + [Fact] |
| 235 | + public void CustomParsersAreAvailableToBuilderSubCommands() |
| 236 | + { |
| 237 | + var expectedDate = new DateTimeOffset(2018, 02, 16, 21, 30, 33, 45, TimeSpan.FromHours(10)); |
| 238 | + DateTimeOffset actualMainDate = default; |
| 239 | + DateTimeOffset actualSubDate = default; |
| 240 | + |
| 241 | + var app = new CommandLineApplication(); |
| 242 | + app.ValueParsers.AddOrReplace(new MyDateTimeOffsetParser()); |
| 243 | + |
| 244 | + var mainDate = app.Option<DateTimeOffset>("-a", "The main date to parse", CommandOptionType.SingleValue); |
| 245 | + app.Command("subcommand", configCmd => |
| 246 | + { |
| 247 | + var subDate = configCmd.Option<DateTimeOffset>("-b", "A date for the sub command", CommandOptionType.SingleValue); |
| 248 | + |
| 249 | + configCmd.OnExecute(() => |
| 250 | + { |
| 251 | + actualMainDate = mainDate.ParsedValue; |
| 252 | + actualSubDate = subDate.ParsedValue; |
| 253 | + }); |
| 254 | + }); |
| 255 | + |
| 256 | + var args = new[] { "-a", expectedDate.ToString("O"), "subcommand", "-b", expectedDate.AddSeconds(123456).ToString("O") }; |
| 257 | + app.Execute(args); |
| 258 | + |
| 259 | + Assert.Equal(expectedDate, actualMainDate); |
| 260 | + Assert.Equal(expectedDate.AddSeconds(123456), actualSubDate); |
| 261 | + } |
| 262 | + |
196 | 263 | private class BadValueParser : IValueParser |
197 | 264 | { |
198 | 265 | public Type TargetType { get; } = null; |
|
0 commit comments