C# Class CommandLine.UI

Main class for interacting via the command-line. Handles the definition and parsing of command-line arguments, and the display of usage and help messages.
显示文件 Open project: agardiner/hfmcmd

Public Properties

Property Type Description
Definition Definition
Interrupted bool
IsRedirected bool

Protected Properties

Property Type Description
_log log4net.ILog

Public Methods

Method Description
AddFlagArgument ( string key, string desc ) : FlagArgument

Convenience method for defining a new flag argument.

AddFlagArgument ( string key, string desc, Argument onParse ) : FlagArgument

Convenience method for defining a new flag argument.

AddKeywordArgument ( string key, string desc ) : KeywordArgument

Convenience method for defining a new keyword argument.

AddKeywordArgument ( string key, string desc, Argument onParse ) : KeywordArgument

Convenience method for defining a new keyword argument.

AddPositionalArgument ( string key, string desc ) : PositionalArgument

Convenience method for defining a new positional argument.

AddPositionalArgument ( string key, string desc, Argument onParse ) : PositionalArgument

Convenience method for defining a new positional argument.

ClassifyArguments ( IEnumerable args ) : int

Classifies the supplied list of arguments, returning a count of the number of positional arguments found.

ClearArguments ( ) : void

Clear existing arguments from the definition.

ClearLine ( ) : void

Clears the last text written to the console using Write. Note that this may be more than a single line of text, if the window width is less than the length of the string written.

DisplayTitle ( TextWriter console ) : void

Displays the application title, version etc.

DisplayUsage ( TextWriter console, object>.Dictionary args ) : void

Displays a usgae message, based on the allowed arguments and purpose represented by this class.

Parse ( IEnumerable args ) : object>.Dictionary

Parses the supplied set of arg strings using the list of Argument definitions maintained by this command-line UI instance.

ReadLine ( string prompt ) : string

For completeness - read a line of input from the console.

ReadPassword ( string prompt ) : string

Reads a password from the command-line, echoing * for each keypress.

RegisterCtrlHandler ( ) : Win32.Handler

This method registers this class as a handler for Ctrl-C etc events in the console. It returns a handle to the handler, which should be referenced via the following at the end of the program Main method: GC.KeepAlive(hr);

UI ( string help ) : System

Constructor; requires a purpose for the program whose args we are parsing.

WithColor ( ConsoleColor color, System.Action op ) : void

Performs the specified operation +op+ with the console color temporarily changed to +color+. On completion of the operation, the color is reset to the original color.

Write ( string line ) : void

Writes a partial line of text to the console, without moving to the next line.

WriteLine ( ) : void

Writes a blank line to the console.

WriteLine ( string text ) : void

Writes a line of text to the console, ensuring lines the same width as the console don't output an unnecessary new-line.

Protected Methods

Method Description
OutputArg ( Argument arg, TextWriter console ) : void

Private Methods

Method Description
CtrlHandler ( EInterruptTypes ctrlType ) : bool

Handler to receive control events, such as Ctrl-C and logoff and shutdown events. As a minimum, this logs the event, so that a record of why the process exited is maintained.

Method Details

AddFlagArgument() public method

Convenience method for defining a new flag argument.
public AddFlagArgument ( string key, string desc ) : FlagArgument
key string
desc string
return FlagArgument

AddFlagArgument() public method

Convenience method for defining a new flag argument.
public AddFlagArgument ( string key, string desc, Argument onParse ) : FlagArgument
key string
desc string
onParse Argument
return FlagArgument

AddKeywordArgument() public method

Convenience method for defining a new keyword argument.
public AddKeywordArgument ( string key, string desc ) : KeywordArgument
key string
desc string
return KeywordArgument

AddKeywordArgument() public method

Convenience method for defining a new keyword argument.
public AddKeywordArgument ( string key, string desc, Argument onParse ) : KeywordArgument
key string
desc string
onParse Argument
return KeywordArgument

AddPositionalArgument() public method

Convenience method for defining a new positional argument.
public AddPositionalArgument ( string key, string desc ) : PositionalArgument
key string
desc string
return PositionalArgument

AddPositionalArgument() public method

Convenience method for defining a new positional argument.
public AddPositionalArgument ( string key, string desc, Argument onParse ) : PositionalArgument
key string
desc string
onParse Argument
return PositionalArgument

ClassifyArguments() public method

Classifies the supplied list of arguments, returning a count of the number of positional arguments found.
public ClassifyArguments ( IEnumerable args ) : int
args IEnumerable
return int

ClearArguments() public method

Clear existing arguments from the definition.
public ClearArguments ( ) : void
return void

ClearLine() public method

Clears the last text written to the console using Write. Note that this may be more than a single line of text, if the window width is less than the length of the string written.
public ClearLine ( ) : void
return void

DisplayTitle() public method

Displays the application title, version etc.
public DisplayTitle ( TextWriter console ) : void
console System.IO.TextWriter
return void

DisplayUsage() public method

Displays a usgae message, based on the allowed arguments and purpose represented by this class.
public DisplayUsage ( TextWriter console, object>.Dictionary args ) : void
console System.IO.TextWriter
args object>.Dictionary
return void

OutputArg() protected method

protected OutputArg ( Argument arg, TextWriter console ) : void
arg Argument
console System.IO.TextWriter
return void

Parse() public method

Parses the supplied set of arg strings using the list of Argument definitions maintained by this command-line UI instance.
public Parse ( IEnumerable args ) : object>.Dictionary
args IEnumerable
return object>.Dictionary

ReadLine() public method

For completeness - read a line of input from the console.
public ReadLine ( string prompt ) : string
prompt string
return string

ReadPassword() public method

Reads a password from the command-line, echoing * for each keypress.
public ReadPassword ( string prompt ) : string
prompt string
return string

RegisterCtrlHandler() public static method

This method registers this class as a handler for Ctrl-C etc events in the console. It returns a handle to the handler, which should be referenced via the following at the end of the program Main method: GC.KeepAlive(hr);
public static RegisterCtrlHandler ( ) : Win32.Handler
return Win32.Handler

UI() public method

Constructor; requires a purpose for the program whose args we are parsing.
public UI ( string help ) : System
help string
return System

WithColor() public method

Performs the specified operation +op+ with the console color temporarily changed to +color+. On completion of the operation, the color is reset to the original color.
public WithColor ( ConsoleColor color, System.Action op ) : void
color ConsoleColor
op System.Action
return void

Write() public method

Writes a partial line of text to the console, without moving to the next line.
public Write ( string line ) : void
line string
return void

WriteLine() public method

Writes a blank line to the console.
public WriteLine ( ) : void
return void

WriteLine() public method

Writes a line of text to the console, ensuring lines the same width as the console don't output an unnecessary new-line.
public WriteLine ( string text ) : void
text string
return void

Property Details

Definition public_oe property

public Definition,CommandLine Definition
return Definition

Interrupted public_oe static_oe property

Static public flag indicating that the application is to terminate immediately, e.g. in response to a Ctrl-C or Logoff event. Any long- running command should check this flag periodically and attempt to abort gracefully.
public static bool Interrupted
return bool

IsRedirected public_oe static_oe property

public static bool IsRedirected
return bool

_log protected_oe static_oe property

protected static ILog,log4net _log
return log4net.ILog