summaryrefslogtreecommitdiff
path: root/cpp/include/IceUtil/Options.h
blob: 3bb3d18c06360649e15d8db044949e24cee7d314 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef ICE_UTIL_OPTIONS_H
#define ICE_UTIL_OPTIONS_H

#include <IceUtil/Config.h>
#include <IceUtil/RecMutex.h>
#include <string>
#include <vector>
#include <map>
#include <set>

namespace IceUtil
{

class ICE_UTIL_API Options
{
public:

    struct Error
    {
        Error(const ::std::string& r) : reason(r) {}
        ::std::string reason;
    };

    struct APIError : public Error
    {
        APIError(const ::std::string& r) : Error(r) {}
    };

    struct BadOpt : public Error
    {
        BadOpt(const ::std::string& r) : Error(r) {}
    };

    struct BadQuote : public Error
    {
        BadQuote(const ::std::string& r) : Error(r) {}
    };

    enum LengthType { ShortOpt, LongOpt };
    enum RepeatType { Repeat, NoRepeat };
    enum ArgType { NeedArg, NoArg };

    Options();
    void addOpt(const ::std::string&, const ::std::string& = "",
                ArgType = NoArg, ::std::string = "", RepeatType = NoRepeat);

    static ::std::vector< ::std::string> split(const ::std::string&);
    ::std::vector< ::std::string> parse(const ::std::vector< ::std::string>&);
    ::std::vector< ::std::string> parse(int, const char* const []);
    bool isSet(const ::std::string&) const;
    ::std::string optArg(const ::std::string&) const;
    ::std::vector< ::std::string> argVec(const ::std::string&) const;

private:

    struct OptionDetails
    {
	LengthType length;
	ArgType arg;
        RepeatType repeat;
    };
    typedef ::std::map< ::std::string, OptionDetails> ValidOpts; // Valid options and their details.
    typedef ::std::map< ::std::string, ::std::string> Opts; // Value of non-repeating options.
    typedef ::std::map< ::std::string, ::std::vector< ::std::string> > ROpts; // Value of repeating options.

    void addValidOpt(const ::std::string&, LengthType, ArgType, const ::std::string&, RepeatType);
    ValidOpts::iterator checkOpt(const ::std::string&, LengthType);
    void setOpt(const ::std::string&, const ::std::string&, RepeatType);
    ValidOpts::const_iterator checkOptIsValid(const ::std::string&) const;
    ValidOpts::const_iterator checkOptHasArg(const ::std::string&) const;

    ValidOpts _validOpts;
    Opts _opts;
    ROpts _ropts;

    bool parseCalled;

    RecMutex _m;

    Options(const Options&); // Not allowed.
    void operator=(const Options&); // Not allowed.

    static void checkArgs(const ::std::string&, const ::std::string&, bool, const ::std::string&);
};

}

#endif