ShellCommand Class |
Namespace: Ookii.CommandLine
The ShellCommand type exposes the following members.
Name | Description | |
---|---|---|
![]() | ShellCommand |
Initializes a new instance of the ShellCommand class.
|
Name | Description | |
---|---|---|
![]() ![]() | CreateShellCommand(Assembly, String, Int32) |
Finds and instantiates the shell command from the specified arguments, or if that fails, writes error and usage information to the standard error and output streams.
|
![]() ![]() | CreateShellCommand(Assembly, String, String, Int32) |
Finds and instantiates the shell command with the specified name, or if that fails, writes error and usage information to the standard error and output streams.
|
![]() ![]() | CreateShellCommand(Assembly, String, Int32, CreateShellCommandOptions) |
Finds and instantiates the shell command from the specified arguments, or if that fails, writes error and usage information to the specified writers.
|
![]() ![]() | CreateShellCommand(Assembly, String, String, Int32, CreateShellCommandOptions) |
Finds and instantiates the shell command with the specified name, or if that fails, writes error and usage information to the specified writers.
|
![]() | Equals | (Inherited from Object.) |
![]() | Finalize | (Inherited from Object.) |
![]() | GetHashCode | (Inherited from Object.) |
![]() ![]() | GetShellCommand(Assembly, String) |
Gets the shell command with the specified command name, using a case-insensitive string comparison for the command names.
|
![]() ![]() | GetShellCommand(Assembly, String, IEqualityComparerString) |
Gets the shell command with the specified command name, using the specified IEqualityComparerT to compare command names.
|
![]() ![]() | GetShellCommandDescription |
Gets the description of the specified shell command.
|
![]() ![]() | GetShellCommandName |
Gets the name of the specified shell command.
|
![]() ![]() | GetShellCommands |
Gets the Type instance for shell commands defined in the specified assembly.
|
![]() | GetType | (Inherited from Object.) |
![]() ![]() | IsShellCommand |
Determines whether the specified type is a shell command type.
|
![]() | MemberwiseClone | (Inherited from Object.) |
![]() | Run |
When implemented in a derived class, executes the command.
|
![]() ![]() | RunShellCommand(Assembly, String, Int32) |
Runs a shell command with the specified arguments; if the command name or arguments are invalid, prints error and usage information.
|
![]() ![]() | RunShellCommand(Assembly, String, String, Int32) |
Runs the specified shell command with the specified arguments; if the command name or arguments are invalid, prints error and usage information.
|
![]() ![]() | RunShellCommand(Assembly, String, Int32, CreateShellCommandOptions) |
Runs a shell command with the specified arguments; if the command name or arguments are invalid, prints error and usage information.
|
![]() ![]() | RunShellCommand(Assembly, String, String, Int32, CreateShellCommandOptions) |
Runs the specified shell command with the specified arguments; if the command name or arguments are invalid, prints error and usage information.
|
![]() | ToString | (Inherited from Object.) |
![]() ![]() | WriteAssemblyCommandList(TextWriter, Assembly) |
Writes a list of all the shell commands in the specified assembly to the specified TextWriter.
|
![]() ![]() | WriteAssemblyCommandList(TextWriter, Assembly, String) |
Writes a list of all the shell commands in the specified assembly to the specified TextWriter using the specified formatting options.
|
![]() ![]() | WriteAssemblyCommandListToConsole |
Writes a list of all the shell commands in the specified assembly to the standard output stream.
|
Shell commands can be used to create shell utilities that perform more than one operation, where each operation has its own set of command line arguments. For example, a utility might be used to modify or query different configuration parameters of a system. Depending on whether it's a query or a modification, and which configuration parameter is used, the arguments to such a utility might differ. Rather than provide different executables for each operation, it is often more convenient to combine related operations in a single utility.
For a program using shell commands, typically the first command line argument will be the name of the operation and identifies which shell command to use, while the remaining arguments are arguments to the command. The ShellCommand class aids you in creating utilities that follow this pattern.
A shell command is created by deriving a type from the ShellCommand class, specifying the ShellCommandAttribute on that type to specify the name of the command, and implementing the Run method for that type.
An application can get a list of all shell commands defined in an assembly by using the GetShellCommands(Assembly) method, or get a specific shell command using the GetShellCommand(Assembly, String) method. The GetShellCommand(Assembly, String) method searches for a type that inherits from ShellCommand and defines the ShellCommandAttribute attribute, and where the value of the CommandName property matches the specified command name. If a matching type is found, it returns the Type instance for that type.
Shell commands behave like regular command line arguments classes for the CommandLineParser class. Once a shell command has been found using the GetShellCommand(Assembly, String) method, you can instantiate it by creating an instance of the CommandLineParser class, passing the shell command's Type to the CommandLineParser(Type) constructor. Then invoke the Parse(String, Int32) method to parse the shell command's arguments (make sure to set index so that the command does not try to parse the command name), and cast the result to a ShellCommand instance. Then invoke the Run method to invoke the command.
It is recommended to return the value of the ExitCode property to the operating system (by returning it from the Main method or by using the ExitCode property) after running the shell command.
Various utility methods to find, create and run shell commands are provided as members of the ShellCommand class.