MPD  0.20.18
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 
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
Command line option definition.
Definition: OptionDef.hxx:26
bool HasEntries() const
Checks if there are command line entries to process.
bool CheckOption(const OptionDef &opt, const OptionDef &alt_opt)
Checks if current option is a specified option or specified alternative option.
static bool IsOption(const char *s)
Checks if specified string is a command line option.
bool ParseNext()
Parses current command line entry.
char * GetOption()
Gets the last parsed option.
Command line option parser.
OptionParser(int _argc, char **_argv)
Constructs OptionParser.
bool CheckOption(const OptionDef &opt)
Checks if current option is a specified option.