From 7b7240d50dd5e418bc17beff48aa5681b398b980 Mon Sep 17 00:00:00 2001 From: gramanas Date: Fri, 14 Oct 2022 22:11:50 +0300 Subject: More changes --- src/food.c | 75 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 25 deletions(-) (limited to 'src/food.c') diff --git a/src/food.c b/src/food.c index 02cd280..95fde55 100644 --- a/src/food.c +++ b/src/food.c @@ -21,7 +21,9 @@ static struct opts { int eval; int add; int add_n; + int listi; int list; + char listfmt[2048]; int hash; int search; int search_strict; @@ -38,7 +40,9 @@ static struct opts { .add = 0, .add_n = 0, .title = 0, + .listi = 0, .list = 0, + .listfmt = "", .hash = 0, .search = 0, .search_strict = 0, @@ -69,26 +73,26 @@ main(int argc, char * argv[]) 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"}, + {"help", no_argument, 0, 'h', "Print this help", 0, "General" }, + {"include", required_argument, 0, 'I', "Path to recipe library, can be passed multiple times", "PATH", 0 }, + {"no-eval", no_argument, 0, 'n', "Don't evaluate recipes", 0, 0 }, + {"to-json", no_argument, 0, 'j', "Format recipe to json", 0, "Output format" }, + {"to-html", no_argument, 0, 'w', "Format recipe to html", 0, 0 }, + {"to-rcp", no_argument, 0, 'r', "Format recipe to rcp", 0, 0 }, + {"list-ingredients", no_argument, 0, 'L', "List ingredients for matched recipes", 0, 0 }, + {"list", optional_argument, 0, 'l', "List matched recipes with optional FORMAT", "[FORMAT]", 0 }, + {"search", required_argument, 0, 's', "Return recipes matching QUERY", "QUERY", "Filters" }, + {"title", required_argument, 0, 't', "Return recipes matching TITLE", "TITLE", 0 }, + {"strict", required_argument, 0, 'S', "Return recipes matching QUERY, exactly", "QUERY", 0 }, + {"hash", required_argument, 0, 'H', "Return recipes by HASH", "HASH", 0 }, + {"add-item", required_argument, 0, 'a', "Add item to matched recipes", "ITEM", "Other" }, //{"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) { int option_index = 0; - c = get_foodopt (argc, argv, "jnlhrwf:s:S:t:H:I:a:", + c = get_foodopt (argc, argv, ":jnLhrws:S:t:H:I:a:l:", long_options, &option_index); if (c == -1) @@ -98,15 +102,6 @@ main(int argc, char * argv[]) case 0: printf("Flag setting\n"); break; - case 'f': - if (!strcmp(optarg, "json")) - opt.json = 1; - else if (!strcmp(optarg, "rcp")) - opt.rcp = 1; - else if (!strcmp(optarg, "html")) - opt.html = 1; - else - fprintf(stderr, "invalid format: %s\n", optarg); break; case 'I': strcpy(opt.includes[opt.includes_n++], optarg); @@ -139,6 +134,10 @@ main(int argc, char * argv[]) break; case 'l': opt.list = 1; + strcpy(opt.listfmt, optarg); + break; + case 'L': + opt.listi = 1; break; case 'h': opt.help = 1; @@ -151,8 +150,21 @@ main(int argc, char * argv[]) strcpy(opt.add_val[opt.add_n++], optarg); break; case '?': + fprintf(stderr, "%s: option '%c' not recognised, try -h for help\n", argv[0], optopt); return -1; break; + case ':': + switch (optopt) { + case 'l': + opt.list = 1; + strcpy(opt.listfmt, "DEFAULT"); + break; + default: + fprintf(stderr, "%s: missing argument %s for -%c\n", argv[0], get_argument(optopt, long_options), optopt); + return -1; + break; + } + break; default: abort (); } @@ -163,6 +175,16 @@ main(int argc, char * argv[]) return 0; } + if (opt.json + opt.rcp + opt.html + opt.list + opt.listi > 1) { + fprintf(stderr, "%s: only one output format is allowed at a time\n", argv[0]); + return 1; + } + + if (opt.eval == 0 && opt.hash == 1) { + fprintf(stderr, "%s: can't search hash without evaluation\n", argv[0]); + return 1; + } + char ** lib = NULL; int n = collect_library(&lib, argv, argc, optind, @@ -208,15 +230,18 @@ main(int argc, char * argv[]) fprintf(stderr, "Error: %s\n", err); } } - if (opt.list) { + if (opt.listi) { pprint_items(r); } + if (opt.list) { + listing(r , opt.listfmt); + } else { if (opt.json) tojson(r); else if (opt.html) tohtml(r); else if (opt.rcp) torcp(r); else { - listing(r); + // listing(r); } } free_recipe(r); -- cgit v1.2.3