Changeset 18187 in vbox for trunk/include/iprt
- Timestamp:
- Mar 24, 2009 2:37:27 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/getopt.h
r18102 r18187 237 237 * 238 238 * @code 239 * int main(int argc, char *argv[]) 240 * { 241 * static const RTGETOPTDEF s_aOptions[] = 242 * { 243 * { "--optwithstring", 's', RTGETOPT_REQ_STRING }, 244 * { "--optwithint", 'i', RTGETOPT_REQ_INT32 }, 245 * { "--verbose", 'v', 0 }, 246 * }; 247 * 248 * int ch; 249 * int i = 1; 250 * RTGETOPTUNION ValueUnion; 251 * RTGETOPTSTATE GetState; 252 * RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0); 253 * while ((ch = RTGetOpt(&GetState, &ValueUnion))) 254 * { 255 * // for options that require an argument, ValueUnion has received the value 256 * switch (ch) 257 * { 258 * case 's': // --optwithstring or -s 259 * // string argument, copy ValueUnion.psz 260 * break; 261 * 262 * case 'i': // --optwithint or -i 263 * // integer argument, copy ValueUnion.i32 264 * break; 265 * 266 * case 'v': // --verbose or -v 267 * g_fOptVerbose = true; 268 * break; 269 * 270 * case VINF_GETOPT_NOT_OPTION: 271 * // handle non-option argument in ValueUnion.psz. 272 * break; 273 * 274 * default: 275 * if (ch > 0) 276 * { 277 * if (RT_C_IS_GRAPH(ch) 278 * Error("unhandled option: -%c\n", ch); 279 * else 280 * Error("unhandled option: %i\n", ch); 281 * } 282 * else if (ch == VERR_GETOPT_UNKNOWN_OPTION) 283 * Error("unknown option: %s", psz); 284 * else if (ValueUnion.pDef) 285 * Error("%s: %Rrs", ValueUnion.pDef->pszLong, ch); 286 * else 287 * Error("%Rrs", ch); 288 * return 1; 289 * } 290 * } 291 * 292 * return 0; 293 * } 294 * @endcode 239 int main(int argc, char **argv) 240 { 241 RTR3Init(); 242 243 static const RTGETOPTDEF s_aOptions[] = 244 { 245 { "--optwithstring", 's', RTGETOPT_REQ_STRING }, 246 { "--optwithint", 'i', RTGETOPT_REQ_INT32 }, 247 { "--verbose", 'v', 0 }, 248 }; 249 250 int ch; 251 int i = 1; 252 RTGETOPTUNION ValueUnion; 253 RTGETOPTSTATE GetState; 254 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0); 255 while ((ch = RTGetOpt(&GetState, &ValueUnion))) 256 { 257 // for options that require an argument, ValueUnion has received the value 258 switch (ch) 259 { 260 case 's': // --optwithstring or -s 261 // string argument, copy ValueUnion.psz 262 break; 263 264 case 'i': // --optwithint or -i 265 // integer argument, copy ValueUnion.i32 266 break; 267 268 case 'v': // --verbose or -v 269 g_fOptVerbose = true; 270 break; 271 272 case VINF_GETOPT_NOT_OPTION: 273 // handle non-option argument in ValueUnion.psz. 274 break; 275 276 default: 277 if (ch > 0) 278 { 279 if (RT_C_IS_GRAPH(ch)) 280 Error("unhandled option: -%c\n", ch); 281 else 282 Error("unhandled option: %i\n", ch); 283 } 284 else if (ch == VERR_GETOPT_UNKNOWN_OPTION) 285 Error("unknown option: %s\n", ValueUnion.psz); 286 else if (ValueUnion.pDef) 287 Error("%s: %Rrs\n", ValueUnion.pDef->pszLong, ch); 288 else 289 Error("%Rrs\n", ch); 290 return 1; 291 } 292 } 293 294 return 0; 295 } 296 @endcode 295 297 * 296 298 * @returns 0 when done parsing.
Note:
See TracChangeset
for help on using the changeset viewer.