30 MCS_DEBUG_SETUP(0,
"CommandParser");
42 MCS_DEBUG_LEAVE(NOARGS);
49 c =
subst(c,
"\t+|\n+|\r+",
" ");
57 MCS_DEBUG_ENTER(NOARGS);
60 if (md_args[i])
delete md_args[i];
61 if (md_optargs[i])
delete md_optargs[i];
70 MCS_DEBUG_LEAVE(NOARGS);
89 for (i=0; i<args.size(); i++) {
104 return tokens.size();
115 if (ui >= tokens.size())
116 throw MCS_ERROR(MSG_INDEX_OUT_RANGE, ui, tokens.size());
129 if (ui >= rest.size())
130 throw MCS_ERROR(MSG_INDEX_OUT_RANGE, ui, rest.size());
143 if (ui >= args.size())
144 throw MCS_ERROR(MSG_INDEX_OUT_RANGE, ui, args.size());
152 return arg(i).sval();
163 if (ui >= opts.size())
164 throw MCS_ERROR(MSG_INDEX_OUT_RANGE, ui, opts.size());
183 if (ui >= opts.size())
184 throw MCS_ERROR(MSG_INDEX_OUT_RANGE, ui, opts.size());
186 return *md_optargs[ui];
193 if (cmd.length() != lcmd.length())
196 return (! strncasecmp(cmd.c_str(),
207 for (ui=0; ui< opts.size(); ui++)
208 if (opts[ui] == opt) {
218 return givenOpt(opt, i);
227 for (i=0; i<argc; i++)
228 cmd += argv[i] +
string(
" ");
243 origcmdline = clean(c);
250 if ((! escape) && (*p ==
'\\'))
253 if ((! escape) && (*p ==
'"'))
255 else if ((! quoted) && (*p ==
' ')) {
260 rest.push_back(
string(p));
278 if (args.size() >= 1) {
280 args.erase(args.begin());
283 pcrecpp::RE reopt(
"^-(\\w+)");
284 pcrecpp::RE reoptwarg(
"^-(\\w+)=(.+)");
286 while (ui < args.size()) {
288 if (reopt.FullMatch(args[ui], &opt) ||
289 reoptwarg.FullMatch(args[ui], &opt, &s) ) {
291 optargs.push_back(s);
292 md_optargs[opts.size()-1] =
new Data(s);
293 args.erase(args.begin() + ui);
296 md_args[ui] =
new Data(args[ui]);
325 if (!(fin.is_open()))
326 throw MCS_ERROR(MSG_CANT_OPEN_FILE, fn.csz);
329 while(getline(fin, s)) {
332 if (! line.empty()) line +=
" " ;
334 if (s[s.length()-1] ==
';') {
335 line = line.substr(0, line.length()-1);
336 v.push_back(
trim(line));
358 s =
subst(s,
"#.*$" ,
"" );
359 s =
subst(s,
"\t+" ,
" ");
360 s =
subst(s,
";\\s*$",
";");
363 for (i=0; i<args.size(); i++) {
364 sprintf(expr,
"\\$%d", i);
365 r = md_args[i]->sval();
string opt(int i)
Return option at i-th position.
int optc()
Return number of options.
string cline()
Returns the entire command line.
string sarg(int i)
Return argument at i-th position as a string, see arg(int).
string allargs()
Returns all arguments (not tokens!) as a string.
#define MCS_CMD_MAX_TOKENS
Max number of tokens for a command line.
void parseCmd(string c)
Parse a command line into tokens, arguments and options.
vector< string > replPars_onFile(string fn)
Parse a MCS script.
Data & optarg(int i)
Return argument to option at i-th position.
#define MCS_SUBST_TRAILING
To be used with subst(), substitute only if "what" is at the end. See subst().
CommandParser()
Constructor.
string line_afterToken(int i)
Return the "rest" relative to token at i-th position.
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
~CommandParser()
Destructor, this destroy all Data objects containing arguments.
int argc()
Return number of arguments.
static string clean(string c)
Cleans a string.
bool givenOpt(string opt)
Check if a certain option has been given.
Main include file for all MCS based applications.
bool cmpCmd(string cmd)
Check if the string in the parameter is the same as the command (case insensitive).
A general purpose data type.
string subst(string s, string what, string with, int op=0)
Perform substitutions on a string.
string trim(string s)
Remove any leading or trailing blanks.
string cmd()
Returns the first token, that is the command.
string token(int i)
Return token at i-th position.
Data & arg(int i)
Return argument at i-th position.
string replPars(string str)
Perform parameter substitutions.
int tokenc()
Return number of tokens.
#define MCS_SUBST_QUOTE_WITH
To be used with subst(), doubles each backslash in "with". See subst().
Namespace for MCS library.
void clearArgs()
Destroy all Data objects containing arguments.