📄 options.cs
字号:
//-----------------------------------------------------------------------------
//
// File: Options.cs
// Description:
// Options defines the Option class used to manage command-line parameters.
//
//
// History:
// 8/22/2001: JStall: Created
//
//-----------------------------------------------------------------------------
using System;
using System.IO;
using System.Collections;
using System.Diagnostics;
namespace Blue.Utilities
{
//-----------------------------------------------------------------------------
// Option handlers can throw this exception
//-----------------------------------------------------------------------------
internal class OptionErrorException : System.Exception
{
public OptionErrorException(string stHint) : base(stHint)
{
}
}
//-----------------------------------------------------------------------------
// The option class.
//-----------------------------------------------------------------------------
internal class Options : Blue.Public.IOptions
{
#region Construction
/***************************************************************************\
*
* Options.Options
*
* Options() initializes a new Options object.
*
\***************************************************************************/
internal
Options()
{
m_tblOptions = new Hashtable();
m_fIsLocked = false;
// @dogfood - In delegate, we should be able to say 'Option_Help', not 'this.Option_Help'
AddHandler(
"help", "?",
new Blue.Public.OptionHandler(this.Option_Help),
"provide detailed help on a specific command",
"ex:\n"+
"/help - print a summary of all commands\n"+
"/help:define - print detailed help for the 'define' command\n"+
"/help:* - print detailed help for all commands\n"
);
}
#endregion Construction
#region Error handling
// The option manager doesn't recognize the following option
static void PrintError_UnrecognizedOption(string stOption)
{
Blue.Driver.StdErrorLog.PrintError(
new Blue.Driver.GeneralErrorException(
Blue.Driver.ErrorCodes.cInternalError,
null,
"The option '" + stOption + "' is not recognized"
)
);
}
static void PrintError_CantOpenResponseFile(string stFilename, System.Exception e)
{
Blue.Driver.StdErrorLog.PrintError(
new Blue.Driver.GeneralErrorException(
Blue.Driver.ErrorCodes.cCantOpenResponseFile,
null,
"Can't open response file '"+stFilename + "'. " + e.Message
)
);
}
// General error mechanism for options
static void PrintError_OptionError(string stOption, string stParam, string stHint)
{
Blue.Driver.StdErrorLog.PrintError(
new Blue.Driver.GeneralErrorException(
Blue.Driver.ErrorCodes.cOptionError,
null,
"The option '" + stOption + "' has illegal parameter '" + stParam + "'."+ stHint
)
);
}
#endregion
#region Implementation for IOption
//-----------------------------------------------------------------------------
// Adds a new handler for the specified option.
//-----------------------------------------------------------------------------
public void
AddHandler(
string stOption, // Unique flag for Option
string stShortcut, // Optional shortcut (can be null);
Blue.Public.OptionHandler ha, // Option Handler
string stDescription, // short 1-line description
string stFullHelp // long description
)
{
Debug.Assert(!m_fIsLocked, "Can't add new optionhandler after we've locked:"+ stDescription);
OptionInfo info = new OptionInfo(stOption, stShortcut, ha, stDescription, stFullHelp);
if (m_tblOptions[stOption] == null)
{
if (stShortcut != null)
{
Debug.Assert(m_tblOptions[stShortcut] == null);
m_tblOptions[stShortcut] = info;
}
m_tblOptions[stOption] = info;
}
else
{
// Since end users can't define option handlers, we should know about
// any collisions during the debug phase.
Debug.Assert(false, "Handler already defined");
}
}
#endregion
#region methods
#region OptionInfo class
// Class to bundle all the option-information together for each option
// Immutable.
class OptionInfo
{
// Constructor
public OptionInfo(
string stOption,
string stShortcut,
Blue.Public.OptionHandler ha,
string stDescription,
string stFullHelp
)
{
Debug.Assert(stOption != null, "Must specify option");
Debug.Assert(ha != null, "Must have valid handler");
Debug.Assert(stDescription != null, "Must have valid description");
m_stOption = stOption;
m_stShortcut = stShortcut; // can be null
m_ha = ha;
m_stDescription = stDescription;
m_stFullHelp = (stFullHelp == null) ? stDescription : stFullHelp;
}
// Data
readonly string m_stOption; // Unique flag for Option
readonly string m_stShortcut; // Optional shortcut (can be null);
readonly Blue.Public.OptionHandler m_ha; // Option Handler
readonly string m_stDescription; // short 1-line description
readonly string m_stFullHelp; // long description
// Properties
public string Option { get { return m_stOption; } }
public string Shortcut { get { return m_stShortcut; } }
public Blue.Public.OptionHandler Handler { get { return m_ha; } }
public string ShortHelp { get { return m_stDescription; } }
public string FullHelp { get { return m_stFullHelp; } }
}
#endregion
//-------------------------------------------------------------------------
// Dispatch a response file. This will parse the file and call DispatchOption
//-------------------------------------------------------------------------
internal void DispatchResponseFile(string stFilename, ArrayList alFiles)
{
//System.IO.TextReader r = new System.IO.StreamReader(stFilename);
TextReader input = null;
try
{
input = File.OpenText(stFilename);
}
catch(Exception e)
{
Options.PrintError_CantOpenResponseFile(stFilename, e);
return;
}
Debug.Assert(input != null);
string line;
while((line = input.ReadLine())!= null)
{
// Skip comments and blank lines
if (line.Length <= 1)
continue;
if (line[0] == '#')
continue;
// Process
string [] stArgs = line.Trim(' ').Split(' '); // split by spaces
foreach(string stArg in stArgs)
DispatchOption(stArg, alFiles);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -