Game Development Reference
In-Depth Information
Sample Usage
Using the command line tokenizer is very simple. Console applications have a
string array that is passed into the main entry point, and this string array contains
the command line parameters specified at the command prompt. Instantiate a
new instance of the ConsoleTokenizer class and pass this string array into it. At this
point everything has been parsed, and you can either access the Parameters or Files
property of the tokenizer instance. Parameters is a string dictionary that uses the
parameter name as a key, and then points to the associated value. Here is an example
of how to get the parameter value for a parameter named mode.
static void Main(string[] args)
{
ConsoleTokenizer tokenizer = new ConsoleTokenizer(args);
string mode = tokenizer.Parameters[“mode”];
}
Alternatively, the indexer operator has been overloaded to reference the Parameters
dictionary as well, making your code even cleaner.
static void Main(string[] args)
{
ConsoleTokenizer tokenizer = new ConsoleTokenizer(args);
string mode = tokenizer[“mode”];
}
There may be some optional parameters that you want to use if they are present.
If you access the Parameters string dictionary using a key that does not exist, you
will be returned null . This is to signify that no such parameter was found. Every
parameter should be tested for null to prevent null reference exceptions. This is
also how you would enforce required parameters.
static void Main(string[] args)
{
ConsoleTokenizer tokenizer = new ConsoleTokenizer(args);
string mode = string.Empty;
if (tokenizer[“mode”] != null)
{
mode = tokenizer[“mode”];
}
}
Search Nedrilad ::




Custom Search