2 //***************************************************************************
8 // A representation of options for any program.
11 // 08/08/95 - adve - Created in the dHPF compiler
12 // 10/10/96 - mpal, dbaker - converted to const member functions.
13 // 10/19/96 - meven - slightly changed interface to accomodate
14 // arguments other than -X type options
15 // 07/15/01 - vadve - Copied to LLVM system and modified
17 //**************************************************************************/
19 #ifndef LLVM_SUPPORT_PROGRAMOPTIONS_H
20 #define LLVM_SUPPORT_PROGRAMOPTIONS_H
22 #include "llvm/Support/Unique.h"
27 template <> struct hash<string> {
28 size_t operator()(string const &str) const {
29 return hash<char const *>()(str.c_str());
35 //---------------------------------------------------------------------------
37 // Class: ProgramOptions
41 // Class Data Members:
42 // ProgramOptionsRepr* Internal representation of program options,
43 // accessible to derived classes.
45 // Base class for representing the set of options for a program.
47 //---------------------------------------------------------------------------
49 class ProgramOptions: public Unique {
51 /*ctor*/ ProgramOptions (int _argc,
54 /*dtor*/ ~ProgramOptions () {}
56 //--------------------------------------------------------------------
57 // Retrieving different kinds of arguments.
58 // The required argument is specified by the optionString.
59 //--------------------------------------------------------------------
61 string StringOptionValue(const string &optionString) const;
62 bool FlagOptionValue (const string &optionString) const;
63 double RealOptionValue (const string &optionString) const;
64 int IntOptionValue (const string &optionString) const;
66 bool OptionSpecified (const string &optionString) const;
68 //--------------------------------------------------------------------
69 // The name used to invoke this program.
70 //--------------------------------------------------------------------
71 const char* ProgramName () const;
73 //--------------------------------------------------------------------
74 // Access to unparsed arguments
75 //--------------------------------------------------------------------
76 int NumberOfOtherOptions() const;
77 const char* OtherOption(int i) const;
79 //--------------------------------------------------------------------
80 // Access to the original arguments
81 //--------------------------------------------------------------------
82 const char** GetOriginalArgs() const;
83 void PrintArgs(ostream &out) const;
85 //--------------------------------------------------------------------
86 // Derived classes may use PrintOptions in their own PrintUsage() fct
87 // to print information about optional, required, or additional
89 //--------------------------------------------------------------------
90 virtual void PrintOptions (ostream& stream) const;
91 virtual void Usage () const;
93 //--------------------------------------------------------------------
94 // Generate a human-friendly description of the options actually set.
95 // The vector returned contains a multiple of 3 of entries, entry 3n is
96 // the name of the option, entry 3n + 1 contains the description of
97 // the option and entry 3n + 2 contains the ascii value of the option.
98 // All entries are allocated using malloc and can be freed with 'free'.
99 //--------------------------------------------------------------------
100 virtual vector<string> GetDescription () const;
103 //--------------------------------------------------------------------
104 // Called by the subclass to register each possible option
105 // used by the program. Assumes ownership of the ProgramOption.
106 //--------------------------------------------------------------------
107 void Register (ProgramOption* option);
109 //--------------------------------------------------------------------
110 // Parses the options.
111 //--------------------------------------------------------------------
112 void ParseArgs (int argc,
116 inline ProgramOption* OptionHandler(const string &optString) {
117 hash_map<string, ProgramOption*>::iterator hp =
118 optionRegistry.find(optString);
119 return (hp != optionRegistry.end()) ? hp->second : 0;
121 inline const ProgramOption* OptionHandler(const string &optString) const {
122 hash_map<string, ProgramOption*>::const_iterator hp =
123 optionRegistry.find(optString);
124 return (hp != optionRegistry.end()) ? hp->second : 0;
127 //--------------------------------------------------------------------
128 // Functions that must be overridden by the subclass.
129 //--------------------------------------------------------------------
131 virtual void ParseExtraArgs () = 0; // called after successful ParseArgs
133 virtual void PrintUsage (ostream& stream) const = 0;
136 hash_map<string, ProgramOption*> optionRegistry;
143 //**************************************************************************/