michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2000-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: uoptions.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2000apr17 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * This file provides a command line argument parser. michael@0: */ michael@0: michael@0: #ifndef __UOPTIONS_H__ michael@0: #define __UOPTIONS_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /* This should usually be called before calling u_parseArgs */ michael@0: /*#if U_PLATFORM == U_PF_OS390 && (U_CHARSET_FAMILY == U_ASCII_FAMILY)*/ michael@0: /* translate args from EBCDIC to ASCII */ michael@0: /*# define U_MAIN_INIT_ARGS(argc, argv) __argvtoascii_a(argc, argv)*/ michael@0: /*#elif defined(XP_MAC_CONSOLE)*/ michael@0: #if defined(XP_MAC_CONSOLE) michael@0: # include michael@0: /* Get the arguments from the GUI, since old Macs don't have a console Window. */ michael@0: # define U_MAIN_INIT_ARGS(argc, argv) argc = ccommand((char***)&argv) michael@0: #else michael@0: /* Normally we do nothing. */ michael@0: # define U_MAIN_INIT_ARGS(argc, argv) michael@0: #endif michael@0: michael@0: michael@0: michael@0: /* forward declarations for the function declaration */ michael@0: struct UOption; michael@0: typedef struct UOption UOption; michael@0: michael@0: /* function to be called for a command line option */ michael@0: typedef int UOptionFn(void *context, UOption *option); michael@0: michael@0: /* values of UOption.hasArg */ michael@0: enum { UOPT_NO_ARG, UOPT_REQUIRES_ARG, UOPT_OPTIONAL_ARG }; michael@0: michael@0: /* structure describing a command line option */ michael@0: struct UOption { michael@0: const char *longName; /* "foo" for --foo */ michael@0: const char *value; /* output placeholder, will point to the argument string, if any */ michael@0: UOptionFn *optionFn; /* function to be called when this option occurs */ michael@0: void *context; /* parameter for the function */ michael@0: char shortName; /* 'f' for -f */ michael@0: char hasArg; /* enum value: option takes no/requires/may have argument */ michael@0: char doesOccur; /* boolean for "this one occured" */ michael@0: }; michael@0: michael@0: /* macro for an entry in a declaration of UOption[] */ michael@0: #define UOPTION_DEF(longName, shortName, hasArg) \ michael@0: { longName, NULL, NULL, NULL, shortName, hasArg, 0 } michael@0: michael@0: /* ICU Tools option definitions */ michael@0: #define UOPTION_HELP_H UOPTION_DEF("help", 'h', UOPT_NO_ARG) michael@0: #define UOPTION_HELP_QUESTION_MARK UOPTION_DEF("help", '?', UOPT_NO_ARG) michael@0: #define UOPTION_VERBOSE UOPTION_DEF("verbose", 'v', UOPT_NO_ARG) michael@0: #define UOPTION_QUIET UOPTION_DEF("quiet", 'q', UOPT_NO_ARG) michael@0: #define UOPTION_VERSION UOPTION_DEF("version", 'V', UOPT_NO_ARG) michael@0: #define UOPTION_COPYRIGHT UOPTION_DEF("copyright", 'c', UOPT_NO_ARG) michael@0: michael@0: #define UOPTION_DESTDIR UOPTION_DEF("destdir", 'd', UOPT_REQUIRES_ARG) michael@0: #define UOPTION_SOURCEDIR UOPTION_DEF("sourcedir", 's', UOPT_REQUIRES_ARG) michael@0: #define UOPTION_ENCODING UOPTION_DEF("encoding", 'e', UOPT_REQUIRES_ARG) michael@0: #define UOPTION_ICUDATADIR UOPTION_DEF("icudatadir", 'i', UOPT_REQUIRES_ARG) michael@0: #define UOPTION_WRITE_JAVA UOPTION_DEF("write-java", 'j', UOPT_OPTIONAL_ARG) michael@0: #define UOPTION_PACKAGE_NAME UOPTION_DEF("package-name", 'p', UOPT_REQUIRES_ARG) michael@0: #define UOPTION_BUNDLE_NAME UOPTION_DEF("bundle-name", 'b', UOPT_REQUIRES_ARG) michael@0: michael@0: /** michael@0: * C Command line argument parser. michael@0: * michael@0: * This function takes the argv[argc] command line and a description of michael@0: * the program's options in form of an array of UOption structures. michael@0: * Each UOption defines a long and a short name (a string and a character) michael@0: * for options like "--foo" and "-f". michael@0: * michael@0: * Each option is marked with whether it does not take an argument, michael@0: * requires one, or optionally takes one. The argument may follow in michael@0: * the same argv[] entry for short options, or it may always follow michael@0: * in the next argv[] entry. michael@0: * michael@0: * An argument is in the next argv[] entry for both long and short name michael@0: * options, except it is taken from directly behind the short name in michael@0: * its own argv[] entry if there are characters following the option letter. michael@0: * An argument in its own argv[] entry must not begin with a '-' michael@0: * unless it is only the '-' itself. There is no restriction of the michael@0: * argument format if it is part of the short name options's argv[] entry. michael@0: * michael@0: * The argument is stored in the value field of the corresponding michael@0: * UOption entry, and the doesOccur field is set to 1 if the option michael@0: * is found at all. michael@0: * michael@0: * Short name options without arguments can be collapsed into a single michael@0: * argv[] entry. After an option letter takes an argument, following michael@0: * letters will be taken as its argument. michael@0: * michael@0: * If the same option is found several times, then the last michael@0: * argument value will be stored in the value field. michael@0: * michael@0: * For each option, a function can be called. This could be used michael@0: * for options that occur multiple times and all arguments are to michael@0: * be collected. michael@0: * michael@0: * All options are removed from the argv[] array itself. If the parser michael@0: * is successful, then it returns the number of remaining non-option michael@0: * strings (including argv[0]). michael@0: * argv[0], the program name, is never read or modified. michael@0: * michael@0: * An option "--" ends option processing; everything after this michael@0: * remains in the argv[] array. michael@0: * michael@0: * An option string "-" alone is treated as a non-option. michael@0: * michael@0: * If an option is not recognized or an argument missing, then michael@0: * the parser returns with the negative index of the argv[] entry michael@0: * where the error was detected. michael@0: * michael@0: * The OS/400 compiler requires that argv either be "char* argv[]", michael@0: * or "const char* const argv[]", and it will not accept, michael@0: * "const char* argv[]" as a definition for main(). michael@0: * michael@0: * @param argv This parameter is modified michael@0: * @param options This parameter is modified michael@0: */ michael@0: U_CAPI int U_EXPORT2 michael@0: u_parseArgs(int argc, char* argv[], michael@0: int optionCount, UOption options[]); michael@0: michael@0: #endif