File: GetOpts.h

package info (click to toggle)
snapper 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,576 kB
  • ctags: 2,049
  • sloc: cpp: 15,605; sh: 11,424; ansic: 1,401; makefile: 287; python: 159
file content (44 lines) | stat: -rw-r--r-- 922 bytes parent folder | download | duplicates (3)
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

#include <getopt.h>
#include <string>
#include <map>
#include <list>


class GetOpts
{
public:

    static const struct option no_options[1];

    typedef std::map<std::string, std::string> parsed_opts;

    void init(int argc, char** argv);

    // longopts.flag must be NULL
    parsed_opts parse(const struct option* longopts);
    parsed_opts parse(const char* command, const struct option* longopts);

    bool hasArgs() const { return argc - optind > 0; }

    int numArgs() const { return argc - optind; }

    const char* popArg() { return argv[optind++]; }

    std::list<std::string> getArgs() const {
	return std::list<std::string>(&argv[optind], &argv[argc]);
    }

private:

    int argc;
    char** argv;

    std::string make_optstring(const struct option* longopts) const;

    typedef std::map<int, const char*> short2long_t;

    short2long_t make_short2long(const struct option* longopts) const;

};