File: CmdLineParams.h

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (116 lines) | stat: -rw-r--r-- 2,676 bytes parent folder | download | duplicates (2)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef _CMD_LINE_PARAMS_H
#define _CMD_LINE_PARAMS_H


#include <vector>
#include <string>
#include <exception>
#include <boost/program_options.hpp>

/**
 * @brief Command-line parser
 * Serves the same purpose as the getopt library.
 */
class CmdLineParams
{
public:
	typedef std::runtime_error unrecognized_option;

public:
	/**
	 * The base constructor sets up the default help and
	 * version options that should be available on all platforms.
	 */
	CmdLineParams(int argc, char* argv[]);
	~CmdLineParams();

	/**
	 *
	 */
	void SetUsageDescription(std::string usagedesc) {
		usage_desc = usagedesc;
	}

	/**
	 * Print out usage information, along with a list of command-line
	 * options that have been set up for this command-line parser.
	 */
	void PrintUsage() const;

	/**
	 * @return The used cmdline to start the program.
	 */
	std::string GetCmdLine() const;

	/**
	 * @return the script or demofile given on the command-line,
	 *   or "" if none was given.
	 */
	std::string GetInputFile() const;

	/**
	 * @brief add options
	 * @param shortopt the short (single character) to use (0 for none)
	 * @param longopt the long (full string) to use (required)
	 * @param desc a short, human-readable description of this parameter
	 */
	void AddSwitch(const char shortopt, std::string longopt, std::string desc);
	void AddString(const char shortopt, std::string longopt, std::string desc);
	void AddInt(const char shortopt, std::string longopt, std::string desc);

	/**
	 * @brief Iterates through and processes arguments
	 *
	 * This will read the parameters and search for recognized strings.
	 */
	void Parse();

	/**
	 * @brief check if commandline flag was set
	 * @param var the longopt-name of the config flag
	 */
	bool IsSet(const std::string& var) const;

	/**
	 * @brief Commandline argument as string
	 * @param var the longopt-name of the config flag
	 */
	std::string GetString(const std::string& var) const;

	/**
	 * @brief Commandline argument as int
	 * @param var the longopt-name of the config flag
	 */
	int GetInt(const std::string& var) const;

protected:
	/**
	 * @brief argument count
	 *
	 * Stores the argument count specified at initialization
	 */
	int argc;

	/**
	 * @brief arguments
	 *
	 * Stores the C string array given at initialization
	 */
	char** argv;

	/**
	 * @brief usage_desc
	 *
	 * Stores the C string array given at initialization
	 */
	std::string usage_desc;

	boost::program_options::variables_map vm;
	boost::program_options::options_description desc;
	boost::program_options::options_description all;
};

#endif // _CMD_LINE_PARAMS_H