BadScript 2
Loading...
Searching...
No Matches
Issue543Tests.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.Linq;
3using Xunit;
5
7{
8 //Reference: PR# 634
9 public class Issue543Tests
10 {
11
12 private const int ERROR_SUCCESS = 0;
13
14 [Fact]
16 {
17 var result = Parser.Default.ParseArguments<Options>(new[] { "--help" });
18
19 Assert.Equal(ParserResultType.NotParsed, result.Tag);
20 Assert.Null(result.Value);
21 Assert.NotEmpty(result.Errors);
22 }
23
24 [Fact]
26 {
27 var result = Parser.Default.ParseArguments<Options>(new[] {
28 "-c", "someConnectionString",
29 "-j", "1234",
30 });
31
32 Assert.Equal(ParserResultType.Parsed, result.Tag);
33 Assert.NotNull(result.Value);
34 Assert.Empty(result.Errors);
35 Assert.Equal("someConnectionString", result.Value.ConnectionString);
36 Assert.Equal(1234, result.Value.JobId);
37 }
38
39 [Fact]
41 {
43 "verb1",
44 "-j", "1234",
45 });
46
47 Assert.Equal(ParserResultType.Parsed, result.Tag);
48 Assert.Empty(result.Errors);
49 Assert.NotNull(result.Value);
50 Assert.NotNull(result.Value as Verb1Options);
51 Assert.Equal(1234, (result.Value as Verb1Options).JobId);
52 }
53
54 [Fact]
56 {
58 "verb2",
59 "-c", "someConnectionString",
60 });
61
62 Assert.Equal(ParserResultType.Parsed, result.Tag);
63 Assert.Empty(result.Errors);
64 Assert.NotNull(result.Value);
65 Assert.NotNull(result.Value as Verb2Options);
66 Assert.Equal("someConnectionString", (result.Value as Verb2Options).ConnectionString);
67 }
68
69 // Options
70 internal class Options
71 {
72 [Option('c', "connectionString", Required = true, HelpText = "Texts.ExplainConnection")]
73 public string ConnectionString { get; set; }
74
75 [Option('j', "jobId", Required = true, HelpText = "Texts.ExplainJob")]
76 public int JobId { get; set; }
77
78 [Usage(ApplicationAlias = "Importer.exe")]
79 public static IEnumerable<Example> Examples
80 {
81 get => new[] {
82 new Example("Texts.ExplainExampleExecution", new Options() {
83 ConnectionString="Server=MyServer;Database=MyDatabase",
84 JobId = 5
85 }),
86 };
87 }
88 }
89
90 // Options
91 [Verb("verb1")]
92 internal class Verb1Options
93 {
94 [Option('j', "jobId", Required = false, HelpText = "Texts.ExplainJob")]
95 public int JobId { get; set; }
96 }
97
98 // Options
99 [Verb("verb2")]
100 internal class Verb2Options
101 {
102 [Option('c', "connectionString", Required = false, HelpText = "Texts.ExplainConnection")]
103 public string ConnectionString { get; set; }
104 }
105
106 }
107}
108
Provides methods to parse command line arguments.
Definition Parser.cs:21
ParserResult< object > ParseArguments(IEnumerable< string > args, params Type[] types)
Parses a string array of command line arguments for verb commands scenario, constructing the proper i...
Definition Parser.cs:216
static Parser Default
Gets the singleton instance created with basic defaults.
Definition Parser.cs:75
static IEnumerable< Example > Examples
Models a command line usage example.
Definition Example.cs:13
ParserResultType
Discriminator enumeration of CommandLine.ParserResultType derivates.