Class CommandBase
Base class for all command implementations
Inherited Members
Namespace: Benday.CommandsFramework
Assembly: Benday.CommandsFramework.dll
Syntax
public abstract class CommandBase
Constructors
| Edit this page View SourceCommandBase(CommandExecutionInfo, ITextOutputProvider)
Constructor
Declaration
public CommandBase(CommandExecutionInfo info, ITextOutputProvider outputProvider)
Parameters
| Type | Name | Description |
|---|---|---|
| CommandExecutionInfo | info | Execution information for the requested command |
| ITextOutputProvider | outputProvider | Output provider |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
Fields
| Edit this page View Source_OutputProvider
Declaration
protected readonly ITextOutputProvider _OutputProvider
Field Value
| Type | Description |
|---|---|
| ITextOutputProvider |
Properties
| Edit this page View SourceArguments
Arguments and values for the command. These are the combination of the argument definitions with the values from the command line. The values are set when the command is executed.
Declaration
public ArgumentCollection Arguments { get; }
Property Value
| Type | Description |
|---|---|
| ArgumentCollection |
Description
Human readable description of this command
Declaration
public string Description { get; }
Property Value
| Type | Description |
|---|---|
| string |
ExecutionInfo
Property for accessing the raw execution info for the command
Declaration
public CommandExecutionInfo ExecutionInfo { get; }
Property Value
| Type | Description |
|---|---|
| CommandExecutionInfo |
IsQuietMode
Is this command running in quiet mode? Quiet mode suppresses the output written by WriteLine(). It is set by the reserved 'quiet' argument and is applied automatically to commands that are run by another command.
Declaration
public bool IsQuietMode { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
| Edit this page View SourceCreateCommand<T>(Action<Dictionary<string, string>>?, bool)
Creates another command so that its logic can be reused from inside this command. The new command shares this command's program options, configuration and output provider, and runs in quiet mode by default so that it does not write over the calling command's output. The command is created but not run -- use ExecuteCommand() or ExecuteCommandAsync() to create and run it in one step.
Declaration
protected T CreateCommand<T>(Action<Dictionary<string, string>>? configureArguments = null, bool quiet = true) where T : CommandBase
Parameters
| Type | Name | Description |
|---|---|---|
| Action<Dictionary<string, string>> | configureArguments | Callback for populating the arguments for the command |
| bool | quiet | Run the command in quiet mode. Defaults to true. |
Returns
| Type | Description |
|---|---|
| T | The new command instance |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the command to create |
Exceptions
| Type | Condition |
|---|---|
| KnownException | Thrown when the command type cannot be used |
DisplayUsage()
Displays the command usage description
Declaration
protected virtual void DisplayUsage()
DisplayUsage(StringBuilder)
Adds the command usage description to the provided string builder
Declaration
protected void DisplayUsage(StringBuilder builder)
Parameters
| Type | Name | Description |
|---|---|---|
| StringBuilder | builder | StringBuilder instance |
DisplayValidationSummary(List<IArgument>)
Creates and displays the validation summary when there are failed argument validations
Declaration
protected virtual void DisplayValidationSummary(List<IArgument> invalidArguments)
Parameters
| Type | Name | Description |
|---|---|---|
| List<IArgument> | invalidArguments | Collection of invalid arguments |
ExecuteCommandAsync<T>(Action<Dictionary<string, string>>?, bool)
Creates another command, validates it, and runs it asynchronously. The command instance is returned so that results can be read back off it. Unlike running a command from the command line, a validation failure here throws rather than printing the usage information, because the calling command needs to know that the command did not run.
Declaration
protected Task<T> ExecuteCommandAsync<T>(Action<Dictionary<string, string>>? configureArguments = null, bool quiet = true) where T : AsynchronousCommand
Parameters
| Type | Name | Description |
|---|---|---|
| Action<Dictionary<string, string>> | configureArguments | Callback for populating the arguments for the command |
| bool | quiet | Run the command in quiet mode. Defaults to true. |
Returns
| Type | Description |
|---|---|
| Task<T> | The command instance after it has run |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the command to run |
Exceptions
| Type | Condition |
|---|---|
| KnownException | Thrown when the arguments for the command are not valid |
ExecuteCommand<T>(Action<Dictionary<string, string>>?, bool)
Creates another command, validates it, and runs it. The command instance is returned so that results can be read back off it. Unlike running a command from the command line, a validation failure here throws rather than printing the usage information, because the calling command needs to know that the command did not run.
Declaration
protected T ExecuteCommand<T>(Action<Dictionary<string, string>>? configureArguments = null, bool quiet = true) where T : SynchronousCommand
Parameters
| Type | Name | Description |
|---|---|---|
| Action<Dictionary<string, string>> | configureArguments | Callback for populating the arguments for the command |
| bool | quiet | Run the command in quiet mode. Defaults to true. |
Returns
| Type | Description |
|---|---|
| T | The command instance after it has run |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the command to run |
Exceptions
| Type | Condition |
|---|---|
| KnownException | Thrown when the arguments for the command are not valid |
GetArgValueOrRequiredConfigValue(string, string)
Declaration
public string GetArgValueOrRequiredConfigValue(string argumentName, string configValueName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | argumentName | |
| string | configValueName |
Returns
| Type | Description |
|---|---|
| string |
GetArguments()
Get the argument definitions for the command execution. These are used to validate the execution but do not have any actual values.
Declaration
public virtual ArgumentCollection GetArguments()
Returns
| Type | Description |
|---|---|
| ArgumentCollection |
SetValuesFromExecutionInfo()
Reads the arguments from the execution info and sets the values on to the argument definitions for the command. For arguments marked with FromConfig(), values are loaded from configuration with command line taking precedence.
Declaration
protected virtual void SetValuesFromExecutionInfo()
Validate()
Validate the arguments provided using the required argument definition for the command.
Declaration
protected virtual List<IArgument> Validate()
Returns
| Type | Description |
|---|---|
| List<IArgument> | List of invalid arguments |
WriteLine()
Write a new line to the output provider. Does nothing in quiet mode.
Declaration
protected virtual void WriteLine()
WriteLine(string)
Write a message to the output provider. Does nothing in quiet mode.
Declaration
protected virtual void WriteLine(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | Message to write |