]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/opt-test.c
Redblack: Added search_up
[libucw.git] / ucw / opt-test.c
index 7de4767c9594a08df3eaaa9919d006d4074d91cc..07fd6835f8c1ca7b1d5a69ed18c49744910feecf 100644 (file)
@@ -2,6 +2,7 @@
  *     UCW Library -- Parsing of command line options
  *
  *     (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
+ *     (c) 2014 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -11,6 +12,7 @@
 #include <ucw/opt.h>
 #include <ucw/strtonum.h>
 #include <ucw/fastbuf.h>
+#include <ucw/gary.h>
 
 static void show_version(struct opt_item * opt UNUSED, const char * value UNUSED, void * data UNUSED) {
   printf("This is a simple tea boiling console v0.1.\n");
@@ -45,9 +47,10 @@ static int english = 0;
 static int sugar = 0;
 static int verbose = 1;
 static int with_gas = 0;
-static clist black_magic;
+static int *black_magic;
 static int pray = 0;
 static int water_amount = 0;
+static int clean_pot = 1;
 static char * first_tea = NULL;
 
 #define MAX_TEA_COUNT 30
@@ -97,7 +100,7 @@ static struct cf_user_type teapot_temperature_t = {
   .dumper = (cf_dumper1*) teapot_temperature_dumper
 };
 
-static void opt_test_hook(struct opt_item * opt, const char * value, void * data) {
+static void opt_test_hook(struct opt_item * opt, uint event UNUSED, const char * value, void * data) {
   if (!show_hooks)
     return;
   if (opt)
@@ -114,10 +117,11 @@ static struct opt_section water_options = {
   }
 };
 
-static struct opt_section help = {
+static struct opt_section options = {
   OPT_ITEMS {
     OPT_HELP("A simple tea boiling console."),
     OPT_HELP("Usage: teapot [options] name-of-the-tea"),
+    OPT_HELP(""),
     OPT_HELP("Black, green or white tea supported as well as fruit or herbal tea."),
     OPT_HELP("You may specify more kinds of tea, all of them will be boiled for you, in the given order."),
     OPT_HELP("At least one kind of tea must be specified."),
@@ -128,10 +132,10 @@ static struct opt_section help = {
     OPT_HELP(""),
     OPT_BOOL('e', "english-style", english, 0, "\tEnglish style (with milk)"),
     OPT_INT('s', "sugar", sugar, OPT_REQUIRED_VALUE, "<spoons>\tAmount of sugar (in teaspoons)"),
-    OPT_SWITCH(0, "standard-set", set, TEAPOT_STANDARD, 0, "\tStandard teapot"),
-    OPT_SWITCH('x', "exclusive-set", set, TEAPOT_EXCLUSIVE, 0, "\tExclusive teapot"),
-    OPT_SWITCH('g', "glass-set", set, TEAPOT_GLASS, 0, "\tTransparent glass teapot"),
-    OPT_SWITCH('h', "hands", set, TEAPOT_HANDS, 0, "\tUse user's hands as a teapot (a bit dangerous)"),
+    OPT_SWITCH(0, "standard-set", set, TEAPOT_STANDARD, OPT_SINGLE, "\tStandard teapot"),
+    OPT_SWITCH('x', "exclusive-set", set, TEAPOT_EXCLUSIVE, OPT_SINGLE, "\tExclusive teapot"),
+    OPT_SWITCH('g', "glass-set", set, TEAPOT_GLASS, OPT_SINGLE, "\tTransparent glass teapot"),
+    OPT_SWITCH('h', "hands", set, TEAPOT_HANDS, OPT_SINGLE, "\tUse user's hands as a teapot (a bit dangerous)"),
     OPT_USER('t', "temperature", temperature, teapot_temperature_t, OPT_REQUIRED_VALUE | OPT_REQUIRED,
                  "<value>\tWanted final temperature of the tea to be served (required)\n"
              "\t\tSupported scales:  Celsius [60C], Fahrenheit [140F],\n"
@@ -139,17 +143,20 @@ static struct opt_section help = {
              "\t\tOnly integer values allowed."),
     OPT_INC('v', "verbose", verbose, 0, "\tVerbose (the more -v, the more verbose)"),
     OPT_INC('q', "quiet", verbose, OPT_NEGATIVE, "\tQuiet (the more -q, the more quiet)"),
-    OPT_INT('b', "black-magic", black_magic, OPT_MULTIPLE, "<strength>\tUse black magic to make the tea extraordinary delicious.\n\t\tMay be specified more than once to describe the amounts of black magic to be invoked in each step of tea boiling."),
+    OPT_INT_MULTIPLE('b', NULL, black_magic, 0, "<strength>\tUse black magic to make the tea extraordinarily delicious.\n\t\tMay be specified more than once to describe the amounts of black magic to be invoked in each step of tea boiling."),
     OPT_BOOL('p', "pray", pray, OPT_SINGLE, "\tPray before boiling"),
-    OPT_STRING(OPT_POSITIONAL(1), NULL, first_tea, OPT_REQUIRED | OPT_NO_HELP, ""),
-    OPT_CALL(OPT_POSITIONAL_TAIL, NULL, add_tea, &tea_list, OPT_NO_HELP, ""),
+    OPT_BOOL(0, "no-clean", clean_pot, OPT_NEGATIVE, "\tDo not clean the teapot before boiling"),
+    OPT_STRING(OPT_POSITIONAL(1), NULL, first_tea, OPT_REQUIRED, ""),
+    OPT_CALL(OPT_POSITIONAL_TAIL, NULL, add_tea, &tea_list, 0, ""),
     OPT_HELP(""),
     OPT_HELP("Water options:"),
     OPT_SECTION(water_options),
     OPT_HOOK(opt_test_hook, "prearg", OPT_HOOK_BEFORE_ARG),
     OPT_HOOK(opt_test_hook, "preval", OPT_HOOK_BEFORE_VALUE),
     OPT_HOOK(opt_test_hook, "postval", OPT_HOOK_AFTER_VALUE),
-    OPT_BOOL('H', "show-hooks", show_hooks, 0, "Demonstrate the hooks."),
+    OPT_BOOL('H', "show-hooks", show_hooks, 0, "\tDemonstrate the hooks."),
+    OPT_HELP(""),
+    OPT_HELP("Configuration options:"),
     OPT_CONF_OPTIONS,
     OPT_END
   }
@@ -162,8 +169,9 @@ struct intnode {
 
 int main(int argc UNUSED, char ** argv)
 {
-  clist_init(&black_magic);
-  opt_parse(&help, argv+1);
+  cf_def_file = "etc/libucw";
+  GARY_INIT(black_magic, 0);
+  opt_parse(&options, argv+1);
 
   printf("English style: %s|", english ? "yes" : "no");
   if (sugar)
@@ -172,9 +180,11 @@ int main(int argc UNUSED, char ** argv)
     printf("Chosen teapot: %s|", teapot_type_str[set]);
   printf("Temperature: %d%s|", temperature.value, temp_scale_str[temperature.scale]);
   printf("Verbosity: %d|", verbose);
-  CLIST_FOR_EACH(struct intnode *, n, black_magic)
-    printf("Black magic: %d|", n->x);
+  uint magick = GARY_SIZE(black_magic);
+  for (uint i=0; i<magick; i++)
+    printf("Black magic: %d|", black_magic[i]);
   printf("Prayer: %s|", pray ? "yes" : "no");
+  printf("Clean: %s|", clean_pot ? "yes" : "no");
   printf("Water amount: %d|", water_amount);
   printf("Gas: %s|", with_gas ? "yes" : "no");
   printf("First tea: %s|", first_tea);