wibble
1.1
Main Page
Namespaces
Classes
Files
File List
File Members
wibble
commandline
parser.h
Go to the documentation of this file.
1
#ifndef WIBBLE_COMMANDLINE_PARSER_H
2
#define WIBBLE_COMMANDLINE_PARSER_H
3
4
#include <
wibble/commandline/engine.h
>
5
#include <iosfwd>
6
7
namespace
wibble {
8
namespace
commandline {
9
13
class
Parser
:
public
Engine
14
{
15
protected
:
16
ArgList
m_args
;
17
18
MemoryManager
m_manager
;
19
20
public
:
21
Parser
(
const
std::string&
name
,
22
const
std::string&
usage
= std::string(),
23
const
std::string&
description
= std::string(),
24
const
std::string&
longDescription
= std::string())
25
:
Engine
(&
m_manager
, name,
usage
,
description
,
longDescription
) {}
26
33
bool
parse
(
int
argc,
const
char
* argv[])
34
{
35
m_args
.clear();
36
for
(
int
i = 1; i < argc; i++)
37
m_args
.push_back(argv[i]);
38
parseList
(
m_args
);
39
return
false
;
40
}
41
42
bool
hasNext
()
const
{
return
!
m_args
.empty(); }
43
44
std::string
next
()
45
{
46
if
(
m_args
.empty())
47
return
std::string();
48
std::string res(*
m_args
.begin());
49
m_args
.erase(
m_args
.begin());
50
return
res;
51
}
52
};
53
57
class
StandardParser
:
public
Parser
58
{
59
protected
:
60
std::string
m_version
;
61
62
public
:
63
StandardParser
(
const
std::string& appname,
const
std::string&
version
) :
64
Parser
(appname),
m_version
(version)
65
{
66
helpGroup
=
addGroup
(
"Help options"
);
67
help
=
helpGroup
->
add
<
BoolOption
>(
"help"
,
'h'
,
"help"
,
""
,
68
"print commandline help and exit"
);
69
help
->
addAlias
(
'?'
);
70
this->version =
helpGroup
->
add
<
BoolOption
>(
"version"
, 0,
"version"
,
""
,
71
"print the program version and exit"
);
72
}
73
74
void
outputHelp
(std::ostream& out);
75
76
bool
parse
(
int
argc,
const
char
* argv[]);
77
78
OptionGroup
*
helpGroup
;
79
BoolOption
*
help
;
80
BoolOption
*
version
;
81
};
82
87
class
StandardParserWithManpage
:
public
StandardParser
88
{
89
protected
:
90
int
m_section
;
91
std::string
m_author
;
92
93
public
:
94
StandardParserWithManpage
(
95
const
std::string& appname,
96
const
std::string&
version
,
97
int
section,
98
const
std::string& author) :
99
StandardParser
(appname, version),
100
m_section
(section),
m_author
(author)
101
{
102
manpage
=
helpGroup
->
add
<
StringOption
>(
"manpage"
, 0,
"manpage"
,
"[hooks]"
,
103
"output the "
+
name
() +
" manpage and exit"
);
104
}
105
106
bool
parse
(
int
argc,
const
char
* argv[]);
107
108
StringOption
*
manpage
;
109
};
110
115
class
StandardParserWithMandatoryCommand
:
public
StandardParserWithManpage
116
{
117
public
:
118
StandardParserWithMandatoryCommand
(
119
const
std::string& appname,
120
const
std::string&
version
,
121
int
section,
122
const
std::string& author) :
123
StandardParserWithManpage
(appname, version, section, author)
124
{
125
helpCommand
=
addEngine
(
"help"
,
"[command]"
,
"print help information"
,
126
"With no arguments, print a summary of available commands. "
127
"If given a command name as argument, print detailed informations "
128
"about that command."
);
129
}
130
131
bool
parse
(
int
argc,
const
char
* argv[]);
132
133
Engine
*
helpCommand
;
134
};
135
136
}
137
}
138
139
// vim:set ts=4 sw=4:
140
#endif
Generated on Wed Oct 23 2013 17:14:25 for wibble by
1.8.4