1 /* -*- related-file-name: "../../liblcdf/clp.c" -*- */ 2 #ifndef LCDF_CLP_H 3 #define LCDF_CLP_H 4 #ifdef __cplusplus 5 extern "C" { 6 #endif 7 8 /* clp.h - Public interface to CLP. 9 * This file is part of CLP, the command line parser package. 10 * 11 * Copyright (c) 1997-2005 Eddie Kohler, kohler@icir.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a 14 * copy of this software and associated documentation files (the "Software"), 15 * to deal in the Software without restriction, subject to the conditions 16 * listed in the Click LICENSE file, which is available in full at 17 * http://www.pdos.lcs.mit.edu/click/license.html. The conditions include: you 18 * must preserve this copyright notice, and you cannot mention the copyright 19 * holders in advertising related to the Software without their permission. 20 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This 21 * notice is a summary of the Click LICENSE file; the license in that file is 22 * legally binding. */ 23 24 25 /* Argument types */ 26 #define Clp_NoArg 0 27 #define Clp_ArgString 1 28 #define Clp_ArgStringNotOption 2 29 #define Clp_ArgBool 3 30 #define Clp_ArgInt 4 31 #define Clp_ArgUnsigned 5 32 #define Clp_ArgDouble 6 33 34 #define Clp_FirstUserType 10 35 36 /* Flags for individual Clp_Options */ 37 #define Clp_Mandatory (1<<0) /* Has mandatory argument */ 38 #define Clp_Optional (1<<1) /* Has optional argument */ 39 #define Clp_Negate (1<<2) /* Allow --no-OPT */ 40 #define Clp_OnlyNegated (1<<3) /* Allow --no-OPT, but not --OPT */ 41 #define Clp_PreferredMatch (1<<4) /* Prefer --OPT to --OPTwhatever */ 42 /* when matching option prefixes */ 43 44 struct Clp_Option { 45 const char *long_name; /* e.g. "version" */ 46 int short_name; /* e.g. 'v' */ 47 int option_id; /* number returned by Clp_Next */ 48 int arg_type; /* e.g. Clp_ArgBool */ 49 int flags; /* e.g. Clp_Optional | Clp_Negate */ 50 }; 51 52 53 /* Sizes of clp->val */ 54 #define Clp_ValSize 40 55 #define Clp_ValIntSize 10 56 57 typedef struct Clp_Option Clp_Option; 58 typedef struct Clp_Parser Clp_Parser; 59 typedef struct Clp_Internal Clp_Internal; 60 typedef struct Clp_ParserState Clp_ParserState; 61 typedef struct Clp_Argv Clp_Argv; 62 63 typedef int (*Clp_ArgParseFunc)(Clp_Parser *, const char *, int, void *); 64 typedef void (*Clp_ErrorHandler)(const char *); 65 66 struct Clp_Parser { 67 68 int negated; 69 70 int have_arg; 71 const char *arg; 72 73 union { 74 int i; 75 unsigned u; 76 double d; 77 const char *s; 78 void *pv; 79 #ifdef HAVE_INT64_TYPES 80 int64_t i64; 81 uint64_t u64; 82 #endif 83 char cs[Clp_ValSize]; 84 unsigned char ucs[Clp_ValSize]; 85 int is[Clp_ValIntSize]; 86 unsigned us[Clp_ValIntSize]; 87 } val; 88 89 Clp_Internal *internal; 90 91 }; 92 93 94 Clp_Parser * Clp_NewParser(int argc, const char * const *argv, 95 int nopt, Clp_Option *opt); 96 void Clp_DeleteParser(Clp_Parser *); 97 98 Clp_ErrorHandler Clp_SetErrorHandler(Clp_Parser *, Clp_ErrorHandler); 99 100 const char * Clp_ProgramName(Clp_Parser *); 101 void Clp_SetProgramName(Clp_Parser *, const char *); 102 103 104 struct Clp_Argv { 105 int argc; 106 char **argv; 107 char *argv_buf; 108 }; 109 110 Clp_Argv * Clp_NewArgv(const char *args, int len); 111 void Clp_DeleteArgv(Clp_Argv *); 112 113 114 /* Option types for Clp_SetOptionChar */ 115 /* Clp_NotOption 0 */ 116 #define Clp_Short (1<<0) 117 #define Clp_Long (1<<1) 118 #define Clp_ShortNegated (1<<2) 119 #define Clp_LongNegated (1<<3) 120 #define Clp_LongImplicit (1<<4) 121 122 int Clp_SetOptionChar(Clp_Parser *, int c, int option_type); 123 124 125 /* Argument type flags for Clp_AddType */ 126 #define Clp_DisallowOptions (1<<0) /* No option-looking args */ 127 128 int Clp_AddType 129 (Clp_Parser *, int type_id, int flags, 130 Clp_ArgParseFunc func, void *user_data); 131 132 133 /* Flags for Clp_AddStringListType */ 134 #define Clp_AllowNumbers (1<<0) /* Number args OK */ 135 136 int Clp_AddStringListType 137 (Clp_Parser *, int type_id, int flags, ...); 138 int Clp_AddStringListTypeVec 139 (Clp_Parser *, int type_id, int flags, 140 int n, char **str, int *val); 141 142 143 /* Return values from Clp_Next */ 144 #define Clp_NotOption 0 145 #define Clp_Done -1 146 #define Clp_BadOption -2 147 #define Clp_Error -3 148 149 int Clp_Next(Clp_Parser *); 150 151 152 const char * Clp_Shift(Clp_Parser *, int allow_dashes); 153 int Clp_SetOptionProcessing(Clp_Parser *, int option_processing); 154 155 Clp_ParserState *Clp_NewParserState(void); 156 void Clp_DeleteParserState(Clp_ParserState *); 157 void Clp_SaveParser(Clp_Parser *, Clp_ParserState *); 158 void Clp_RestoreParser(Clp_Parser *, Clp_ParserState *); 159 160 int Clp_OptionError(Clp_Parser *, const char *, ...); 161 int Clp_CurOptionNameBuf(Clp_Parser *, char *buf, int buflen); 162 const char * Clp_CurOptionName(Clp_Parser *); /* uses static memory */ 163 164 #ifdef __cplusplus 165 } 166 #endif 167 #endif 168