[util] Further refactor option parsing
authorBehdad Esfahbod <behdad@behdad.org>
Thu, 8 Sep 2011 20:00:04 +0000 (16:00 -0400)
committerBehdad Esfahbod <behdad@behdad.org>
Thu, 8 Sep 2011 20:00:04 +0000 (16:00 -0400)
util/options.cc
util/options.hh

index 3809b1a..72f8e5d 100644 (file)
@@ -261,31 +261,82 @@ show_version (const char *name G_GNUC_UNUSED,
 }
 
 
+static void
+option_context_add_entries (GOptionContext *context,
+                           GOptionEntry   *entries,
+                           const gchar    *name,
+                           const gchar    *description,
+                           const gchar    *help_description)
+{
+  if (0) {
+    GOptionGroup *group = g_option_group_new (name, description, help_description, NULL, NULL);
+    g_option_group_add_entries (group, entries);
+    g_option_context_add_group (context, group);
+  } else {
+    g_option_context_add_main_entries (context, entries, NULL);
+  }
+}
+
 void
-parse_options (int argc, char *argv[])
+option_context_add_view_opts (GOptionContext *context)
 {
   GOptionEntry entries[] =
   {
-    {"version",                0, G_OPTION_FLAG_NO_ARG,
-                             G_OPTION_ARG_CALLBACK,    (gpointer) &show_version,       "Show version numbers",                 NULL},
-    {"debug",          0, 0, G_OPTION_ARG_NONE,        &debug,                         "Free all resources before exit",       NULL},
-    {"output",         0, 0, G_OPTION_ARG_STRING,      &out_file,                      "Set output file name",                 "filename"},
-
-    {"annotate",       0, 0, G_OPTION_ARG_NONE,        &view_opts->annotate,           "Annotate output rendering",            NULL},
-    {"background",     0, 0, G_OPTION_ARG_STRING,      &view_opts->back,               "Set background color",                 "red/#rrggbb/#rrggbbaa"},
-    {"foreground",     0, 0, G_OPTION_ARG_STRING,      &view_opts->fore,               "Set foreground color",                 "red/#rrggbb/#rrggbbaa"},
-    {"line-space",     0, 0, G_OPTION_ARG_DOUBLE,      &view_opts->line_space,         "Set space between lines (default: 0)", "units"},
-    {"margin",         0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_margin,       "Margin around output",                 "one to four numbers"},
+    {"annotate",       0, 0, G_OPTION_ARG_NONE,        &view_opts->annotate,           "Annotate output rendering",                            NULL},
+    {"background",     0, 0, G_OPTION_ARG_STRING,      &view_opts->back,               "Set background color (default: "DEFAULT_BACK")",       "red/#rrggbb/#rrggbbaa"},
+    {"foreground",     0, 0, G_OPTION_ARG_STRING,      &view_opts->fore,               "Set foreground color (default: "DEFAULT_FORE")",       "red/#rrggbb/#rrggbbaa"},
+    {"line-space",     0, 0, G_OPTION_ARG_DOUBLE,      &view_opts->line_space,         "Set space between lines (default: 0)",                 "units"},
+    {"margin",         0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_margin,       "Margin around output (default: "G_STRINGIFY(DEFAULT_MARGIN)")","one to four numbers"},
+    {NULL}
+  };
+  option_context_add_entries (context, entries,
+                             "view",
+                             "View options:",
+                             "Options controlling the output rendering");
+}
 
+void
+option_context_add_shape_opts (GOptionContext *context)
+{
+  GOptionEntry entries[] =
+  {
     {"shapers",                0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_shapers,      "Comma-separated list of shapers",      "list"},
     {"direction",      0, 0, G_OPTION_ARG_STRING,      &shape_opts->direction,         "Set text direction (default: auto)",   "ltr/rtl/ttb/btt"},
     {"language",       0, 0, G_OPTION_ARG_STRING,      &shape_opts->language,          "Set text language (default: $LANG)",   "langstr"},
     {"script",         0, 0, G_OPTION_ARG_STRING,      &shape_opts->script,            "Set text script (default: auto)",      "ISO-15924 tag"},
     {"features",       0, 0, G_OPTION_ARG_CALLBACK,    (gpointer) &parse_features,     "Font features to apply to text",       "TODO"},
+    {NULL}
+  };
+  option_context_add_entries (context, entries,
+                             "shape",
+                             "Shape options:",
+                             "Options controlling the shaping process");
+}
 
