/// <summary> /// Configures this command by hooking up the input setup and the execution callback. /// </summary> private void ConfigureInternal() { this.m_commandLineApplication.Command( this.CommandName, (subCommandLineApplication) => { // This callback is called by the CommandLineUtils library to configure description/inputs, // and handle the exeuction logic subCommandLineApplication.Description = this.CommandDescription; CommandOption helpOption = subCommandLineApplication.Option( CLIApplication.HelpOptionSwitches, CLIApplication.HelpDescriptionText, CommandOptionType.NoValue); ConfiguredInputs configuredInputs = this.SetupInputs(subCommandLineApplication); subCommandLineApplication.OnExecute(() => { int exitCode = 1; // Display help information and return the appropriate exit code if either no inputs // where passed to a command that expects inputs, or a command parsing error occurred try { configuredInputs.ValidateInputs(subCommandLineApplication); configuredInputs.ValidateAllInputs?.Invoke(); } catch (Exception ex) { exitCode = this.OnInputValidationError(ex); subCommandLineApplication.ShowHelp(); return(exitCode); } try { if (helpOption.HasValue()) { Console.Write($"{this.CommandName}\t{this.CommandDescription}"); subCommandLineApplication.ShowHelp(); return(0); } // Execute the pre-execution routine if it is set if (this.m_onPreExecute != null) { this.m_onPreExecute(); } exitCode = this.OnExecute(configuredInputs); } catch (CommandParsingException exp) { // This will get invoked if the "command" part is not successfully parsed Console.Error.Write(exp.Message); subCommandLineApplication.ShowHelp(); exitCode = 1; } return(exitCode); }); }); }
/// <summary> /// Executes the CLI application. Handles all exceptions and returns appropriate exit code /// </summary> /// <param name="args">The inputs to the program</param> /// <returns>Return 0 if the function succeeded, other value if failed</returns> public int Execute(string[] args) { int exitCode = 1; try { ConfiguredInputs configuredInputs = null; if (this.SetupInputs != null) { configuredInputs = this.SetupInputs.Invoke(this.m_commandLineApplication); } this.m_commandLineApplication.OnExecute(() => { if (this.m_versionOption.HasValue()) { Console.WriteLine(this.m_versionString); return(0); } else if (this.m_helpOption.HasValue()) { this.ShowHelp(); return(0); } if (configuredInputs != null) { try { configuredInputs.ValidateInputs(this.m_commandLineApplication); } catch (Exception exp) { if (this.OnInputValidationError != null) { exitCode = this.OnInputValidationError.Invoke(exp); } else { // This can happen if caller doesn't want to use OnInputValidationError - and so // CLIApplication will default to just returning 1 as the exit code Console.Error.WriteLine("Error: {0}", exp.Message); exitCode = 1; } this.ShowHelp(); return(exitCode); } if (this.OnExecute != null) { return(this.OnExecute.Invoke(configuredInputs)); } else { this.ShowHelp(); return(1); } } else { this.ShowHelp(); return(1); } }); exitCode = this.m_commandLineApplication.Execute(args); } catch (CommandParsingException exp) { // This will get invoked if the "command" part is not successfully parsed Console.Error.WriteLine(exp.Message); this.m_commandLineApplication.ShowHelp(); exitCode = 1; } return(exitCode); }
/// <summary> /// Method that gets invoked when this command is being executed with validated inputs. /// Must be implemented by the concreted derived classes. /// Note: This method must handle all exceptions and return appropriate exit code for the program. /// </summary> /// <param name="configuredInputs">The validated inputs</param> /// <returns>The program exit code</returns> protected abstract int OnExecute(ConfiguredInputs configuredInputs);