diff options
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/food.c | 55 | ||||
-rw-r--r-- | src/foodopts.c | 33 | ||||
-rw-r--r-- | src/foodopts.h | 19 | ||||
-rw-r--r-- | src/util.c | 2 |
5 files changed, 79 insertions, 32 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 032e134..e3645a9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,6 +4,8 @@ common_sources = eval.c \ eval.h \ lib.c \ lib.h \ + foodopts.h \ + foodopts.c \ parser.c \ parser.h \ search.c \ @@ -1,8 +1,9 @@ -#include <getopt.h> +//#include <getopt.h> #include "util.h" #include "parser.h" #include "search.h" +#include "foodopts.h" #include "eval.h" #include "lib.h" @@ -66,33 +67,28 @@ main(int argc, char * argv[]) int c; + struct foodoption long_options[] = + {/* name, has_arg, flag, val, help, arg */ + {"help", no_argument, 0, 'h', "Print this help", 0}, + {"no-eval", no_argument, 0, 'n', "Don't evaluate recipes", 0}, + {"to-json", no_argument, 0, 'j', "Format recipe to json", 0}, + {"to-html", no_argument, 0, 'w', "Format recipe to html", 0}, + {"to-rcp", no_argument, 0, 'r', "Format recipe to rcp", 0}, + {"add-item", required_argument, 0, 'a', "Add item to matched recipes", "ITEM"}, + //{"add-step", required_argument, 0, 'o'},, 0 + {"format", required_argument, 0, 'f', "Select recipe formatting", "{json,html,rcp}"}, + {"include", required_argument, 0, 'I', "Path to recipe library, can be passed many times", "PATH"}, + {"search", required_argument, 0, 's', "Return recipes matching QUERY", "QUERY"}, + {"title", required_argument, 0, 't', "Return recipes matching TITLE", "TITLE"}, + {"strict", required_argument, 0, 'S', "Return recipes matching QUERY, exaclty", "QUERY"}, + {"hash", required_argument, 0, 'H', "Return recipes by HASH", "HASH"}, + {"list-ingredients", no_argument, 0, 'l', "List ingredients for matched recipes", 0}, + {0, 0, 0, 0, 0} + }; while (1) { - static struct option long_options[] = - { - /* <span class="roman">These options set a flag.</span> */ - // {"verbose", no_argument, &verbose_flag, 1}, - // {"brief", no_argument, &verbose_flag, 0}, - {"help", no_argument, 0, 'h'}, - {"no-eval", no_argument, 0, 'n'}, - {"to-json", no_argument, 0, 'j'}, - {"to-html", no_argument, 0, 'w'}, - {"to-rcp", no_argument, 0, 'r'}, - {"add-item", required_argument, 0, 'a'}, - //{"add-step", required_argument, 0, 'o'}, - {"format", required_argument, 0, 'f'}, - {"include", required_argument, 0, 'I'}, - {"search", required_argument, 0, 's'}, - {"title", required_argument, 0, 't'}, - {"strict", required_argument, 0, 'S'}, - {"hash", required_argument, 0, 'H'}, - {"list-ingredients", no_argument, 0, 'l'}, - // {"to-rcp", required_argument, 0, 'r'}, - {0, 0, 0, 0} - }; - int option_index = 0; - c = getopt_long (argc, argv, "jnlhrwf:s:S:t:H:I:a:", + c = get_foodopt (argc, argv, "jnlhrwf:s:S:t:H:I:a:", long_options, &option_index); if (c == -1) @@ -100,12 +96,7 @@ main(int argc, char * argv[]) switch (c) { case 0: - if (long_options[option_index].flag != 0) - break; - printf ("option %s", long_options[option_index].name); - if (optarg) - printf (" with arg %s", optarg); - printf ("\n"); + printf("Flag setting\n"); break; case 'f': if (!strcmp(optarg, "json")) @@ -168,7 +159,7 @@ main(int argc, char * argv[]) } if (opt.help) { - print_help(argv[0]); + foodopt_help(argv[0], long_options); return 0; } diff --git a/src/foodopts.c b/src/foodopts.c index e69de29..7a4cbbd 100644 --- a/src/foodopts.c +++ b/src/foodopts.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#include "foodopts.h" + +int +get_foodopt(int argc, char *const argv[], + const char *optstring, + const struct foodoption *longopts, + int *longindex) +{ + return getopt_long(argc, argv, optstring, + (struct option *)longopts, longindex); +} + +void +foodopt_help(char * argv0, + const struct foodoption *longopts) +{ + fprintf(stderr, "%s [OPTION ...] FILE ...\n", argv0); + fprintf(stderr, "\nOPTIONS:\n"); + + + int i = 0; + while ((longopts[i].name) + && (longopts[i].val)) { + fprintf(stderr, "-%c, --%s%s%s: %s\n", + longopts[i].val, + longopts[i].name, + longopts[i].has_arg == required_argument ? " " : "", + longopts[i].has_arg == required_argument ? longopts[i].arg : "", + longopts[i].help); + i++; + } +} diff --git a/src/foodopts.h b/src/foodopts.h index 544dd3f..c8b0686 100644 --- a/src/foodopts.h +++ b/src/foodopts.h @@ -11,6 +11,25 @@ * the parsing results. * */ +struct foodoption { + const char *name; + int has_arg; + int *flag; + int val; + /* Extra values */ + const char *help; + const char *arg; +}; + +int +get_foodopt(int argc, char *const argv[], + const char *optstring, + const struct foodoption *longopts, + int *longindex); + +void +foodopt_help(char * argv0, + const struct foodoption *longopts); #endif /* __FOODOPTS_H */ @@ -40,6 +40,8 @@ trim(char * str) int begin = 0; int end = strlen(str) - 1; + if (end == -1) return; + while (isspace((unsigned char) str[begin])) begin++; |