1 //-------------------------------------------------------------------------
2 //
3 // This code was taken from Open Scene Graph and incorporated from into
4 // OSSIM.
5 //
6 //-------------------------------------------------------------------------
7 // $Id: ossimArgumentParser.h 22491 2013-11-26 18:17:29Z dburken $
8 #ifndef ossimArgumentParser_HEADER
9 #define ossimArgumentParser_HEADER 1
10 #include <ossim/base/ossimConstants.h>
11 #include <ossim/base/ossimString.h>
12 #include <map>
13 #include <string>
14 #include <iosfwd>
15 
16 class ossimApplicationUsage;
17 
18 class OSSIMDLLEXPORT ossimArgumentParser
19 {
20 public:
21 
22    class ossimParameter
23    {
24    public:
25       enum ossimParameterType
26       {
27          OSSIM_FLOAT_PARAMETER,
28          OSSIM_DOUBLE_PARAMETER,
29          OSSIM_INT_PARAMETER,
30          OSSIM_UNSIGNED_INT_PARAMETER,
31          OSSIM_STRING_PARAMETER,
32       };
33 
34       union ossimValueUnion
35       {
36          float*          theFloat;
37          double*         theDouble;
38          int*            theInt;
39          unsigned int*   theUint;
40          std::string*    theString;
41       };
42 
ossimParameter(float & value)43       ossimParameter(float& value)
44       {
45          theType = OSSIM_FLOAT_PARAMETER; theValue.theFloat = &value;
46       }
47 
ossimParameter(double & value)48       ossimParameter(double& value)
49       {
50          theType = OSSIM_DOUBLE_PARAMETER; theValue.theDouble = &value;
51       }
52 
ossimParameter(int & value)53       ossimParameter(int& value)
54       {
55          theType = OSSIM_INT_PARAMETER; theValue.theInt = &value;
56       }
57 
ossimParameter(unsigned int & value)58       ossimParameter(unsigned int& value)
59       {
60          theType = OSSIM_UNSIGNED_INT_PARAMETER; theValue.theUint = &value;
61       }
62 
ossimParameter(std::string & value)63       ossimParameter(std::string& value)
64       {
65          theType = OSSIM_STRING_PARAMETER; theValue.theString = &value;
66       }
67 
ossimParameter(ossimString & value)68       ossimParameter(ossimString& value)
69       {
70          theType = OSSIM_STRING_PARAMETER; theValue.theString =
71                                               &(value.string());
72       }
73 
74       bool valid(const char* str) const;
75       bool assign(const char* str);
76 
77    protected:
78 
79       ossimParameterType   theType;
80       ossimValueUnion      theValue;
81    };
82 
83    /** return return true if specified string is an option in the form of
84     * -option or --option .
85     */
86    static bool isOption(const char* str);
87 
88    /** return return true if string is any other string apart from an option.*/
89    static bool isString(const char* str);
90 
91    /** return return true if specified parameter is an number.*/
92    static bool isNumber(const char* str);
93 
94 public:
95 
96    ossimArgumentParser(int* argc,char **argv);
97    ossimArgumentParser(const ossimString& commandLine);
98 
99    ~ossimArgumentParser();
100 
101    /** @brief Initialize from command arguments. */
102    void initialize(int* argc, const char **argv);
103 
setApplicationUsage(ossimApplicationUsage * usage)104    void setApplicationUsage(ossimApplicationUsage* usage) { theUsage = usage; }
getApplicationUsage()105    ossimApplicationUsage* getApplicationUsage() { return theUsage; }
getApplicationUsage()106    const ossimApplicationUsage* getApplicationUsage() const { return theUsage; }
107 
108    /** return the argument count.*/
argc()109    int& argc() { return *theArgc; }
110 
111    /** return the argument array.*/
argv()112    char** argv() { return theArgv; }
113 
114    /** return char* argument at specificed position.*/
115    char* operator [] (int pos) { return theArgv[pos]; }
116 
117    /** return const char* argument at specificed position.*/
118    const char* operator [] (int pos) const { return theArgv[pos]; }
119 
120    /** return the application name, as specified by argv[0] */
121    std::string getApplicationName() const;
122 
123    /** return the position of an occurence of a string in the argument list.
124     * return -1 when no string is found.*/
125    int find(const std::string& str) const;
126 
127    /** return return true if specified parameter is an option in the form of -option or --option .*/
128    bool isOption(int pos) const;
129 
130    /** return return true if specified parameter is an string, which can be any other string apart from an option.*/
131    bool isString(int pos) const;
132 
133    /** return return true if specified parameter is an number.*/
134    bool isNumber(int pos) const;
135 
136    bool containsOptions() const;
137 
138    /** remove one or more arguments from the argv argument list, and decrement the argc respectively.*/
139    void remove(int pos,int num=1);
140 
141    /** Inserts string into the argv argument list, and increment the argc respectively.
142     * If string contains spaces, it will be split up into component simple strings. */
143    void insert(int pos, const ossimString& arg);
144 
145    /** return true if specified argument matches string.*/
146    bool match(int pos, const std::string& str) const;
147 
148    /**
149     * search for an occurance of a string in the argument list, on sucess
150     * remove that occurance from the list and return true, otherwise
151     * return false.
152     */
153    bool read(const std::string& str);
154    bool read(const std::string& str, ossimParameter value1);
155    bool read(const std::string& str, ossimParameter value1,
156              ossimParameter value2);
157    bool read(const std::string& str, ossimParameter value1,
158              ossimParameter value2, ossimParameter value3);
159    bool read(const std::string& str, ossimParameter value1,
160              ossimParameter value2, ossimParameter value3,
161              ossimParameter value4);
162    bool read(const std::string& str, ossimParameter value1,
163              ossimParameter value2, ossimParameter value3,
164              ossimParameter value4, ossimParameter value5);
165    bool read(const std::string& str, ossimParameter value1,
166              ossimParameter value2, ossimParameter value3,
167              ossimParameter value4, ossimParameter value5,
168              ossimParameter value6);
169 
170    /**
171     * Alternate form for reading variable length arguments (must be comma-separated), e.g.,
172     *
173     *    --input_files file1, file2, file3,file4 next_arg
174     *
175     * Note that spaces between arguments are optional. The next_arg entry will not be considered
176     * part of the list since there's no comma separator and will be left on the argument array.
177     * @param str The option string (with "-" or "--")
178     * @param param_list Vector to contain results as strings. Always cleared before populating
179     * @return True if option found (param_list may be empty f no args followed).
180     */
181    bool read(const std::string& str, std::vector<ossimString>& param_list);
182 
183    /**
184     * @return The number of parameters of type value associated with specified
185     * option, or -1 if option not found
186     */
187    int numberOfParams(const std::string& str,
188                       const ossimParameter value) const;
189 
190    /**
191     * if the argument value at the position pos matches specified string, and
192     * subsequent paramters are also matched then set the paramter values and
193     * remove the from the list of arguments.
194     */
195    bool read(int pos, const std::string& str);
196    bool read(int pos, const std::string& str, ossimParameter value1);
197    bool read(int pos, const std::string& str, ossimParameter value1,
198              ossimParameter value2);
199    bool read(int pos, const std::string& str, ossimParameter value1,
200              ossimParameter value2, ossimParameter value3);
201    bool read(int pos, const std::string& str, ossimParameter value1,
202              ossimParameter value2, ossimParameter value3,
203              ossimParameter value4);
204 
205 
206    enum ossimErrorSeverity
207    {
208       OSSIM_BENIGN = 0,
209       OSSIM_CRITICAL = 1
210    };
211 
212    typedef std::map<std::string,ossimErrorSeverity> ossimErrorMessageMap;
213 
214    /**
215     * @return The error flag, true if an error has occured when
216     * reading arguments.
217     */
218    bool errors(ossimErrorSeverity severity=OSSIM_BENIGN) const;
219 
220    /** report an error message by adding to the ErrorMessageMap.*/
221    void reportError(const std::string& message,
222                     ossimErrorSeverity severity=OSSIM_CRITICAL);
223 
224    /** for each remaining option report it as an unrecongnized.*/
225    void reportRemainingOptionsAsUnrecognized(
226       ossimErrorSeverity severity=OSSIM_BENIGN);
227 
228    /** @return The error message, if any has occured.*/
229    ossimErrorMessageMap& getErrorMessageMap();
230 
231    /** @return The error message, if any has occured.*/
232    const ossimErrorMessageMap& getErrorMessageMap() const;
233 
234    /** write out error messages at an above specified .*/
235    void writeErrorMessages(std::ostream& output,
236                            ossimErrorSeverity sevrity=OSSIM_BENIGN);
237 
238 
239 protected:
240 
241    int*                     theArgc;
242    char**                   theArgv;
243    ossimErrorMessageMap     theErrorMessageMap;
244    ossimApplicationUsage*   theUsage;
245    bool                     theMemAllocated;
246 
247 };
248 
249 #endif
250