MPD: util/OptionParser.hxx Source File

MPD 0.20.15

OptionParser.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_UTIL_OPTIONPARSER_HXX
21 #define MPD_UTIL_OPTIONPARSER_HXX
22
23 #include <assert.h>
24
25 class OptionDef;
26
30 class OptionParser
31 {
32 int argc;
33 char **argv;
34 char *option;
35 char *option_raw;
36 bool is_long;
37 public:
41 OptionParser(int _argc, char **_argv)
42 : argc(_argc - 1), argv(_argv + 1),
43 option(nullptr), option_raw(nullptr), is_long(false) { }
44
48 bool HasEntries() const { return argc > 0; }
49
53 char *GetOption() {
54 assert(option_raw != nullptr);
55 return option_raw;
56 }
57
61 bool CheckOption(const OptionDef& opt);
62
67 bool CheckOption(const OptionDef& opt, const OptionDef &alt_opt) {
68 return CheckOption(opt) || CheckOption(alt_opt);
69 }
70
77 bool ParseNext();
78
82 static bool IsOption(const char *s) {
83 assert(s != nullptr);
84 return s[0] == '-';
85 }
86 };
87
88 #endif
OptionDef
Command line option definition.
Definition: OptionDef.hxx:26
OptionParser::CheckOption
bool CheckOption(const OptionDef &opt, const OptionDef &alt_opt)
Checks if current option is a specified option or specified alternative option.
Definition: OptionParser.hxx:67
OptionParser::IsOption
static bool IsOption(const char *s)
Checks if specified string is a command line option.
Definition: OptionParser.hxx:82
OptionParser::ParseNext
bool ParseNext()
Parses current command line entry.
OptionParser::GetOption
char * GetOption()
Gets the last parsed option.
Definition: OptionParser.hxx:53
OptionParser
Command line option parser.
Definition: OptionParser.hxx:30
OptionParser::OptionParser
OptionParser(int _argc, char **_argv)
Constructs OptionParser.
Definition: OptionParser.hxx:41
OptionParser::CheckOption
bool CheckOption(const OptionDef &opt)
Checks if current option is a specified option.
OptionParser::HasEntries
bool HasEntries() const
Checks if there are command line entries to process.
Definition: OptionParser.hxx:48