1
/* -*- Mode: C++; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
*******************************************************************************
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
* Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
8
* All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
10
* THIS IS UNPUBLISHED SOURCE CODE OF create3000.
12
* The copyright notice above does not evidence any actual of intended
13
* publication of such source code, and is an unpublished work by create3000.
14
* This material contains CONFIDENTIAL INFORMATION that is the property of
17
* No permission is granted to copy, distribute, or create derivative works from
18
* the contents of this software, in whole or in part, without the prior written
19
* permission of create3000.
21
* NON-MILITARY USE ONLY
23
* All create3000 software are effectively free software with a non-military use
24
* restriction. It is free. Well commented source is provided. You may reuse the
25
* source in any way you please with the exception anything that uses it must be
26
* marked to indicate is contains 'non-military use only' components.
28
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
30
* Copyright 1999, 2016 Holger Seelig <holger.seelig@yahoo.de>.
32
* This file is part of the Titania Project.
34
* Titania is free software: you can redistribute it and/or modify it under the
35
* terms of the GNU General Public License version 3 only, as published by the
36
* Free Software Foundation.
38
* Titania is distributed in the hope that it will be useful, but WITHOUT ANY
39
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
40
* A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
41
* details (a copy is included in the LICENSE file that accompanied this code).
43
* You should have received a copy of the GNU General Public License version 3
44
* along with Titania. If not, see <http://www.gnu.org/licenses/gpl.html> for a
45
* copy of the GPLv3 License.
47
* For Silvio, Joy and Adi.
49
******************************************************************************/
51
#ifndef __ANYOPTION_H__
52
#define __ANYOPTION_H__
64
#define COMMAND_FLAG 5
67
#define COMMAND_OPTION_TYPE 1
68
#define COMMAND_FLAG_TYPE 2
69
#define FILE_OPTION_TYPE 3
70
#define FILE_FLAG_TYPE 4
71
#define UNKNOWN_TYPE 5
73
#define DEFAULT_MAXOPTS 10
74
#define MAX_LONG_PREFIX_LENGTH 2
76
#define DEFAULT_MAXUSAGE 3
77
#define DEFAULT_MAXHELP 10
79
#define TRUE_FLAG "true"
88
/* the public interface */
91
AnyOption (int maxoptions);
92
AnyOption (int maxoptions, int maxcharoptions);
96
* following set methods specifies the
97
* special characters and delimiters
98
* if not set traditional defaults will be used
102
setCommandPrefixChar (char _prefix); /* '-' in "-w" */
105
setCommandLongPrefix (char* _prefix); /* '--' in "--width" */
108
setFileCommentChar (char _comment); /* '#' in shellscripts */
111
setFileDelimiterChar (char _delimiter); /* ':' in "width : 100" */
114
* provide the input for the options
115
* like argv[ ] for commndline and the
116
* option file name to use;
120
useCommandArgs (int _argc, char** _argv);
123
useFiileName (const char* _filename);
126
* turn off the POSIX style options
127
* this means anything starting with a '-' or "--"
128
* will be considered a valid option
129
* which alo means you cannot add a bunch of
130
* POIX options chars together like "-lr" for "-l -r"
138
* prints warning verbose if you set anything wrong
144
* there are two types of options
146
* Option - has an associated value ( -w 100 )
147
* Flag - no value, just a boolean flag ( -nogui )
149
* the options can be either a string ( GNU style )
150
* or a character ( traditional POSIX style )
151
* or both ( --width, -w )
153
* the options can be common to the commandline and
154
* the optionfile, or can belong only to either of
155
* commandline and optionfile
157
* following set methods, handle all the aboove
161
/* options comman to command line and option file */
163
setOption (const char* opt_string);
166
setOption (char opt_char);
169
setOption (const char* opt_string, char opt_char);
172
setFlag (const char* opt_string);
175
setFlag (char opt_char);
178
setFlag (const char* opt_string, char opt_char);
180
/* options read from commandline only */
182
setCommandOption (const char* opt_string);
185
setCommandOption (char opt_char);
188
setCommandOption (const char* opt_string, char opt_char);
191
setCommandFlag (const char* opt_string);
194
setCommandFlag (char opt_char);
197
setCommandFlag (const char* opt_string, char opt_char);
199
/* options read from an option file only */
201
setFileOption (const char* opt_string);
204
setFileOption (char opt_char);
207
setFileOption (const char* opt_string, char opt_char);
210
setFileFlag (const char* opt_string);
213
setFileFlag (char opt_char);
216
setFileFlag (const char* opt_string, char opt_char);
219
* process the options, registerd using
220
* useCommandArgs() and useFileName();
226
processCommandArgs ();
229
processCommandArgs (int max_args);
235
* process the specified options
238
processCommandArgs (int _argc, char** _argv);
241
processCommandArgs (int _argc, char** _argv, int max_args);
244
processFile (const char* _filename);
247
* get the value of the options
248
* will return NULL if no value is set
251
getValue (const char* _option);
254
getFlag (const char* _option);
257
getValue (char _optchar);
260
getFlag (char _optchar);
272
addUsage (const char* line);
277
/* print auto usage printing for unknown options or flag */
279
autoUsagePrint (bool flag);
282
* get the argument count and arguments sans the options
299
/* the hidden data structure */
301
int argc; /* commandline arg count */
302
char** argv; /* commndline args */
303
const char* filename; /* the option file */
304
char* appname; /* the application name from argv[0] */
306
int* new_argv; /* arguments sans options (index to argv) */
307
int new_argc; /* argument count sans the options */
308
int max_legal_args; /* ignore extra arguments */
310
/* option strings storage + indexing */
311
int max_options; /* maximum number of options */
312
const char** options; /* storage */
313
int* optiontype; /* type - common, command, file */
314
int* optionindex; /* index into value storage */
315
int option_counter; /* counter for added options */
317
/* option chars storage + indexing */
318
int max_char_options; /* maximum number options */
319
char* optionchars; /* storage */
320
int* optchartype; /* type - common, command, file */
321
int* optcharindex; /* index into value storage */
322
int optchar_counter; /* counter for added options */
325
char** values; /* common value storage */
326
int g_value_counter; /* globally updated value index LAME! */
329
const char** usage; /* usage */
330
int max_usage_lines; /* max usage lines reseverd */
331
int usage_lines; /* number of usage lines */
333
bool command_set; /* if argc/argv were provided */
334
bool file_set; /* if a filename was provided */
335
bool mem_allocated; /* if memory allocated in init() */
336
bool posix_style; /* enables to turn off POSIX style options */
337
bool verbose; /* silent|verbose */
338
bool print_usage; /* usage verbose */
339
bool print_help; /* help verbose */
341
char opt_prefix_char; /* '-' in "-w" */
342
char long_opt_prefix [MAX_LONG_PREFIX_LENGTH + 1]; /* '--' in "--width" */
343
char file_delimiter_char; /* ':' in width : 100 */
344
char file_comment_char; /* '#' in "#this is a comment" */
352
bool set; //was static member
353
bool once; //was static member
361
/* the hidden utils */
367
init (int maxopt, int maxcharopt);
378
/* grow storage arrays as required */
383
doubleCharStorage ();
386
doubleUsageStorage ();
389
setValue (const char* option, char* value);
392
setFlagOn (const char* option);
395
setValue (char optchar, char* value);
398
setFlagOn (char optchar);
401
addOption (const char* option, int type);
404
addOption (char optchar, int type);
407
addOptionError (const char* opt);
410
addOptionError (char opt);
413
findFlag (char* value);
416
addUsageError (const char* line);
428
parsePOSIX (char* arg);
431
parseGNU (char* arg);
437
matchOpt (char* opt);
439
/* dot file methods */
444
readFile (const char* fname);
447
consumeFile (char* buffer);
450
processLine (char* theline, int length);
456
valuePairs (char* type, char* value);
459
justValue (char* value);
462
printVerbose (const char* msg);
465
printVerbose (char* msg);
468
printVerbose (char ch);
475
#endif /* ! _ANYOPTION_H */