|
| 1 | +using CommandLine.Text; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace CommandLine.Tests.Unit |
| 9 | +{ |
| 10 | + //Reference: PR# 392 |
| 11 | + public class Issue389Tests |
| 12 | + { |
| 13 | + |
| 14 | + private const int ERROR_SUCCESS = 0; |
| 15 | + |
| 16 | + // Test method (xUnit) which fails |
| 17 | + [Fact] |
| 18 | + public void CallMain_GiveHelpArgument_ExpectSuccess() |
| 19 | + { |
| 20 | + var result = Program.__Main(new[] { "--help" }); |
| 21 | + |
| 22 | + Assert.Equal(ERROR_SUCCESS, result); |
| 23 | + } |
| 24 | + |
| 25 | + // main program |
| 26 | + internal class Program |
| 27 | + { |
| 28 | + |
| 29 | + |
| 30 | + internal static int __Main(string[] args) |
| 31 | + { |
| 32 | + bool hasError = false; |
| 33 | + bool helpOrVersionRequested = false; |
| 34 | + |
| 35 | + ParserResult<Options> parsedOptions = Parser.Default.ParseArguments<Options>(args) |
| 36 | + .WithNotParsed(errors => { |
| 37 | + helpOrVersionRequested = errors.Any( |
| 38 | + x => x.Tag == ErrorType.HelpRequestedError |
| 39 | + || x.Tag == ErrorType.VersionRequestedError); |
| 40 | + hasError = true; |
| 41 | + }); |
| 42 | + |
| 43 | + if(helpOrVersionRequested) |
| 44 | + { |
| 45 | + return ERROR_SUCCESS; |
| 46 | + } |
| 47 | + |
| 48 | + // Execute as a normal call |
| 49 | + // ... |
| 50 | + return ERROR_SUCCESS; |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + // Options |
| 56 | + internal class Options |
| 57 | + { |
| 58 | + |
| 59 | + [Option('c', "connectionString", Required = true, HelpText = "Texts.ExplainConnection")] |
| 60 | + public string ConnectionString { get; set; } |
| 61 | + |
| 62 | + [Option('j', "jobId", Required = true, HelpText = "Texts.ExplainJob")] |
| 63 | + public int JobId { get; set; } |
| 64 | + |
| 65 | + [Usage(ApplicationAlias = "Importer.exe")] |
| 66 | + public static IEnumerable<Example> Examples |
| 67 | + { |
| 68 | + get => new[] { |
| 69 | + new Example("Texts.ExplainExampleExecution", new Options() { |
| 70 | + ConnectionString="Server=MyServer;Database=MyDatabase", |
| 71 | + JobId = 5 |
| 72 | + }), |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments