mcs::CommandParser Class Reference

#include <mcs.hh>

List of all members.


Detailed Description

Parse command lines.

This class parses a MCS command line (very close to the one used in a unix like shell) into its tokens.

In a command line tokens are separated by one or more blanks or tabs. A token can also contains blanks, because everything enclosed in double quotes is taken as a single token, in this case the double quotes are removed from the token. Double quotes can be escaped by a backslash, this way becoming part of the token. Also backslashes can be escaped with another backslash. Tokens are available through the tokenc() and token() methods.

The first token is considered the command, returned by the cmd() method.

After being parsed the tokens are separated into options and arguments. A token is an options if it start with a minus "-" character like the options in a unix shell, and can have an optional value (following the "=" chatracter). Argument are all the other tokens. Options are available through the optc(), opt(), givenOpt() methods. Argument to options are available through the optArg() method. Arguments are available through the argc() and arg() methods.

This class can also be used to read a MCS batch file, or SQL file and perform parameter substitutions. In these scripts an instruction terminate with a semicolon (";") character. Comments are everything between the "#" character and a newline. Parameters are substituted using the $1, $2, etc... placeholders. The replPars_onFile() and replPars() to this job. Consider the following script:

  #This is a comment

  line 1;
  line 2;  #A line with a comment
  line $1;
  line $2; #A line with a comment ;

  line splitted
      in two $3;

  line splitted   #with comment on the first line
      in two $3;  #and on the second !"£$%&;/()='ì

And a command line:

  script one two three

where "script" is the name of the script. A call to replPars_onFile() will return a vector of strings as follows:

  line 1
  line 2
  line one
  line two
  line splitted in two three
  line splitted in two three

Definition at line 5200 of file mcs.hh.


Public Member Functions

string allargs ()
 Returns all arguments (not tokens!) as a string.
Dataarg (int i)
 Return argument at i-th position.
int argc ()
 Return number of arguments.
string cline ()
 Returns the entire command line.
string cmd ()
 Returns the first token, that is the command.
bool cmpCmd (string cmd)
 Check if the string in the parameter is the same as the command (case insensitive).
 CommandParser ()
 Constructor.
 CommandParser (const CommandParser &)
 Declared to avoid using of default copy constructor.
bool givenOpt (string opt, int &i)
 Check if a certain option has been given and return the index in the "i" parameter.
bool givenOpt (string opt)
 Check if a certain option has been given.
string line_afterToken (int i)
 Return the "rest" relative to token at i-th position.
CommandParseroperator= (const CommandParser &)
 Declared to avoid using of default assignment operator.
string opt (int i)
 Return option at i-th position.
Dataoptarg (int i)
 Return argument to option at i-th position.
int optc ()
 Return number of options.
void parseCmd (int argc, char *argv[])
 Parse a command line using standard argc and argv parameters.
void parseCmd (string c)
 Parse a command line into tokens, arguments and options.
string replPars (string str)
 Perform parameter substitutions.
vector< string > replPars_onFile (string fn)
 Parse a MCS script.
string sarg (int i)
 Return argument at i-th position as a string, see arg(int).
string token (int i)
 Return token at i-th position.
int tokenc ()
 Return number of tokens.
 ~CommandParser ()
 Destructor, this destroy all Data objects containing arguments.

Private Member Functions

void clearArgs ()
 Destroy all Data objects containing arguments.

Static Private Member Functions

static string clean (string c)
 Cleans a string.

Private Attributes

vector< string > args
 vector of arguments.
string lcmd
 First token, that is the command.
 MCS_DEBUG_ALLOC
Datamd_args [100]
 Array of arguments stored as Data objects.
Datamd_optargs [100]
 Array of arguments to options stored as Data objects.
vector< string > optargs
 vector of argument to options.
vector< string > opts
 vector of options.
string origcmdline
 Command line parsed.
vector< string > rest
 vector of "rests".
vector< string > tokens
 vector of tokens.

