Click or drag to resize

CommandLine Class

Performs common operations on application a DOS or Linux command line.
Inheritance Hierarchy

Namespace:  Neon.Common
Assembly:  Neon.Common (in Neon.Common.dll) Version: 2.14.0
public sealed class CommandLine

The CommandLine type exposes the following members.

Public methodCommandLine
Constructs an instance optionally expanding any response files specified in the arguments passed.
Public propertyArguments
Returns the array of command line values (items that are not command line options).
Public propertyHasHelpOption
Determines if the --help command line option is present.
Public propertyItem
Returns an item from the command line based on its position.
Public propertyItems
Returns the array of command line arguments (including both command line options and values).
Public propertyOptions
Returns the command line options as a dictionary of option name/value tuples.
Public methodDefineOption
Adds an option definition to the command line and returns the definition so its default value may be set if desired.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodStatic memberExpandFiles
Expands the command line by processing items beginning with '@' as input files.
Public methodStatic memberExpandWildcards
Checks the argument passed for wildcards and expands them into the appopriate set of matching file names.
Public methodStatic memberFormat
Formats an array of objects into a form suitable for passing to a process on the command line by adding double quotes around any values with embedded spaces.
Public methodGetArguments
Enumerates the command line arguments beginning at the specified index.
Public methodGetFlag
Determines whether an option is present on the command line.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetOption
Returns the value associated with a command line option if the option was present on the command line otherwise, the specified default value will be returned.
Public methodGetOptionValues
Returns all of the values a command line option that appears multiple times in the command.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodHasOption
Determines if an option was present on the command line.
Public methodStatic memberParse
Parses the argument string passed into a CommandLine instance, dealing with quoted parameters, etc.
Public methodShift
Returns a new CommandLine which includes all of the command line options and the arguments starting at the position passed to the end of the command line, essentially shifting arguments to the left.
Public methodSplit
Splits the command line into two parts, the command line to the left of the first specified item (defaults to "--") and the command line to the right of it.
Public methodStartsWithArgs
Determines whether the command line starts with the specified arguments.
Public methodToString
Renders the command line as a string suitable for presenting to a process or a command line shell. Arguments that include spaces will be enclosed in double quotes.
(Overrides ObjectToString.)

Command lines may consist of zero or more items. Items that begin with the dash (-) character are considered to be command line options. Items that begin with an at sign (@) can be considered to be response files, and all other items are considered to be command line arguments.

Note Note
The "-" and "--" items (without an option name are considered arguments, not options.

The static ExpandFiles(String) method can be used to process response files specified in a command line. Response files are specified by prepending a '@' character to the name of a text file and then treating each line of the file as a command line item.

The static ExpandWildcards(String) method can be used to expand file names with wildcard characters into the set of actual files that match the pattern.

The CommandLine class can also handles parsing of items as command line options.

-<option name>[=<value>]

will be parsed into name/value pairs and will be available for lookup via the string keyed indexer. Options that specify no value will be assigned an empty string value.

Note Note
Command line option names are case sensitive.

The class will also make all command line items available via the integer keyed indexer which will return items based on their position on the command line and also via the Items property. Command line items that are not command, are available via the Arguments property. Options can be looked up via the GetOption(String, String) and GetOptionValues(String) overrides.

CommandLine also supports the definition of long and short forms of options with optional default values using the DefineOption(String) method. This associates one or more option names with an optional default value.

You can use this easily implement the short and long forms of options as well as to centralize the specification of option default values.

var commandLine = new CommandLine(args);

commandLine.DefineOption("-q", "--quiet");
commandLine.DefineOption("-k", "--key").Default = "none";

// These calls both return the option value for "-q" or "--quiet".
// Since no default value was set, the default value will be the
// empty string.


// These calls both return the option value for "-k" or "--key".
// The default value will be "none".

Note Note
This class assumes that the position of command line options doesn't matter, which is somewhat simplistic. In particular, the Shift(Int32, String) method actually relocates all of the options to the beginning of the shifted command line.
See Also