Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
3e2e0d06
Commit
3e2e0d06
authored
Jan 17, 2018
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util/OptionParser: support option values
parent
c45fe351
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
10 deletions
+57
-10
OptionDef.hxx
src/util/OptionDef.hxx
+14
-0
OptionParser.cxx
src/util/OptionParser.cxx
+40
-9
OptionParser.hxx
src/util/OptionParser.hxx
+3
-1
No files found.
src/util/OptionDef.hxx
View file @
3e2e0d06
...
@@ -29,6 +29,7 @@ class OptionDef
...
@@ -29,6 +29,7 @@ class OptionDef
{
{
const
char
*
long_option
;
const
char
*
long_option
;
char
short_option
;
char
short_option
;
bool
has_value
=
false
;
const
char
*
desc
;
const
char
*
desc
;
public
:
public
:
constexpr
OptionDef
(
const
char
*
_long_option
,
const
char
*
_desc
)
constexpr
OptionDef
(
const
char
*
_long_option
,
const
char
*
_desc
)
...
@@ -42,8 +43,21 @@ public:
...
@@ -42,8 +43,21 @@ public:
short_option
(
_short_option
),
short_option
(
_short_option
),
desc
(
_desc
)
{
}
desc
(
_desc
)
{
}
constexpr
OptionDef
(
const
char
*
_long_option
,
char
_short_option
,
bool
_has_value
,
const
char
*
_desc
)
noexcept
:
long_option
(
_long_option
),
short_option
(
_short_option
),
has_value
(
_has_value
),
desc
(
_desc
)
{}
constexpr
bool
HasLongOption
()
const
{
return
long_option
!=
nullptr
;
}
constexpr
bool
HasLongOption
()
const
{
return
long_option
!=
nullptr
;
}
constexpr
bool
HasShortOption
()
const
{
return
short_option
!=
0
;
}
constexpr
bool
HasShortOption
()
const
{
return
short_option
!=
0
;
}
constexpr
bool
HasValue
()
const
noexcept
{
return
has_value
;
}
constexpr
bool
HasDescription
()
const
{
return
desc
!=
nullptr
;
}
constexpr
bool
HasDescription
()
const
{
return
desc
!=
nullptr
;
}
const
char
*
GetLongOption
()
const
{
const
char
*
GetLongOption
()
const
{
...
...
src/util/OptionParser.cxx
View file @
3e2e0d06
...
@@ -20,25 +20,56 @@
...
@@ -20,25 +20,56 @@
#include "OptionParser.hxx"
#include "OptionParser.hxx"
#include "OptionDef.hxx"
#include "OptionDef.hxx"
#include "util/RuntimeError.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringCompare.hxx"
#include <string.h>
#include <string.h>
inline
const
char
*
OptionParser
::
CheckShiftValue
(
const
char
*
s
,
const
OptionDef
&
option
)
{
if
(
!
option
.
HasValue
())
return
nullptr
;
if
(
args
.
empty
())
throw
FormatRuntimeError
(
"Value expected after %s"
,
s
);
return
args
.
shift
();
}
inline
OptionParser
::
Result
inline
OptionParser
::
Result
OptionParser
::
IdentifyOption
(
const
char
*
s
)
const
OptionParser
::
IdentifyOption
(
const
char
*
s
)
{
{
assert
(
s
!=
nullptr
);
assert
(
s
!=
nullptr
);
assert
(
*
s
==
'-'
);
assert
(
*
s
==
'-'
);
if
(
s
[
1
]
==
'-'
)
{
if
(
s
[
1
]
==
'-'
)
{
for
(
const
auto
&
i
:
options
)
for
(
const
auto
&
i
:
options
)
{
if
(
i
.
HasLongOption
()
&&
if
(
!
i
.
HasLongOption
())
strcmp
(
s
+
2
,
i
.
GetLongOption
())
==
0
)
continue
;
return
{
int
(
&
i
-
options
.
data
)};
const
char
*
t
=
StringAfterPrefix
(
s
+
2
,
i
.
GetLongOption
());
if
(
t
==
nullptr
)
continue
;
const
char
*
value
;
if
(
*
t
==
0
)
value
=
CheckShiftValue
(
s
,
i
);
else
if
(
*
t
==
'='
)
value
=
t
+
1
;
else
continue
;
return
{
int
(
&
i
-
options
.
data
),
value
};
}
}
else
if
(
s
[
1
]
!=
0
&&
s
[
2
]
==
0
)
{
}
else
if
(
s
[
1
]
!=
0
&&
s
[
2
]
==
0
)
{
const
char
ch
=
s
[
1
];
const
char
ch
=
s
[
1
];
for
(
const
auto
&
i
:
options
)
for
(
const
auto
&
i
:
options
)
{
if
(
i
.
HasShortOption
()
&&
ch
==
i
.
GetShortOption
())
if
(
i
.
HasShortOption
()
&&
ch
==
i
.
GetShortOption
())
{
return
{
int
(
&
i
-
options
.
data
)};
const
char
*
value
=
CheckShiftValue
(
s
,
i
);
return
{
int
(
&
i
-
options
.
data
),
value
};
}
}
}
}
throw
FormatRuntimeError
(
"Unknown option: %s"
,
s
);
throw
FormatRuntimeError
(
"Unknown option: %s"
,
s
);
...
@@ -55,5 +86,5 @@ OptionParser::Next()
...
@@ -55,5 +86,5 @@ OptionParser::Next()
*
remaining_tail
++
=
arg
;
*
remaining_tail
++
=
arg
;
}
}
return
{
-
1
};
return
{
-
1
,
nullptr
};
}
}
src/util/OptionParser.hxx
View file @
3e2e0d06
...
@@ -47,6 +47,7 @@ public:
...
@@ -47,6 +47,7 @@ public:
struct
Result
{
struct
Result
{
int
index
;
int
index
;
const
char
*
value
;
constexpr
operator
bool
()
noexcept
{
constexpr
operator
bool
()
noexcept
{
return
index
>=
0
;
return
index
>=
0
;
...
@@ -70,7 +71,8 @@ public:
...
@@ -70,7 +71,8 @@ public:
}
}
private
:
private
:
Result
IdentifyOption
(
const
char
*
s
)
const
;
const
char
*
CheckShiftValue
(
const
char
*
s
,
const
OptionDef
&
option
);
Result
IdentifyOption
(
const
char
*
s
);
};
};
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment