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
68f660db
Commit
68f660db
authored
Jan 16, 2018
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util/OptionParser: collect remaining arguments
Allow the caller to use a simple "for" loop without checking arguments.
parent
0066f7a8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
10 deletions
+13
-10
CommandLine.cxx
src/CommandLine.cxx
+2
-4
OptionParser.cxx
src/util/OptionParser.cxx
+2
-0
OptionParser.hxx
src/util/OptionParser.hxx
+9
-6
No files found.
src/CommandLine.cxx
View file @
68f660db
...
...
@@ -357,11 +357,9 @@ ParseCommandLine(int argc, char **argv, struct options *options)
// Second pass: find non-option parameters (i.e. config file)
const
char
*
config_file
=
nullptr
;
for
(
int
i
=
1
;
i
<
argc
;
++
i
)
{
if
(
OptionParser
::
IsOption
(
argv
[
i
]))
continue
;
for
(
const
char
*
i
:
parser
.
GetRemaining
())
{
if
(
config_file
==
nullptr
)
{
config_file
=
argv
[
i
]
;
config_file
=
i
;
continue
;
}
...
...
src/util/OptionParser.cxx
View file @
68f660db
...
...
@@ -53,7 +53,9 @@ OptionParser::ParseNext() noexcept
option_raw
=
arg
;
return
true
;
}
option
=
nullptr
;
option_raw
=
nullptr
;
*
remaining_tail
++
=
arg
;
return
false
;
}
src/util/OptionParser.hxx
View file @
68f660db
...
...
@@ -36,12 +36,16 @@ class OptionParser
const
char
*
option_raw
=
nullptr
;
bool
is_long
=
false
;
const
char
**
const
remaining_head
,
**
remaining_tail
;
public
:
/**
* Constructs #OptionParser.
*/
constexpr
OptionParser
(
int
_argc
,
char
*
const
*
_argv
)
noexcept
:
args
(
_argv
+
1
,
_argc
-
1
)
{}
constexpr
OptionParser
(
int
_argc
,
char
**
_argv
)
noexcept
:
args
(
_argv
+
1
,
_argc
-
1
),
remaining_head
(
const_cast
<
const
char
**>
(
_argv
+
1
)),
remaining_tail
(
remaining_head
)
{}
/**
* Checks if there are command line entries to process.
...
...
@@ -81,11 +85,10 @@ public:
bool
ParseNext
()
noexcept
;
/**
*
Checks if specified string is a command line option
.
*
Returns the remaining non-option arguments
.
*/
static
bool
IsOption
(
const
char
*
s
)
noexcept
{
assert
(
s
!=
nullptr
);
return
s
[
0
]
==
'-'
;
ConstBuffer
<
const
char
*>
GetRemaining
()
const
noexcept
{
return
{
remaining_head
,
remaining_tail
};
}
};
...
...
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