2 //***************************************************************************
8 // General representations for a program option.
11 // 08/08/95 - adve - created in the dHPF compiler
12 // 11/26/96 - adve - EvalOpt now returns #args consumed, or -1 for error
13 // 07/15/01 - vadve - Copied to LLVM system and modified
15 //**************************************************************************/
17 #ifndef LLVM_SUPPORT_PROGRAMOPTION_H
18 #define LLVM_SUPPORT_PROGRAMOPTION_H
20 //************************** System Include Files **************************/
24 //*************************** User Include Files ***************************/
26 #include "llvm/Support/Unique.h"
27 #include "llvm/Support/StringUtils.h"
29 //********************** Local Variable Definitions ************************/
31 class ProgramOption: public Unique {
33 /*ctor*/ ProgramOption (const char* _argString,
34 const char* _helpMesg,
35 int _minExpectedArgs = 1)
36 : optionSpecified(false),
37 argString(_argString),
39 minExpectedArgs(_minExpectedArgs) {}
41 /*dtor*/ virtual ~ProgramOption() {}
43 // Pure virtual function for an option with 0 or more arguments.
44 // `optarg' points to the start of the next word in argv[].
45 // It will be NULL if there are no more words.
46 // The return value indicates the number of words of argv[] that
47 // were consumed by EvalOpt and should be discarded.
48 // A return value of -1 indicates an error.
50 virtual int EvalOpt (const char* optarg) = 0;
52 // Returns the value associated with the option as a human-readable
53 // string. The memory returned is allocated via `malloc'.
54 virtual char* GetTextValue () const = 0;
56 // Inline accessor functions for common option information
58 bool OptionSpecified () const { return optionSpecified; }
59 const char* ArgString () const { return argString.c_str(); }
60 const char* HelpMesg () const { return helpMesg.c_str(); }
61 int MinExpectedArgs () const { return minExpectedArgs; }
70 //**************************************************************************/
72 class StringOption : public ProgramOption {
74 /*ctor*/ StringOption (const char* argString,
76 const char* initValue = "",
78 // append = false: EvalOpt will overwrite preexisting value
79 // append = true : EvalOpt will append <optArg> to value
81 /*dtor*/ virtual ~StringOption () {}
83 virtual int EvalOpt (const char* optarg);
85 const char* Value () const { return value.c_str(); }
86 virtual char* GetTextValue () const { return strdup(Value()); }
93 //**************************************************************************/
95 // -<flag_opt> sets the flag to TRUE
96 // -<flag_opt> 0 sets the flag to FALSE
98 // To provide an actual argument (not option) of "0", mark the
99 // end of the options with "--" (see getopt(1)).
101 class FlagOption : public ProgramOption {
103 /*ctor*/ FlagOption (const char* argString,
104 const char* helpMesg,
105 bool initValue = false);
107 /*dtor*/ virtual ~FlagOption () {}
109 virtual int EvalOpt (const char* optarg);
111 bool Value () const { return value; }
112 virtual char* GetTextValue () const { return strdup(
113 value ? "true" : "false");}
118 //**************************************************************************/
120 class RealValuedOption : public ProgramOption {
122 /*ctor*/ RealValuedOption(const char* argString,
123 const char* helpMesg,
124 double initValue = 0.0);
125 /*dtor*/ virtual ~RealValuedOption() {}
127 virtual int EvalOpt (const char* optarg);
129 double Value () const { return value; }
130 virtual char* GetTextValue () const;
136 //**************************************************************************/
138 class IntegerValuedOption : public RealValuedOption {
140 /*ctor*/ IntegerValuedOption(const char* argString,
141 const char* helpMesg,
143 /*ctor*/ virtual ~IntegerValuedOption() {}
146 virtual char* GetTextValue () const;
149 //**************************************************************************/