-    {"face-index",     0, 0, G_OPTION_ARG_INT,         &font_opts->face_index,         "Face index (default: 0)",              "index"},
-    {"font-size",      0, 0, G_OPTION_ARG_DOUBLE,      &font_opts->font_size,          "Font size",                            "size"},
+void
+option_context_add_font_opts (GOptionContext *context)
+{
+  GOptionEntry entries[] =
+  {
+    {"face-index",     0, 0, G_OPTION_ARG_INT,         &font_opts->face_index,         "Face index (default: 0)",                              "index"},
+    {"font-size",      0, 0, G_OPTION_ARG_DOUBLE,      &font_opts->font_size,          "Font size (default: "G_STRINGIFY(DEFAULT_FONT_SIZE)")","size"},
+    {NULL}
+  };
+  option_context_add_entries (context, entries,
+                             "font",
+                             "Font options:",
+                             "Options controlling the font");
+}
 
+void
+parse_options (int argc, char *argv[])
+{
+  GOptionEntry entries[] =
+  {
+    {"version",                0, G_OPTION_FLAG_NO_ARG,
+                             G_OPTION_ARG_CALLBACK,    (gpointer) &show_version,       "Show version numbers",                 NULL},
+    {"debug",          0, 0, G_OPTION_ARG_NONE,        &debug,                         "Free all resources before exit",       NULL},
+    {"output",         0, 0, G_OPTION_ARG_STRING,      &out_file,                      "Set output file name",                 "filename"},
     {NULL}
   };
   GError *parse_error = NULL;
@@ -294,6 +345,9 @@ parse_options (int argc, char *argv[])
   context = g_option_context_new ("- FONT-FILE TEXT");
 
   g_option_context_add_main_entries (context, entries, NULL);
+  option_context_add_view_opts (context);
+  option_context_add_shape_opts (context);
+  option_context_add_font_opts (context);
 
   if (!g_option_context_parse (context, &argc, &argv, &parse_error))
   {
index 9be0b6b..716d1ad 100644 (file)
 #define OPTIONS_HH
 
 
+#define DEFAULT_MARGIN 18
+#define DEFAULT_FORE "#000000"
+#define DEFAULT_BACK "#FFFFFF"
+
 extern struct view_options_t
 {
   view_options_t (void) {
     memset (this, 0, sizeof (*this));
-    fore = "#000000";
-    back = "#ffffff";
-    margin.t = margin.r = margin.b = margin.l = 18.;
+    fore = DEFAULT_FORE;
+    back = DEFAULT_BACK;
+    margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
   }
 
   hb_bool_t annotate;
@@ -48,6 +52,7 @@ extern struct view_options_t
   } margin;
 } view_opts[1];
 
+
 extern struct shape_options_t
 {
   shape_options_t (void) {
@@ -63,11 +68,14 @@ extern struct shape_options_t
   char **shapers;
 } shape_opts[1];
 
+
+#define DEFAULT_FONT_SIZE 36
+
 extern struct font_options_t
 {
   font_options_t (void) {
     memset (this, 0, sizeof (*this));
-    font_size = 36.;
+    font_size = DEFAULT_FONT_SIZE;
   }
 
   const char *font_file;
@@ -80,6 +88,10 @@ extern const char *out_file;
 extern hb_bool_t debug;
 
 
+void option_context_add_view_opts      (GOptionContext *context);
+void option_context_add_shape_opts     (GOptionContext *context);
+void option_context_add_font_opts      (GOptionContext *context);
+
 void parse_options (int argc, char *argv[]);