Click or drag to resize

ShellCommand Class

Represents a command that can be invoked through a command line application that supports more than one operation.
Inheritance Hierarchy
SystemObject
  Ookii.CommandLineShellCommand

Namespace:  Ookii.CommandLine
Assembly:  Ookii.CommandLine (in Ookii.CommandLine.dll) Version: 2.3.0
Syntax
public abstract class ShellCommand

The ShellCommand type exposes the following members.

Constructors
  NameDescription
Protected methodShellCommand
Initializes a new instance of the ShellCommand class.
Top
Properties
  NameDescription
Public propertyExitCode
Gets or sets the exit code for the command.
Top
Methods
  NameDescription
Public methodStatic memberCreateShellCommand(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.
Public methodStatic memberCreateShellCommand(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.
Public methodStatic memberCreateShellCommand(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.
Public methodStatic memberCreateShellCommand(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.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodStatic memberGetShellCommand(Assembly, String)
Gets the shell command with the specified command name, using a case-insensitive string comparison for the command names.
Public methodStatic memberGetShellCommand(Assembly, String, IEqualityComparerString)
Public methodStatic memberGetShellCommandDescription
Gets the description of the specified shell command.
Public methodStatic memberGetShellCommandName
Gets the name of the specified shell command.
Public methodStatic memberGetShellCommands
Gets the Type instance for shell commands defined in the specified assembly.
Public methodGetType (Inherited from Object.)
Public methodStatic memberIsShellCommand
Determines whether the specified type is a shell command type.
Protected methodMemberwiseClone (Inherited from Object.)
Public methodRun
When implemented in a derived class, executes the command.
Public methodStatic memberRunShellCommand(Assembly, String, Int32)
Runs a shell command with the specified arguments; if the command name or arguments are invalid, prints error and usage information.
Public methodStatic memberRunShellCommand(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.
Public methodStatic memberRunShellCommand(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.
Public methodStatic memberRunShellCommand(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.
Public methodToString (Inherited from Object.)
Public methodStatic memberWriteAssemblyCommandList(TextWriter, Assembly)
Writes a list of all the shell commands in the specified assembly to the specified TextWriter.
Public methodStatic memberWriteAssemblyCommandList(TextWriter, Assembly, String)
Writes a list of all the shell commands in the specified assembly to the specified TextWriter using the specified formatting options.
Public methodStatic memberWriteAssemblyCommandListToConsole
Writes a list of all the shell commands in the specified assembly to the standard output stream.
Top
Remarks

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.

See Also