1/* 2 * Command line options parser. 3 * 4 * Copyright (C) 2015, Broadcom Corporation. All Rights Reserved. 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * $Id: miniopt.h 419467 2013-08-21 09:19:48Z $ 18 */ 19 20 21#ifndef MINI_OPT_H 22#define MINI_OPT_H 23 24#ifdef __cplusplus 25extern "C" { 26#endif 27 28/* ---- Include Files ---------------------------------------------------- */ 29/* ---- Constants and Types ---------------------------------------------- */ 30 31#define MINIOPT_MAXKEY 128 /* Max options */ 32typedef struct miniopt { 33 34 /* These are persistent after miniopt_init() */ 35 const char* name; /* name for prompt in error strings */ 36 const char* flags; /* option chars that take no args */ 37 bool longflags; /* long options may be flags */ 38 bool opt_end; /* at end of options (passed a "--") */ 39 40 /* These are per-call to miniopt() */ 41 42 int consumed; /* number of argv entries cosumed in 43 * the most recent call to miniopt() 44 */ 45 bool positional; 46 bool good_int; /* 'val' member is the result of a sucessful 47 * strtol conversion of the option value 48 */ 49 char opt; 50 char key[MINIOPT_MAXKEY]; 51 char* valstr; /* positional param, or value for the option, 52 * or null if the option had 53 * no accompanying value 54 */ 55 uint uval; /* strtol translation of valstr */ 56 int val; /* strtol translation of valstr */ 57} miniopt_t; 58 59void miniopt_init(miniopt_t *t, const char* name, const char* flags, bool longflags); 60int miniopt(miniopt_t *t, char **argv); 61 62 63/* ---- Variable Externs ------------------------------------------------- */ 64/* ---- Function Prototypes ---------------------------------------------- */ 65 66 67#ifdef __cplusplus 68 } 69#endif 70 71#endif /* MINI_OPT_H */ 72