|
using System;
using System.IO;
using System.Collections;
namespace VersionVDProj
{
/// <summary>
/// Summary description for ArgumentParser.
/// </summary>
public class ArgumentParser
{
public readonly Hashtable Options;
public readonly string Command;
public readonly FileInfo ProjectFile;
public ArgumentParser( string[] args )
{
Options = new Hashtable();
if ( args.Length < 2 )
throw new Exception( "Wrong number of arguments" );
Command = ParseCommand( args[0] );
ProjectFile = ParseFileName( args[1] );
for ( int i = 2; i < args.Length; i++ )
ParseArgument( args[i] );
}
private string ParseCommand( string command )
{
if ( command.Substring( 0, 1 ) != "-" )
throw new Exception( string.Format( "Unrecognized command {0}", command ) );
return command.Substring( 1 ).ToLower();
}
private FileInfo ParseFileName( string path )
{
FileInfo file = new FileInfo( path );
if ( !file.Exists )
throw new Exception( string.Format( "The file {0} could not be found", path ) );
return file;
}
private void ParseArgument( string arg )
{
int eqPos = arg.IndexOf( '=', 0 );
if ( eqPos == -1 )
throw new Exception( string.Format( "The argument {0} is not in the format NAME=VALUE", arg ) );
string name = arg.Substring( 0, eqPos );
string val = arg.Substring( eqPos + 1 );
Options.Add( name.ToLower(), val );
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10
It only went downhill from there.
Hey look, I've got a blog