Constructor & Destructor Documentation

mcs::CommandParser::CommandParser ( const CommandParser  ) 

Declared to avoid using of default copy constructor.

Warning:
This constructor is declared but not implemented. If you try to use it you will get a compilation error.

mcs::CommandParser::CommandParser (  ) 

Constructor.

Definition at line 28 of file CommandParser.cc.

mcs::CommandParser::~CommandParser (  ) 

Destructor, this destroy all Data objects containing arguments.

Definition at line 39 of file CommandParser.cc.


Member Function Documentation

string mcs::CommandParser::allargs (  ) 

Returns all arguments (not tokens!) as a string.

Definition at line 85 of file CommandParser.cc.

Data & mcs::CommandParser::arg ( int  i  ) 

Return argument at i-th position.

The index must be in the allowed range (0 <= i < argc()). Note that i-th position, doesn't mean that the argument is at the i-th position in the command line. Those are tokens.

Parameters:
i Index of the argument to be returned.
Returns:
A reference to a STRING Data object containing the argument at i-th position.
Exceptions:
ERROR MSG_INDEX_LT_ZERO.
ERROR MSG_INDEX_OUT_RANGE.

Definition at line 136 of file CommandParser.cc.

int mcs::CommandParser::argc (  ) 

Return number of arguments.

Definition at line 96 of file CommandParser.cc.

string mcs::CommandParser::clean ( string  c  )  [static, private]

Cleans a string.

Remove tabs, newline, carriage-return, multiple spaces and trailing ";".

Definition at line 46 of file CommandParser.cc.

void mcs::CommandParser::clearArgs (  )  [private]

Destroy all Data objects containing arguments.

Definition at line 55 of file CommandParser.cc.

string mcs::CommandParser::cline (  ) 

Returns the entire command line.

Definition at line 74 of file CommandParser.cc.

string mcs::CommandParser::cmd (  ) 

Returns the first token, that is the command.

Definition at line 80 of file CommandParser.cc.

bool mcs::CommandParser::cmpCmd ( string  cmd  ) 

Check if the string in the parameter is the same as the command (case insensitive).

Definition at line 190 of file CommandParser.cc.

bool mcs::CommandParser::givenOpt ( string  opt,
int &  i 
)

Check if a certain option has been given and return the index in the "i" parameter.

Definition at line 202 of file CommandParser.cc.

bool mcs::CommandParser::givenOpt ( string  opt  ) 

Check if a certain option has been given.

Definition at line 215 of file CommandParser.cc.

string mcs::CommandParser::line_afterToken ( int  i  ) 

Return the "rest" relative to token at i-th position.

The index must be in the allowed range (0 <= i < tokenc())

Parameters:
i Index of the "rest" to be returned.
Returns:
A string containing the "rest" at i-th position.
Exceptions:
ERROR MSG_INDEX_LT_ZERO.
ERROR MSG_INDEX_OUT_RANGE.
See also:
rest.

Definition at line 122 of file CommandParser.cc.

CommandParser& mcs::CommandParser::operator= ( const CommandParser  ) 

Declared to avoid using of default assignment operator.

Warning:
This operator is declared but not implemented. If you try to use it you will get a compilation error.

string mcs::CommandParser::opt ( int  i  ) 

Return option at i-th position.

The index must be in the allowed range (0 <= i < optc()). Note that i-th position, doesn't mean that the option is at the i-th position in the command line. Those are tokens.

Parameters:
i Index of the option to be returned.
Returns:
A string containing the option at i-th position, without the minus ("-") character.
Exceptions:
ERROR MSG_INDEX_LT_ZERO.
ERROR MSG_INDEX_OUT_RANGE.

Definition at line 156 of file CommandParser.cc.

Data & mcs::CommandParser::optarg ( int  i  ) 

Return argument to option at i-th position.

Argument to options are the part of the option token after the equal "=" character. If the equal character is not present in the option the corresponding argument is an empty string.

