6 /** Name Value Pairs, aka: NVP
7 * - Given a string - return the associated int.
8 * - Given a number - return the associated string.
11 * Very useful when the number is not a simple index into an array of
12 * known string, or there may be multiple strings (aliases) that mean then same
15 * An NVP Table is terminated with ".name = NULL".
17 * During the 'name2value' operation, if no matching string is found
18 * the pointer to the terminal element (with p->name == NULL) is returned.
22 * const Jim_Nvp yn[] = {
31 * e = Jim_Nvp_name2value(interp, yn, "y", &result);
33 * e = Jim_Nvp_name2value(interp, yn, "n", &result);
35 * e = Jim_Nvp_name2value(interp, yn, "Blah", &result);
39 * During the number2name operation, the first matching value is returned.
47 int Jim_GetNvp (Jim_Interp
*interp
,
49 const Jim_Nvp
*nvp_table
,
50 const Jim_Nvp
**result
);
52 /* Name Value Pairs Operations */
53 Jim_Nvp
*Jim_Nvp_name2value_simple(const Jim_Nvp
*nvp_table
, const char *name
);
54 Jim_Nvp
*Jim_Nvp_name2value_nocase_simple(const Jim_Nvp
*nvp_table
, const char *name
);
55 Jim_Nvp
*Jim_Nvp_value2name_simple(const Jim_Nvp
*nvp_table
, int v
);
57 int Jim_Nvp_name2value(Jim_Interp
*interp
, const Jim_Nvp
*nvp_table
, const char *name
, Jim_Nvp
**result
);
58 int Jim_Nvp_name2value_nocase(Jim_Interp
*interp
, const Jim_Nvp
*nvp_table
, const char *name
, Jim_Nvp
**result
);
59 int Jim_Nvp_value2name(Jim_Interp
*interp
, const Jim_Nvp
*nvp_table
, int value
, Jim_Nvp
**result
);
61 int Jim_Nvp_name2value_obj(Jim_Interp
*interp
, const Jim_Nvp
*nvp_table
, Jim_Obj
*name_obj
, Jim_Nvp
**result
);
62 int Jim_Nvp_name2value_obj_nocase(Jim_Interp
*interp
, const Jim_Nvp
*nvp_table
, Jim_Obj
*name_obj
, Jim_Nvp
**result
);
63 int Jim_Nvp_value2name_obj(Jim_Interp
*interp
, const Jim_Nvp
*nvp_table
, Jim_Obj
*value_obj
, Jim_Nvp
**result
);
65 /** prints a nice 'unknown' parameter error message to the 'result' */
66 void Jim_SetResult_NvpUnknown(Jim_Interp
*interp
,
69 const Jim_Nvp
*nvp_table
);
72 /** Debug: convert argc/argv into a printable string for printf() debug
74 * \param interp - the interpeter
75 * \param argc - arg count
76 * \param argv - the objects
78 * \returns string pointer holding the text.
80 * Note, next call to this function will free the old (last) string.
82 * For example might want do this:
84 * fp = fopen("some.file.log", "a");
85 * fprintf(fp, "PARAMS are: %s\n", Jim_DebugArgvString(interp, argc, argv));
89 const char *Jim_Debug_ArgvString(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
92 /** A TCL -ish GetOpt like code.
94 * Some TCL objects have various "configuration" values.
95 * For example - in Tcl/Tk the "buttons" have many options.
97 * Usefull when dealing with command options.
98 * that may come in any order...
100 * Does not support "-foo = 123" type options.
101 * Only supports tcl type options, like "-foo 123"
104 typedef struct jim_getopt
{
107 Jim_Obj
* const * argv
;
108 int isconfigure
; /* non-zero if configure */
113 * Example (short and incomplete):
115 * Jim_GetOptInfo goi;
117 * Jim_GetOpt_Setup(&goi, interp, argc, argv);
120 * e = Jim_GetOpt_Nvp(&goi, nvp_options, &n);
122 * Jim_GetOpt_NvpUnknown(&goi, nvp_options, 0);
126 * switch (n->value) {
128 * printf("Option ALIVE specified\n");
131 * if (goi.argc < 1) {
132 * .. not enough args error ..
134 * Jim_GetOpt_String(&goi, &cp, NULL);
135 * printf("FIRSTNAME: %s\n", cp);
137 * Jim_GetOpt_Wide(&goi, &w);
138 * printf("AGE: %d\n", (int)(w));
141 * e = Jim_GetOpt_Nvp(&goi, nvp_politics, &n);
143 * Jim_GetOpt_NvpUnknown(&goi, nvp_politics, 1);
155 * \param goi - get opt info to be initialized
156 * \param interp - jim interp
157 * \param argc - argc count.
158 * \param argv - argv (will be copied)
161 * Jim_GetOptInfo goi;
163 * Jim_GetOptSetup(&goi, interp, argc, argv);
167 int Jim_GetOpt_Setup(Jim_GetOptInfo
*goi
,
170 Jim_Obj
* const * argv
);
173 /** Debug - Dump parameters to stderr
174 * \param goi - current parameters
176 void Jim_GetOpt_Debug(Jim_GetOptInfo
*goi
);
180 /** Remove argv[0] from the list.
182 * \param goi - get opt info
183 * \param puthere - where param is put
186 int Jim_GetOpt_Obj(Jim_GetOptInfo
*goi
, Jim_Obj
**puthere
);
188 /** Remove argv[0] as string.
190 * \param goi - get opt info
191 * \param puthere - where param is put
192 * \param len - return its length
194 int Jim_GetOpt_String(Jim_GetOptInfo
*goi
, char **puthere
, int *len
);
196 /** Remove argv[0] as double.
198 * \param goi - get opt info
199 * \param puthere - where param is put.
202 int Jim_GetOpt_Double(Jim_GetOptInfo
*goi
, double *puthere
);
204 /** Remove argv[0] as wide.
206 * \param goi - get opt info
207 * \param puthere - where param is put.
209 int Jim_GetOpt_Wide(Jim_GetOptInfo
*goi
, jim_wide
*puthere
);
211 /** Remove argv[0] as NVP.
213 * \param goi - get opt info
214 * \param lookup - nvp lookup table
215 * \param puthere - where param is put.
218 int Jim_GetOpt_Nvp(Jim_GetOptInfo
*goi
, const Jim_Nvp
*lookup
, Jim_Nvp
**puthere
);
220 /** Create an appropriate error message for an NVP.
222 * \param goi - options info
223 * \param lookup - the NVP table that was used.
224 * \param hadprefix - 0 or 1 if the option had a prefix.
226 * This function will set the "interp->result" to a human readable
227 * error message listing the available options.
229 * This function assumes the previous option argv[-1] is the unknown string.
231 * If this option had some prefix, then pass "hadprefix = 1" else pass "hadprefix = 0"
237 * // Get the next option
238 * e = Jim_GetOpt_Nvp(&goi, cmd_options, &n);
240 * // option was not recognized
241 * // pass 'hadprefix = 0' because there is no prefix
242 * Jim_GetOpt_NvpUnknown(&goi, cmd_options, 0);
246 * switch (n->value) {
248 * // handle: --sex male | female | lots | needmore
249 * e = Jim_GetOpt_Nvp(&goi, &nvp_sex, &n);
251 * Jim_GetOpt_NvpUnknown(&ogi, nvp_sex, 1);
254 * printf("Code: (%d) is %s\n", n->value, n->name);
263 void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo
*goi
, const Jim_Nvp
*lookup
, int hadprefix
);
266 /** Remove argv[0] as Enum
268 * \param goi - get opt info
269 * \param lookup - lookup table.
270 * \param puthere - where param is put.
273 int Jim_GetOpt_Enum(Jim_GetOptInfo
*goi
, const char * const * lookup
, int *puthere
);