MCS  0.3.3-alpha7
mcs::CommandParser Class Reference

Parse command lines. More...

#include <mcs.hh>

Public Member Functions

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

Private Member Functions

void clearArgs ()
 Destroy all Data objects containing arguments. More...
 

Static Private Member Functions

static string clean (string c)
 Cleans a string. More...
 

Private Attributes

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

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 5185 of file mcs.hh.

Constructor & Destructor Documentation

◆ CommandParser() [1/2]

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.

◆ CommandParser() [2/2]

mcs::CommandParser::CommandParser ( )

Constructor.

Definition at line 28 of file CommandParser.cc.

◆ ~CommandParser()

mcs::CommandParser::~CommandParser ( )

Destructor, this destroy all Data objects containing arguments.

Definition at line 39 of file CommandParser.cc.

Member Function Documentation

◆ allargs()

string mcs::CommandParser::allargs ( )

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

Definition at line 85 of file CommandParser.cc.

◆ arg()

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
iIndex of the argument to be returned.
Returns
A reference to a STRING Data object containing the argument at i-th position.
Exceptions
ERRORMSG_INDEX_LT_ZERO.
ERRORMSG_INDEX_OUT_RANGE.

Definition at line 136 of file CommandParser.cc.

◆ argc()

int mcs::CommandParser::argc ( )

Return number of arguments.

Definition at line 96 of file CommandParser.cc.

◆ clean()

string mcs::CommandParser::clean ( string  c)
staticprivate

Cleans a string.

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

Definition at line 46 of file CommandParser.cc.

◆ clearArgs()

void mcs::CommandParser::clearArgs ( )
private

Destroy all Data objects containing arguments.

Definition at line 55 of file CommandParser.cc.

◆ cline()

string mcs::CommandParser::cline ( )

Returns the entire command line.

Definition at line 74 of file CommandParser.cc.

◆ cmd()

string mcs::CommandParser::cmd ( )

Returns the first token, that is the command.

Definition at line 80 of file CommandParser.cc.

◆ cmpCmd()

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.

◆ givenOpt() [1/2]

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

Check if a certain option has been given.

Definition at line 215 of file CommandParser.cc.

◆ givenOpt() [2/2]

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.

◆ line_afterToken()

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
iIndex of the "rest" to be returned.
Returns
A string containing the "rest" at i-th position.
Exceptions
ERRORMSG_INDEX_LT_ZERO.
ERRORMSG_INDEX_OUT_RANGE.
See also
rest.

Definition at line 122 of file CommandParser.cc.

◆ operator=()

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.

◆ opt()

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
iIndex of the option to be returned.
Returns
A string containing the option at i-th position, without the minus ("-") character.
Exceptions
ERRORMSG_INDEX_LT_ZERO.
ERRORMSG_INDEX_OUT_RANGE.

Definition at line 156 of file CommandParser.cc.

◆ optarg()

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
iIndex of the option to be returned.
Returns
A string containing the argument to the option at i-th position, without the equal ("=") character.
Exceptions
ERRORMSG_INDEX_LT_ZERO.
ERRORMSG_INDEX_OUT_RANGE.

Definition at line 176 of file CommandParser.cc.

◆ optc()

int mcs::CommandParser::optc ( )

Return number of options.

Definition at line 170 of file CommandParser.cc.

◆ parseCmd() [1/2]

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

Parse a command line into tokens, arguments and options.

Definition at line 233 of file CommandParser.cc.

◆ parseCmd() [2/2]

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.

◆ replPars()

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
strThe string in which perform parameter substitutions.
Returns
The final string.

Definition at line 349 of file CommandParser.cc.

◆ replPars_onFile()

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
fnFile to parse.
Returns
vector of string containing instructions.
Exceptions
ERRORMSG_CANT_OPEN_FILE.

Definition at line 318 of file CommandParser.cc.

◆ sarg()

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.

◆ token()

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

Return token at i-th position.

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

Parameters
iIndex of the token to be returned.
Returns
A string containing the token at i-th position.
Exceptions
ERRORMSG_INDEX_LT_ZERO.
ERRORMSG_INDEX_OUT_RANGE.

Definition at line 108 of file CommandParser.cc.

◆ tokenc()

int mcs::CommandParser::tokenc ( )

Return number of tokens.

Definition at line 102 of file CommandParser.cc.

Member Data Documentation

◆ args

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

vector of arguments.

Definition at line 5208 of file mcs.hh.

◆ lcmd

string mcs::CommandParser::lcmd
private

First token, that is the command.

Definition at line 5193 of file mcs.hh.

◆ md_args

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

Array of arguments stored as Data objects.

Definition at line 5217 of file mcs.hh.

◆ md_optargs

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

Array of arguments to options stored as Data objects.

Definition at line 5220 of file mcs.hh.

◆ optargs

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

vector of argument to options.

Definition at line 5214 of file mcs.hh.

◆ opts

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

vector of options.

Definition at line 5211 of file mcs.hh.

◆ origcmdline

string mcs::CommandParser::origcmdline
private

Command line parsed.

Definition at line 5190 of file mcs.hh.

◆ rest

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 5205 of file mcs.hh.

◆ tokens

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

vector of tokens.

Definition at line 5196 of file mcs.hh.


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

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018