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
735a3712
Commit
735a3712
authored
Jan 17, 2018
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util/OptionParser: pass array of OptionDefs to constructor
parent
5ab086e3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
103 deletions
+88
-103
CommandLine.cxx
src/CommandLine.cxx
+50
-47
OptionParser.cxx
src/util/OptionParser.cxx
+24
-26
OptionParser.hxx
src/util/OptionParser.hxx
+14
-30
No files found.
src/CommandLine.cxx
View file @
735a3712
...
...
@@ -76,24 +76,29 @@
#define USER_CONFIG_FILE_LOCATION_XDG PATH_LITERAL("mpd/mpd.conf")
#endif
static
constexpr
OptionDef
opt_kill
(
"kill"
,
"kill the currently running mpd session"
);
static
constexpr
OptionDef
opt_no_config
(
"no-config"
,
"don't read from config"
);
static
constexpr
OptionDef
opt_no_daemon
(
"no-daemon"
,
"don't detach from console"
);
static
constexpr
OptionDef
opt_stdout
(
"stdout"
,
nullptr
);
// hidden, compatibility with old versions
static
constexpr
OptionDef
opt_stderr
(
"stderr"
,
"print messages to stderr"
);
static
constexpr
OptionDef
opt_verbose
(
"verbose"
,
'v'
,
"verbose logging"
);
static
constexpr
OptionDef
opt_version
(
"version"
,
'V'
,
"print version number"
);
static
constexpr
OptionDef
opt_help
(
"help"
,
'h'
,
"show help options"
);
static
constexpr
OptionDef
opt_help_alt
(
nullptr
,
'?'
,
nullptr
);
// hidden, standard alias for --help
enum
Option
{
OPTION_KILL
,
OPTION_NO_CONFIG
,
OPTION_NO_DAEMON
,
OPTION_STDOUT
,
OPTION_STDERR
,
OPTION_VERBOSE
,
OPTION_VERSION
,
OPTION_HELP
,
OPTION_HELP2
,
};
static
constexpr
OptionDef
option_defs
[]
=
{
{
"kill"
,
"kill the currently running mpd session"
},
{
"no-config"
,
"don't read from config"
},
{
"no-daemon"
,
"don't detach from console"
},
{
"stdout"
,
nullptr
},
// hidden, compatibility with old versions
{
"stderr"
,
"print messages to stderr"
},
{
"verbose"
,
'v'
,
"verbose logging"
},
{
"version"
,
'V'
,
"print version number"
},
{
"help"
,
'h'
,
"show help options"
},
{
nullptr
,
'?'
,
nullptr
},
// hidden, standard alias for --help
};
static
constexpr
Domain
cmdline_domain
(
"cmdline"
);
...
...
@@ -265,13 +270,8 @@ static void help(void)
"
\n
"
"Options:
\n
"
);
PrintOption
(
opt_help
);
PrintOption
(
opt_kill
);
PrintOption
(
opt_no_config
);
PrintOption
(
opt_no_daemon
);
PrintOption
(
opt_stderr
);
PrintOption
(
opt_verbose
);
PrintOption
(
opt_version
);
for
(
const
auto
&
i
:
option_defs
)
PrintOption
(
i
);
exit
(
EXIT_SUCCESS
);
}
...
...
@@ -312,35 +312,38 @@ ParseCommandLine(int argc, char **argv, struct options *options)
options
->
verbose
=
false
;
// First pass: handle command line options
OptionParser
parser
(
argc
,
argv
);
while
(
parser
.
ParseNext
())
{
if
(
parser
.
CheckOption
(
opt_kill
))
{
OptionParser
parser
(
option_defs
,
argc
,
argv
);
int
option_index
;
while
((
option_index
=
parser
.
Next
())
>=
0
)
{
switch
(
Option
(
option_index
))
{
case
OPTION_KILL
:
options
->
kill
=
true
;
continue
;
}
if
(
parser
.
CheckOption
(
opt_no_config
))
{
break
;
case
OPTION_NO_CONFIG
:
use_config_file
=
false
;
continue
;
}
if
(
parser
.
CheckOption
(
opt_no_daemon
))
{
break
;
case
OPTION_NO_DAEMON
:
options
->
daemon
=
false
;
continue
;
}
if
(
parser
.
CheckOption
(
opt_stderr
,
opt_stdout
))
{
break
;
case
OPTION_STDOUT
:
case
OPTION_STDERR
:
options
->
log_stderr
=
true
;
continue
;
}
if
(
parser
.
CheckOption
(
opt_verbose
))
{
break
;
case
OPTION_VERBOSE
:
options
->
verbose
=
true
;
continue
;
}
if
(
parser
.
CheckOption
(
opt_version
))
break
;
case
OPTION_VERSION
:
version
();
if
(
parser
.
CheckOption
(
opt_help
,
opt_help_alt
))
help
();
throw
FormatRuntimeError
(
"invalid option: %s"
,
parser
.
GetOption
());
case
OPTION_HELP
:
case
OPTION_HELP2
:
help
();
}
}
/* initialize the logging library, so the configuration file
...
...
src/util/OptionParser.cxx
View file @
735a3712
/*
* Copyright 2003-201
7
The Music Player Daemon Project
* Copyright 2003-201
8
The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
...
...
@@ -19,43 +19,41 @@
#include "OptionParser.hxx"
#include "OptionDef.hxx"
#include "util/RuntimeError.hxx"
#include <string.h>
bool
OptionParser
::
CheckOption
(
const
OptionDef
&
opt
)
const
noexcep
t
inline
unsigned
OptionParser
::
IdentifyOption
(
const
char
*
s
)
cons
t
{
assert
(
option
!=
nullptr
);
if
(
is_long
)
return
opt
.
HasLongOption
()
&&
strcmp
(
option
,
opt
.
GetLongOption
())
==
0
;
assert
(
s
!=
nullptr
);
assert
(
*
s
==
'-'
);
if
(
s
[
1
]
==
'-'
)
{
for
(
const
auto
&
i
:
options
)
if
(
i
.
HasLongOption
()
&&
strcmp
(
s
+
2
,
i
.
GetLongOption
())
==
0
)
return
&
i
-
options
.
data
;
}
else
if
(
s
[
1
]
!=
0
&&
s
[
2
]
==
0
)
{
const
char
ch
=
s
[
1
];
for
(
const
auto
&
i
:
options
)
if
(
i
.
HasShortOption
()
&&
ch
==
i
.
GetShortOption
())
return
&
i
-
options
.
data
;
}
return
opt
.
HasShortOption
()
&&
option
[
0
]
==
opt
.
GetShortOption
()
&&
option
[
1
]
==
'\0'
;
throw
FormatRuntimeError
(
"Unknown option: %s"
,
s
);
}
bool
OptionParser
::
ParseNext
()
noexcept
int
OptionParser
::
Next
()
{
while
(
!
args
.
empty
())
{
const
char
*
arg
=
args
.
shift
();
if
(
arg
[
0
]
==
'-'
)
{
if
(
arg
[
1
]
==
'-'
)
{
option
=
arg
+
2
;
is_long
=
true
;
}
else
{
option
=
arg
+
1
;
is_long
=
false
;
}
option_raw
=
arg
;
return
true
;
}
if
(
arg
[
0
]
==
'-'
)
return
IdentifyOption
(
arg
);
*
remaining_tail
++
=
arg
;
}
return
false
;
return
-
1
;
}
src/util/OptionParser.hxx
View file @
735a3712
/*
* Copyright 2003-201
7
The Music Player Daemon Project
* Copyright 2003-201
8
The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
...
...
@@ -29,10 +29,9 @@ class OptionDef;
*/
class
OptionParser
{
ConstBuffer
<
OptionDef
>
options
;
ConstBuffer
<
const
char
*>
args
;
const
char
*
option
;
const
char
*
option_raw
;
bool
is_long
=
false
;
const
char
**
const
remaining_head
,
**
remaining_tail
;
...
...
@@ -40,41 +39,23 @@ public:
/**
* Constructs #OptionParser.
*/
OptionParser
(
int
_argc
,
char
**
_argv
)
noexcept
:
args
(
_argv
+
1
,
_argc
-
1
),
OptionParser
(
ConstBuffer
<
OptionDef
>
_options
,
int
_argc
,
char
**
_argv
)
noexcept
:
options
(
_options
),
args
(
_argv
+
1
,
_argc
-
1
),
remaining_head
(
const_cast
<
const
char
**>
(
_argv
+
1
)),
remaining_tail
(
remaining_head
)
{}
/**
* Gets the last parsed option.
*/
const
char
*
GetOption
()
noexcept
{
return
option_raw
;
}
/**
* Checks if current option is a specified option.
*/
bool
CheckOption
(
const
OptionDef
&
opt
)
const
noexcept
;
/**
* Checks if current option is a specified option
* or specified alternative option.
*/
bool
CheckOption
(
const
OptionDef
&
opt
,
const
OptionDef
&
alt_opt
)
const
noexcept
{
return
CheckOption
(
opt
)
||
CheckOption
(
alt_opt
);
}
/**
* Parses current command line entry.
* Regardless of result, advances current position to the next
* command line entry.
*
* @return true if an option was found, false if there are no
* more options
* Throws on error.
*
* @return the index if an option was found, -1 if there are
* no more options
*/
bool
ParseNext
()
noexcept
;
int
Next
()
;
/**
* Returns the remaining non-option arguments.
...
...
@@ -82,6 +63,9 @@ public:
ConstBuffer
<
const
char
*>
GetRemaining
()
const
noexcept
{
return
{
remaining_head
,
remaining_tail
};
}
private
:
unsigned
IdentifyOption
(
const
char
*
s
)
const
;
};
#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