]> git.mdlowis.com Git - projs/opts.git/commitdiff
Added new convenience function for checking an options value
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 6 Oct 2014 19:02:07 +0000 (15:02 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 6 Oct 2014 19:02:07 +0000 (15:02 -0400)
Gemfile.lock
source/opts.c
source/opts.h
tests/test_opts.c

index d8548a32c125274283fc0bfe8319add907c579f8..1539f10063745135e7253a3de0176d3643107336 100755 (executable)
@@ -1,4 +1,5 @@
 GEM
+  remote: https://rubygems.org/
   specs:
     json (1.8.1)
     rake (10.3.2)
index de760862f560b420ef6fd6a8a311d178e9094568..26e9a638c1f223d32b9ef761027b93f9c621e69c 100755 (executable)
@@ -22,7 +22,7 @@ typedef struct {
     unsigned int arg_count;
     char** arg_vect;
     int current;
-    OptionConfig_T* options;
+    opts_cfg_t* options;
 } StreamContext_T;
 
 static entry_t* Options   = NULL;
@@ -34,7 +34,7 @@ static char* opts_parse_optarg(StreamContext_T* ctx, char* opt_name);
 static void opts_parse_argument( StreamContext_T* ctx );
 static void opts_parse_error(const char* msg, char* opt_name);
 
-static OptionConfig_T* opts_get_option_config( OptionConfig_T* opts, OptionType_T typ, char* name );
+static opts_cfg_t* opts_get_option_config( opts_cfg_t* opts, OptionType_T typ, char* name );
 static char* opts_next_token( StreamContext_T* ctx );
 static void opts_consume_ws( StreamContext_T* ctx );
 static char opts_next_char( StreamContext_T* ctx );
@@ -44,7 +44,7 @@ static char* strclone(const char* p_old);
 static void opts_add_option(char* name, char* tag, char* arg);
 static void opts_add_argument(char* arg);
 
-void opts_parse( OptionConfig_T* opts, int argc, char** argv ) {
+void opts_parse( opts_cfg_t* opts, int argc, char** argv ) {
     /* Setup the stream */
     StreamContext_T ctx;
     ctx.line_idx  = 0;
@@ -100,7 +100,7 @@ void opts_reset(void) {
 static void opts_parse_short_option( StreamContext_T* ctx ) {
     char opt[2] = { ctx->current, '\0' };
     char* opt_name = strclone(opt);
-    OptionConfig_T* config = opts_get_option_config( ctx->options, SHORT, opt_name );
+    opts_cfg_t* config = opts_get_option_config( ctx->options, SHORT, opt_name );
     if (config != NULL) {
         char* opt_arg = NULL;
         (void)opts_next_char( ctx );
@@ -116,7 +116,7 @@ static void opts_parse_short_option( StreamContext_T* ctx ) {
 
 static void opts_parse_long_option( StreamContext_T* ctx ) {
     char* opt_name = opts_next_token( ctx );
-    OptionConfig_T* config = opts_get_option_config( ctx->options, LONG, opt_name );
+    opts_cfg_t* config = opts_get_option_config( ctx->options, LONG, opt_name );
     if (config != NULL) {
         char* opt_arg = NULL;
         if (config->has_arg)
@@ -146,8 +146,8 @@ static void opts_parse_argument( StreamContext_T* ctx ) {
         opts_add_argument(arg_val);
 }
 
-static OptionConfig_T* opts_get_option_config( OptionConfig_T* opts, OptionType_T type, char* name ) {
-    OptionConfig_T* cfg = NULL;
+static opts_cfg_t* opts_get_option_config( opts_cfg_t* opts, OptionType_T type, char* name ) {
+    opts_cfg_t* cfg = NULL;
     int i = 0;
     while( opts[i].name != NULL ) {
         OptionType_T curr_type = (strlen(opts[i].name) > 1) ? LONG : SHORT;
@@ -270,6 +270,10 @@ const char* opts_get_value(const char* name, const char* tag) {
     return (NULL == p_opt) ? NULL : p_opt->value;
 }
 
+bool opts_equal(const char* name, const char* tag, const char* value) {
+    return (0 == strcmp(value, opts_get_value(name,tag)));
+}
+
 const char** opts_select(const char* name, const char* tag) {
     size_t index = 0;
     const char** ret = (const char**)malloc(sizeof(const char*));
index 18022a647eed71a7d6d580a8cf1616d20482bd90..7e51bdc3f6928a1f5fa503407e05d3e23010e62d 100755 (executable)
@@ -8,19 +8,21 @@ extern "C" {
 #include <stdbool.h>
 #include <stddef.h>
 
-typedef struct OptionConfig_T {
+typedef struct {
     char* name;
     bool has_arg;
     char* tag;
     char* desc;
-} OptionConfig_T;
+} opts_cfg_t;
 
-void opts_parse( OptionConfig_T* opts, int argc, char** argv );
+void opts_parse(opts_cfg_t* opts, int argc, char** argv);
 
 void opts_reset(void);
 
 bool opts_is_set(const char* name, const char* tag);
 
+bool opts_equal(const char* name, const char* tag, const char* value);
+
 const char* opts_get_value(const char* name, const char* tag);
 
 const char** opts_select(const char* name, const char* tag);
index e9a3b19c2f76ca10c1674cc5f124dc6a6203cbe2..5feceeda37e58b2e0d20ff7005168354f6ea2716 100755 (executable)
@@ -12,7 +12,7 @@
 //-----------------------------------------------------------------------------
 // Sample Option Configuration
 //-----------------------------------------------------------------------------
-OptionConfig_T Options_Config[] = {
+opts_cfg_t Options_Config[] = {
     { "a",   false, "test_a", "A simple test option" },
     { "b",   true,  "test_b", "A simple test option" },
     { "c",   false, "test_c", "A simple test option" },