File: argmap.hpp

package info (click to toggle)
libopendbx 1.4.6-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,960 kB
  • ctags: 1,006
  • sloc: sh: 10,899; ansic: 7,283; xml: 1,837; cpp: 1,696; makefile: 362; sed: 16
file content (85 lines) | stat: -rw-r--r-- 1,820 bytes parent folder | download | duplicates (7)
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
/*
 *  Parameter parsing from file, command line and environment
 *  (c) 2006-2008, Norbert Sendetzky <norbert@linuxnetworks.de>
 *  Inspired by PowerDNS ArgvMap code
 *
 *  Distributed under the terms of the GNU Library General Public Licence
 *  version 2 or (at your option) any later version.
 */



#include <map>
#include <string>
#include <vector>
#include <stdexcept>



#ifndef ARGMAP_HPP
#define ARGMAP_HPP



using std::map;
using std::string;
using std::vector;



class ArgException : public std::runtime_error
{
public:
	explicit ArgException( const string& str ) : std::runtime_error( str ) {}
};



class ArgMap
{
protected:

	struct argopt {
		string longopt;
		string shortopt;
		string value;
		string help;
		bool req;
	};

	vector<string> m_items;
	vector<struct argopt> m_arg;
	map<string,size_t> m_long;
	map<string,size_t> m_short;

	int getOptionType( const string& arg );
	void parseItem( const string& item, bool strict );
	void parseLongOption( const string& keyvalue, bool strict );
	void parseShortOption( const string& arg, const string& value, bool strict );

public:

	enum opttype { Delimiter, Item, Short, Long };

	string& set( const string& longopt, const string& shortopt, const string& help, bool req = true );

	bool mustDo( const string &arg );
	long asLong( const string& arg );
	double asDouble( const string& arg );
	const string& asString( const string& arg );

	vector<string> getArgList();
	const vector<string>& getItems();

	bool checkArgv( int argc, char* argv[], const string& str, string& val );
	void parseArgv( int argc, char* argv[], bool strict = true );
	void parseFile( const string& filename, bool strict = true );
	void parseEnv( char* env[], bool strict = false );

	string help( const string& prefix = "" );
	string config();
};



#endif /* ARGUMENTS_HH */