The index must be in the allowed range (0 <= i < optc()). Note that i-th position, doesn't mean that the option is at the i-th position in the command line. Those are tokens.

Parameters:
i Index of the option to be returned.
Returns:
A string containing the argument to the option at i-th position, without the equal ("=") character.
Exceptions:
ERROR MSG_INDEX_LT_ZERO.
ERROR MSG_INDEX_OUT_RANGE.

Definition at line 176 of file CommandParser.cc.

int mcs::CommandParser::optc (  ) 

Return number of options.

Definition at line 170 of file CommandParser.cc.

void mcs::CommandParser::parseCmd ( int  argc,
char *  argv[] 
)

Parse a command line using standard argc and argv parameters.

Definition at line 222 of file CommandParser.cc.

void mcs::CommandParser::parseCmd ( string  c  ) 

Parse a command line into tokens, arguments and options.

Definition at line 233 of file CommandParser.cc.

string mcs::CommandParser::replPars ( string  str  ) 

Perform parameter substitutions.

After a command line have been parsed (with parseCmd()) we have at our disposal a number of argument. This method substitute every occurrence of a placeholder of the type $1, $2, etc..., in "str" parameter with the corresponding parameter. It also remove comments.

Parameters:
str The string in which perform parameter substitutions.
Returns:
The final string.

Definition at line 349 of file CommandParser.cc.

vector< string > mcs::CommandParser::replPars_onFile ( string  fn  ) 

Parse a MCS script.

This method reads the file given as parameter, and parse it into single instructions, that is string terminating with a semicolon ";" character. Then it passes the instruction to the replPars() method to substitute the parameters. The returned strings are stored in a vector that is returned after all the content of the file have been parsed.

Parameters:
fn File to parse.
Returns:
vector of string containing instructions.
Exceptions:
ERROR MSG_CANT_OPEN_FILE.

Definition at line 318 of file CommandParser.cc.

string mcs::CommandParser::sarg ( int  i  ) 

Return argument at i-th position as a string, see arg(int).

Definition at line 150 of file CommandParser.cc.

string mcs::CommandParser::token ( int  i  ) 

Return token at i-th position.

The index must be in the allowed range (0 <= i < tokenc())

Parameters:
i Index of the token to be returned.
Returns:
A string containing the token at i-th position.
Exceptions:
ERROR MSG_INDEX_LT_ZERO.
ERROR MSG_INDEX_OUT_RANGE.

Definition at line 108 of file CommandParser.cc.

int mcs::CommandParser::tokenc (  ) 

Return number of tokens.

Definition at line 102 of file CommandParser.cc.


Member Data Documentation

vector<string> mcs::CommandParser::args [private]

vector of arguments.

Definition at line 5223 of file mcs.hh.

string mcs::CommandParser::lcmd [private]

First token, that is the command.

Definition at line 5208 of file mcs.hh.

Data* mcs::CommandParser::md_args[100] [private]

Array of arguments stored as Data objects.

Definition at line 5232 of file mcs.hh.

Data* mcs::CommandParser::md_optargs[100] [private]

Array of arguments to options stored as Data objects.

Definition at line 5235 of file mcs.hh.

vector<string> mcs::CommandParser::optargs [private]

vector of argument to options.

Definition at line 5229 of file mcs.hh.

vector<string> mcs::CommandParser::opts [private]

vector of options.

Definition at line 5226 of file mcs.hh.

string mcs::CommandParser::origcmdline [private]

Command line parsed.

Definition at line 5205 of file mcs.hh.

vector<string> mcs::CommandParser::rest [private]

vector of "rests".

For ach token there is a "rest", that is the rest of the command line starting from the token. This string is called the "rest" of the token.

Definition at line 5220 of file mcs.hh.

vector<string> mcs::CommandParser::tokens [private]

vector of tokens.

Definition at line 5211 of file mcs.hh.


The documentation for this class was generated from the following files:
mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012