release
[tango-nonfree.git] / utils / tango_admin / anyoption.h
blob2a5e0af8fd329f3bbcf3a37f2ba07fd20463d81c
1 //
2 // anyoption.h - include file for command line options management
3 //
4 //
5 // Copyright (C) : 2004,2005,2006,2007,2008,2009,2010
6 // European Synchrotron Radiation Facility
7 // BP 220, Grenoble 38043
8 // FRANCE
9 //
10 // This file is part of Tango.
12 // Tango is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // Tango is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with Tango. If not, see <http://www.gnu.org/licenses/>.
25 #ifndef _ANYOPTION_H
26 #define _ANYOPTION_H
28 #include <iostream>
29 #include <fstream>
30 #include <stdlib.h>
31 #include <string.h>
33 #define COMMON_OPT 1
34 #define COMMAND_OPT 2
35 #define FILE_OPT 3
36 #define COMMON_FLAG 4
37 #define COMMAND_FLAG 5
38 #define FILE_FLAG 6
40 #define COMMAND_OPTION_TYPE 1
41 #define COMMAND_FLAG_TYPE 2
42 #define FILE_OPTION_TYPE 3
43 #define FILE_FLAG_TYPE 4
44 #define UNKNOWN_TYPE 5
46 #define DEFAULT_MAXOPTS 10
47 #define MAX_LONG_PREFIX_LENGTH 3
49 #define DEFAULT_MAXUSAGE 3
50 #define DEFAULT_MAXHELP 10
52 #define TRUE_FLAG "true"
54 using namespace std;
56 class AnyOption
59 public: /* the public interface */
60 AnyOption();
61 AnyOption(int maxoptions );
62 AnyOption(int maxoptions , int maxcharoptions);
63 ~AnyOption();
65 /*
66 * following set methods specifies the
67 * special characters and delimiters
68 * if not set traditional defaults will be used
71 void setCommandPrefixChar( char _prefix ); /* '-' in "-w" */
72 void setCommandLongPrefix( char *_prefix ); /* '--' in "--width" */
73 void setFileCommentChar( char _comment ); /* '#' in shellscripts */
74 void setFileDelimiterChar( char _delimiter );/* ':' in "width : 100" */
76 /*
77 * provide the input for the options
78 * like argv[] for commndline and the
79 * option file name to use;
82 void useCommandArgs( int _argc, char **_argv );
83 void useFiileName( const char *_filename );
85 /*
86 * turn off the POSIX style options
87 * this means anything starting with a '-' or "--"
88 * will be considered a valid option
89 * which alo means you cannot add a bunch of
90 * POIX options chars together like "-lr" for "-l -r"
94 void noPOSIX();
97 * prints warning verbose if you set anything wrong
99 void setVerbose();
103 * there are two types of options
105 * Option - has an associated value ( -w 100 )
106 * Flag - no value, just a boolean flag ( -nogui )
108 * the options can be either a string ( GNU style )
109 * or a character ( traditional POSIX style )
110 * or both ( --width, -w )
112 * the options can be common to the commandline and
113 * the optionfile, or can belong only to either of
114 * commandline and optionfile
116 * following set methods, handle all the aboove
117 * cases of options.
120 /* options comman to command line and option file */
121 void setOption( const char *opt_string );
122 void setOption( char opt_char );
123 void setOption( const char *opt_string , char opt_char );
124 void setFlag( const char *opt_string );
125 void setFlag( char opt_char );
126 void setFlag( const char *opt_string , char opt_char );
128 /* options read from commandline only */
129 void setCommandOption( const char *opt_string );
130 void setCommandOption( char opt_char );
131 void setCommandOption( const char *opt_string , char opt_char );
132 void setCommandFlag( const char *opt_string );
133 void setCommandFlag( char opt_char );
134 void setCommandFlag( const char *opt_string , char opt_char );
136 /* options read from an option file only */
137 void setFileOption( const char *opt_string );
138 void setFileOption( char opt_char );
139 void setFileOption( const char *opt_string , char opt_char );
140 void setFileFlag( const char *opt_string );
141 void setFileFlag( char opt_char );
142 void setFileFlag( const char *opt_string , char opt_char );
145 * process the options, registerd using
146 * useCommandArgs() and useFileName();
148 void processOptions();
149 void processCommandArgs();
150 void processCommandArgs( int max_args );
151 bool processFile();
154 * process the specified options
156 void processCommandArgs( int _argc, char **_argv );
157 void processCommandArgs( int _argc, char **_argv, int max_args );
158 bool processFile( const char *_filename );
161 * get the value of the options
162 * will return NULL if no value is set
164 char *getValue( const char *_option );
165 bool getFlag( const char *_option );
166 char *getValue( char _optchar );
167 bool getFlag( char _optchar );
170 * Print Usage
172 void printUsage();
173 void printAutoUsage();
174 void addUsage( const char *line );
175 void printHelp();
176 /* print auto usage printing for unknown options or flag */
177 void autoUsagePrint(bool flag);
180 * get the argument count and arguments sans the options
182 int getArgc();
183 char* getArgv( int index );
184 bool hasOptions();
186 private: /* the hidden data structure */
187 int argc; /* commandline arg count */
188 char **argv; /* commndline args */
189 const char* filename; /* the option file */
190 char* appname; /* the application name from argv[0] */
192 int *new_argv; /* arguments sans options (index to argv) */
193 int new_argc; /* argument count sans the options */
194 int max_legal_args; /* ignore extra arguments */
197 /* option strings storage + indexing */
198 int max_options; /* maximum number of options */
199 const char **options; /* storage */
200 int *optiontype; /* type - common, command, file */
201 int *optionindex; /* index into value storage */
202 int option_counter; /* counter for added options */
204 /* option chars storage + indexing */
205 int max_char_options; /* maximum number options */
206 char *optionchars; /* storage */
207 int *optchartype; /* type - common, command, file */
208 int *optcharindex; /* index into value storage */
209 int optchar_counter; /* counter for added options */
211 /* values */
212 char **values; /* common value storage */
213 int g_value_counter; /* globally updated value index LAME! */
215 /* help and usage */
216 const char **usage; /* usage */
217 int max_usage_lines; /* max usage lines reseverd */
218 int usage_lines; /* number of usage lines */
220 bool command_set; /* if argc/argv were provided */
221 bool file_set; /* if a filename was provided */
222 bool mem_allocated; /* if memory allocated in init() */
223 bool posix_style; /* enables to turn off POSIX style options */
224 bool verbose; /* silent|verbose */
225 bool print_usage; /* usage verbose */
226 bool print_help; /* help verbose */
228 char opt_prefix_char; /* '-' in "-w" */
229 char long_opt_prefix[MAX_LONG_PREFIX_LENGTH]; /* '--' in "--width" */
230 char file_delimiter_char; /* ':' in width : 100 */
231 char file_comment_char; /* '#' in "#this is a comment" */
232 char equalsign;
233 char comment;
234 char delimiter;
235 char endofline;
236 char whitespace;
237 char nullterminate;
239 bool set; //was static member
240 bool once; //was static member
242 bool hasoptions;
243 bool autousage;
245 private: /* the hidden utils */
246 void init();
247 void init(int maxopt, int maxcharopt );
248 bool alloc();
249 void cleanup();
250 bool valueStoreOK();
252 /* grow storage arrays as required */
253 bool doubleOptStorage();
254 bool doubleCharStorage();
255 bool doubleUsageStorage();
257 bool setValue( const char *option , char *value );
258 bool setFlagOn( const char *option );
259 bool setValue( char optchar , char *value);
260 bool setFlagOn( char optchar );
262 void addOption( const char* option , int type );
263 void addOption( char optchar , int type );
264 void addOptionError( const char *opt);
265 void addOptionError( char opt);
266 bool findFlag( char* value );
267 void addUsageError( const char *line );
268 bool CommandSet();
269 bool FileSet();
270 bool POSIX();
272 char parsePOSIX( char* arg );
273 int parseGNU( char *arg );
274 bool matchChar( char c );
275 int matchOpt( char *opt );
277 /* dot file methods */
278 char *readFile();
279 char *readFile( const char* fname );
280 bool consumeFile( char *buffer );
281 void processLine( char *theline, int length );
282 char *chomp( char *str );
283 void valuePairs( char *type, char *value );
284 void justValue( char *value );
286 void printVerbose( const char *msg );
287 void printVerbose( char *msg );
288 void printVerbose( char ch );
289 void printVerbose( );
294 #endif /* ! _ANYOPTION_H */