[util] Further refactor option parsing

pull/1/head
Behdad Esfahbod 13 years ago
parent bc187e5ac7
commit 109cb38289
  1. 80
      util/options.cc
  2. 20
      util/options.hh

@ -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 void
parse_options (int argc, char *argv[]) option_context_add_view_opts (GOptionContext *context)
{ {
GOptionEntry entries[] = GOptionEntry entries[] =
{ {
{"version", 0, G_OPTION_FLAG_NO_ARG, {"annotate", 0, 0, G_OPTION_ARG_NONE, &view_opts->annotate, "Annotate output rendering", NULL},
G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", NULL}, {"background", 0, 0, G_OPTION_ARG_STRING, &view_opts->back, "Set background color (default: "DEFAULT_BACK")", "red/#rrggbb/#rrggbbaa"},
{"debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Free all resources before exit", NULL}, {"foreground", 0, 0, G_OPTION_ARG_STRING, &view_opts->fore, "Set foreground color (default: "DEFAULT_FORE")", "red/#rrggbb/#rrggbbaa"},
{"output", 0, 0, G_OPTION_ARG_STRING, &out_file, "Set output file name", "filename"}, {"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"},
{"annotate", 0, 0, G_OPTION_ARG_NONE, &view_opts->annotate, "Annotate output rendering", NULL}, {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"}, option_context_add_entries (context, entries,
{"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &view_opts->line_space, "Set space between lines (default: 0)", "units"}, "view",
{"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output", "one to four numbers"}, "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"}, {"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"}, {"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"}, {"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"}, {"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"}, {"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"}, void
{"font-size", 0, 0, G_OPTION_ARG_DOUBLE, &font_opts->font_size, "Font size", "size"}, 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} {NULL}
}; };
GError *parse_error = NULL; GError *parse_error = NULL;
@ -294,6 +345,9 @@ parse_options (int argc, char *argv[])
context = g_option_context_new ("- FONT-FILE TEXT"); context = g_option_context_new ("- FONT-FILE TEXT");
g_option_context_add_main_entries (context, entries, NULL); 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)) if (!g_option_context_parse (context, &argc, &argv, &parse_error))
{ {

@ -30,13 +30,17 @@
#define OPTIONS_HH #define OPTIONS_HH
#define DEFAULT_MARGIN 18
#define DEFAULT_FORE "#000000"
#define DEFAULT_BACK "#FFFFFF"
extern struct view_options_t extern struct view_options_t
{ {
view_options_t (void) { view_options_t (void) {
memset (this, 0, sizeof (*this)); memset (this, 0, sizeof (*this));
fore = "#000000"; fore = DEFAULT_FORE;
back = "#ffffff"; back = DEFAULT_BACK;
margin.t = margin.r = margin.b = margin.l = 18.; margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
} }
hb_bool_t annotate; hb_bool_t annotate;
@ -48,6 +52,7 @@ extern struct view_options_t
} margin; } margin;
} view_opts[1]; } view_opts[1];
extern struct shape_options_t extern struct shape_options_t
{ {
shape_options_t (void) { shape_options_t (void) {
@ -63,11 +68,14 @@ extern struct shape_options_t
char **shapers; char **shapers;
} shape_opts[1]; } shape_opts[1];
#define DEFAULT_FONT_SIZE 36
extern struct font_options_t extern struct font_options_t
{ {
font_options_t (void) { font_options_t (void) {
memset (this, 0, sizeof (*this)); memset (this, 0, sizeof (*this));
font_size = 36.; font_size = DEFAULT_FONT_SIZE;
} }
const char *font_file; const char *font_file;
@ -80,6 +88,10 @@ extern const char *out_file;
extern hb_bool_t debug; 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[]); void parse_options (int argc, char *argv[]);

Loading…
Cancel
Save