public void Explicit_help_request_with_immutable_instance_generates_help_requested_error() { // Fixture setup var expectedError = new HelpRequestedError(); var sut = new Parser(); // Exercize system var result = sut.ParseArguments<FakeImmutableOptions>(new[] { "--help" }); // Verify outcome ((NotParsed<FakeImmutableOptions>)result).Errors.Should().HaveCount(x => x == 1); ((NotParsed<FakeImmutableOptions>)result).Errors.Should().ContainSingle(e => e.Equals(expectedError)); // Teardown }
public void Explicit_help_request_with_immutable_instance_generates_help_requested_error() { // Fixture setup var expectedError = new HelpRequestedError(); var sut = new Parser(); // Exercize system var result = sut.ParseArguments <Immutable_Simple_Options>(new[] { "--help" }); // Verify outcome ((NotParsed <Immutable_Simple_Options>)result).Errors.Should().HaveCount(x => x == 1); ((NotParsed <Immutable_Simple_Options>)result).Errors.Should().ContainSingle(e => e.Equals(expectedError)); // Teardown }
static void Main(string[] args) { if (args.Contains("--debug")) { SetLogger(new SerilogLogger(console: true, debug: true)); Info("Debug mode set."); } else { AppDomain.CurrentDomain.UnhandledException += Program_UnhandledException; SetLogger(new SerilogLogger(console: true, debug: false)); } PrintLogo(); #if WINDOWS && NET461 ParserResult <object> result = new Parser().ParseArguments <Options, ApiOptions, PingOptions, EndpointsOptions, SchemaOptions, VerticesOptions, EdgesOptions, UpsertOptions, QueryOptions, BuiltinOptions, ExecOptions, WinEvtOptions>(args); #else ParserResult <object> result = new Parser().ParseArguments <Options, ApiOptions, PingOptions, EndpointsOptions, SchemaOptions, VerticesOptions, EdgesOptions, UpsertOptions, QueryOptions, BuiltinOptions, ExecOptions>(args); #endif result.WithParsed <ApiOptions>(o => { ApiClient = new ApiClient(GetToken(o), GetRestServerUrl(o), GetGsqlServerUrl(o), GetUser(o), GetPass(o)); if (!ApiClient.Initialized) { Error("The API client did not initialize."); Exit(ExitResult.INVALID_OPTIONS); } }) .WithParsed <PingOptions>(o => { Exit(Echo(o).Result); }) .WithParsed <EndpointsOptions>(o => { Exit(Endpoints(o).Result); }) .WithParsed <SchemaOptions>(o => { Exit(Schema(o).Result); }) .WithParsed <VerticesOptions>(o => { Exit(Vertices(o).Result); }) .WithParsed <EdgesOptions>(o => { Exit(Edges(o).Result); }) .WithParsed <ExecOptions>(o => { Exit(Exec(o).Result); }) #if WINDOWS && NET461 .WithParsed <WinEvtOptions>(o => { Exit(WinEvt(o).Result); }) #endif .WithParsed <UpsertOptions>(o => { Exit(Upsert(o).Result); }) .WithParsed <QueryOptions>(o => { Exit(Query(o).Result); }) .WithParsed <BuiltinOptions>(o => { Exit(Builtin(o).Result); }) #region Print options help .WithNotParsed((IEnumerable <Error> errors) => { HelpText help = GetAutoBuiltHelpText(result); help.Heading = new HeadingInfo("TigerGraph.NET", ApiClient.AssemblyVersion.ToString(3)); help.Copyright = ""; if (errors.Any(e => e.Tag == ErrorType.VersionRequestedError)) { help.Heading = new HeadingInfo("TigerGraph.NET", ApiClient.AssemblyVersion.ToString(3)); help.Copyright = new CopyrightInfo("Allister Beharry", new int[] { 2020 }); Info(help); Exit(ExitResult.SUCCESS); } else if (errors.Any(e => e.Tag == ErrorType.HelpVerbRequestedError)) { HelpVerbRequestedError error = (HelpVerbRequestedError)errors.First(e => e.Tag == ErrorType.HelpVerbRequestedError); if (error.Type != null) { help.AddVerbs(error.Type); } else { help.AddVerbs(OptionTypes); } Info(help.ToString().Replace("--", "")); Exit(ExitResult.SUCCESS); } else if (errors.Any(e => e.Tag == ErrorType.HelpRequestedError)) { HelpRequestedError error = (HelpRequestedError)errors.First(e => e.Tag == ErrorType.HelpRequestedError); help.AddVerbs(result.TypeInfo.Current); help.AddOptions(result); help.AddPreOptionsLine($"{result.TypeInfo.Current.Name.Replace("Options", "").ToLower()} options:"); Info(help); Exit(ExitResult.SUCCESS); } else if (errors.Any(e => e.Tag == ErrorType.NoVerbSelectedError)) { help.AddVerbs(OptionTypes); Info(help); Exit(ExitResult.INVALID_OPTIONS); } else if (errors.Any(e => e.Tag == ErrorType.MissingRequiredOptionError)) { MissingRequiredOptionError error = (MissingRequiredOptionError)errors.First(e => e.Tag == ErrorType.MissingRequiredOptionError); Error("A required option is missing: {0}.", error.NameInfo.NameText); Info(help); Exit(ExitResult.INVALID_OPTIONS); } else if (errors.Any(e => e.Tag == ErrorType.UnknownOptionError)) { UnknownOptionError error = (UnknownOptionError)errors.First(e => e.Tag == ErrorType.UnknownOptionError); help.AddVerbs(OptionTypes); Error("Unknown option: {error}.", error.Token); Info(help); Exit(ExitResult.INVALID_OPTIONS); } else { Error("An error occurred parsing the program options: {errors}.", errors); help.AddVerbs(OptionTypes); Info(help); Exit(ExitResult.INVALID_OPTIONS); } }); #endregion }
static void Main(string[] args) { Args = args; if (Args.Contains("fn") || Args.Contains("pm") || Args.Contains("cr")) { SetLogger(new SerilogLogger(console: false, debug: true)); } else if (Args.Contains("--debug")) { SetLogger(new SerilogLogger(console: true, debug: true)); } else { SetLogger(new SerilogLogger(console: true, debug: false)); } AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Console.CancelKeyPress += Console_CancelKeyPress; WebServer = new WebServer(); WebServer.ProcessRequest += WebServer_ProcessRequest; WebServer.EndPoint = new IPEndPoint(IPAddress.Loopback, 5001); WebServer.IsStarted = true; if (!args.Contains("fn") && !args.Contains("pm") && !args.Contains("cr")) { PrintLogo(); } if (args.Contains("--debug")) { Info("Debug mode set."); } ParserResult <object> result = new Parser().ParseArguments <Options, SpeechRecognitionOptions, TTSOptions, FNOptions, PMOptions>(args); result.WithNotParsed((IEnumerable <Error> errors) => { HelpText help = GetAutoBuiltHelpText(result); help.Copyright = string.Empty; help.AddPreOptionsLine(string.Empty); if (errors.Any(e => e.Tag == ErrorType.VersionRequestedError)) { Exit(ExitResult.SUCCESS); } else if (errors.Any(e => e.Tag == ErrorType.HelpVerbRequestedError)) { HelpVerbRequestedError error = (HelpVerbRequestedError)errors.First(e => e.Tag == ErrorType.HelpVerbRequestedError); if (error.Type != null) { help.AddVerbs(error.Type); } else { help.AddVerbs(typeof(SpeechRecognitionOptions), typeof(TTSOptions), typeof(FNOptions), typeof(PMOptions)); } Info(help); Exit(ExitResult.SUCCESS); } else if (errors.Any(e => e.Tag == ErrorType.HelpRequestedError)) { HelpRequestedError error = (HelpRequestedError)errors.First(e => e.Tag == ErrorType.HelpRequestedError); help.AddVerbs(typeof(SpeechRecognitionOptions), typeof(TTSOptions), typeof(FNOptions), typeof(PMOptions)); Info(help); Exit(ExitResult.SUCCESS); } else if (errors.Any(e => e.Tag == ErrorType.NoVerbSelectedError)) { help.AddVerbs(typeof(SpeechRecognitionOptions), typeof(TTSOptions), typeof(FNOptions), typeof(PMOptions)); Info(help); Exit(ExitResult.INVALID_OPTIONS); } else if (errors.Any(e => e.Tag == ErrorType.MissingRequiredOptionError)) { MissingRequiredOptionError error = (MissingRequiredOptionError)errors.First(e => e.Tag == ErrorType.MissingRequiredOptionError); Error("A required option is missing: {0}.", error.NameInfo.NameText); Info(help); Exit(ExitResult.INVALID_OPTIONS); } else if (errors.Any(e => e.Tag == ErrorType.UnknownOptionError)) { UnknownOptionError error = (UnknownOptionError)errors.First(e => e.Tag == ErrorType.UnknownOptionError); help.AddVerbs(typeof(SpeechRecognitionOptions), typeof(TTSOptions)); Error("Unknown option: {error}.", error.Token); Info(help); Exit(ExitResult.INVALID_OPTIONS); } else { Error("An error occurred parsing the program options: {errors}.", errors); help.AddVerbs(typeof(SpeechRecognitionOptions), typeof(TTSOptions)); Info(help); Exit(ExitResult.INVALID_OPTIONS); } }) .WithParsed <SpeechRecognitionOptions>(o => { ASR(); Exit(ExitResult.SUCCESS); }) .WithParsed <TTSOptions>(o => { TTS(o.Text); Exit(ExitResult.SUCCESS); }) .WithParsed <FNOptions>(o => { FNController.Options = o; new FNController(o).Start(); Exit(ExitResult.SUCCESS); }) .WithParsed <PMOptions>(o => { PMController.Options = o; new PMController(o).Start(); Exit(ExitResult.SUCCESS); }); }
public static string GetHelpForInvalidOptions(ParserResult <object> result, IEnumerable <Error> errors) { Type[] stageTypes = GetSubTypes <Stage>(ExplicitAssemblyName); HelpText help = HelpText.AutoBuild(result, h => { h.AddOptions(result); return(h); }, e => { return(e); }, true); help.MaximumDisplayWidth = Console.WindowWidth; help.Copyright = string.Empty; help.Heading = new HeadingInfo("ClassifyBot", Assembly.GetExecutingAssembly().GetName().Version.ToString(3)); help.AddPreOptionsLine(string.Empty); if (errors.Any(e => e.Tag == ErrorType.VersionRequestedError)) { help.Heading = string.Empty; return("{2}{0}v{1}".F(FiggleFonts.Chunky.Render("ClassifyBot"), Assembly.GetExecutingAssembly().GetName().Version.ToString(4), Environment.NewLine)); } else if (errors.Any(e => e.Tag == ErrorType.HelpVerbRequestedError)) { HelpVerbRequestedError error = (HelpVerbRequestedError)errors.First(e => e.Tag == ErrorType.HelpVerbRequestedError); if (error.Type != null) { help.AddVerbs(error.Type); } else { help.AddVerbs(stageTypes); } return(help); } else if (errors.Any(e => e.Tag == ErrorType.HelpRequestedError)) { HelpRequestedError error = (HelpRequestedError)errors.First(e => e.Tag == ErrorType.HelpRequestedError); help.AddOptions(result); return(help); } else if (errors.Any(e => e.Tag == ErrorType.NoVerbSelectedError)) { help.AddVerbs(stageTypes); help.AddPreOptionsLine("No stage selected. Select a stage or verb from the ones listed below:"); return(help); } else if (errors.Any(e => e.Tag == ErrorType.BadVerbSelectedError)) { BadVerbSelectedError error = (BadVerbSelectedError)errors.First(e => e.Tag == ErrorType.BadVerbSelectedError); help.AddVerbs(stageTypes); help.AddPreOptionsLine($"Unknown stage: {error.Token}. Valid stages and verbs are:"); return(help); } else if (errors.Any(e => e.Tag == ErrorType.MissingRequiredOptionError)) { MissingRequiredOptionError error = (MissingRequiredOptionError)errors.First(e => e is MissingRequiredOptionError); help.AddOptions(result); help.AddPreOptionsLine($"A required option or value is missing. The options and values for this stage are: "); return(help); } else if (errors.Any(e => e.Tag == ErrorType.MissingValueOptionError)) { MissingValueOptionError error = (MissingValueOptionError)errors.First(e => e.Tag == ErrorType.MissingValueOptionError); help.AddOptions(result); help.AddPreOptionsLine($"A required option or value is missing. The options and values for this stage are: "); return(help); } else if (errors.Any(e => e.Tag == ErrorType.UnknownOptionError)) { UnknownOptionError error = (UnknownOptionError)errors.First(e => e.Tag == ErrorType.UnknownOptionError); help.AddOptions(result); help.AddPreOptionsLine($"Unknown option: {error.Token}."); return(help); } else { help.AddPreOptionsLine($"An error occurred parsing the program options: {string.Join(" ", errors.Select(e => e.Tag.ToString()).ToArray())}"); help.AddVerbs(stageTypes); return(help); } }