-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGetOpt.h
91 lines (83 loc) · 2.46 KB
/
GetOpt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* @file GetOpt.h
*
* Command line parser
*
* @author igor@
*
* Copyright (c) 2005-2017, Parallels International GmbH
* Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
*
* This file is part of OpenVZ. OpenVZ is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
* Schaffhausen, Switzerland.
*/
#ifndef __GETOPT_H__
#define __GETOPT_H__
#include <list>
#include <string>
typedef std::list<std::string> stringList;
#define GETOPTERROR '?'
#define GETOPTUNKNOWN -2
#define OPTION_END {NULL, '\0', OptNoArg , -1}
enum OptionArg {OptNoArg, OptRequireArg};
enum OptionType {OptLong, OptShort, OptUnknown};
struct Option {
const char *long_name;
char short_name;
OptionArg type;
int id;
};
class GetOptLong
{
private:
const Option *m_options;
int m_argc;
char **m_argv;
int m_cur_id;
char *m_cur_short_arg;
char *m_parsed_arg;
public:
GetOptLong(int argc, char *argv[], const Option *options,
int offset = 1);
/*
If an option was successfully found, then returns the Option::id.
If all command-line options have been parsed, then returns -1.
If encounters an option character that was not in Option,
the '?'’is returned. If encounters an option with a missing argument.
then the return value depends on the Option::type:
if it is ’OptRequireArg, then '?' is returned; otherwise
Option::id’is returned.
*/
int parse(std::string &arg);
const char *get_next()
{
if (m_cur_id >= m_argc)
return NULL;
return m_argv[m_cur_id++];
}
char **get_args()
{
return &m_argv[m_cur_id];
}
void hide_arg();
private:
OptionType getOptType(const std::string &opt) const;
const Option &findOption(const std::string &opt, OptionType type);
const Option &findOption();
};
#endif // __GETOPT_H__