MCS
0.3.3-alpha7
|
Parse command lines. More...
#include <mcs.hh>
Public Member Functions | |
string | allargs () |
Returns all arguments (not tokens!) as a string. More... | |
Data & | arg (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... | |
CommandParser & | operator= (const CommandParser &) |
Declared to avoid using of default assignment operator. More... | |
string | opt (int i) |
Return option at i-th position. More... | |
Data & | optarg (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 | |
Data * | md_args [100] |
Array of arguments stored as Data objects. More... | |
Data * | md_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... | |
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:
And a command line:
where "script" is the name of the script. A call to replPars_onFile() will return a vector of strings as follows:
mcs::CommandParser::CommandParser | ( | const CommandParser & | ) |
Declared to avoid using of default copy constructor.
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.
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.
i | Index of the argument to be returned. |
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.
|
staticprivate |
Cleans a string.
Remove tabs, newline, carriage-return, multiple spaces and trailing ";".
Definition at line 46 of file CommandParser.cc.
|
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 | ) |
Check if a certain option has been given.
Definition at line 215 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.
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())
i | Index of the "rest" to be returned. |
ERROR | MSG_INDEX_LT_ZERO. |
ERROR | MSG_INDEX_OUT_RANGE. |
Definition at line 122 of file CommandParser.cc.
CommandParser& mcs::CommandParser::operator= | ( | const CommandParser & | ) |
Declared to avoid using of default assignment operator.
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.
i | Index of the option to be returned. |
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.
i | Index of the option to be returned. |
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 | ( | string | c | ) |
Parse a command line into tokens, arguments and options.
Definition at line 233 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.
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.
str | The string in which perform parameter substitutions. |
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.
fn | File to parse. |
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())
i | Index of the token to be returned. |
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.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha7
|