[2880] in BarnOwl Developers
Re: [PATCH] RFC: Use Glib's GOptionContext to parse command-line
daemon@ATHENA.MIT.EDU (Nelson Elhage)
Fri Feb 4 19:07:27 2011
Date: Fri, 4 Feb 2011 19:07:22 -0500
From: Nelson Elhage <nelhage@nelhage.com>
To: David Benjamin <davidben@mit.edu>
Cc: barnowl-dev@mit.edu
In-Reply-To: <1296861769-4424-1-git-send-email-davidben@mit.edu>
I don't have strong opinions here. --help autogeneration is nice, and this is a
bit more succinct, but it involves a new API that people are less likely to know
than geptopt. And personally I like my --help output to look like --help output,
instead of a manpage :)
- Nelson
On Fri, Feb 04, 2011 at 06:22:49PM -0500, David Benjamin wrote:
> I'm not really sure if we want to use this instead of getopt_long. What
> do folks think? GOptionContext makes --help output and whatnot for us,
> which is kinda nice. It also makes us more consistent with, uh, all the
> other GOptionContext-using command-line programs. All two of them.
>
> Also kill --remove-debug because it doesn't do anything anymore.
>
> For reference, here's the old --help:
>
> Barnowl version 1.8dev-34-g4d24650
> Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]
> -n,--no-subs don't load zephyr subscriptions
> -d,--debug enable debugging
> -D,--remove-debug enable debugging and delete previous debug file
> -v,--version print the Barnowl version number and exit
> -h,--help print this help message
> -c,--config-file specify an alternate config file
> -s,--config-dir specify an alternate config dir (default ~/.owl)
> -t,--tty set the tty name
>
> And here's the GOptionContext one:
>
> Usage:
> barnowl [OPTION...]
>
> BarnOwl version 1.8dev-37-gfea0c4e
>
> Help Options:
> -h, --help Show help options
>
> Application Options:
> -v, --version Print the BarnOwl version number and exit
> -n, --no-subs Don't load zephyr subscriptions
> -d, --debug Enable debugging
> -c, --config-file=FILE Specify an alternate config file
> -s, --config-dir=DIR Specify an alternative config dir (default ~/.owl)
> -t, --tty=TTY Set the tty name
> ---
> owl.c | 98 ++++++++++++++++++++++------------------------------------------
> 1 files changed, 34 insertions(+), 64 deletions(-)
>
> diff --git a/owl.c b/owl.c
> index dd06f67..b758d7a 100644
> --- a/owl.c
> +++ b/owl.c
> @@ -8,7 +8,6 @@
>
> #include <stdio.h>
> #include <unistd.h>
> -#include <getopt.h>
> #include <stdlib.h>
> #include <string.h>
> #include <signal.h>
> @@ -37,72 +36,45 @@ int stderr_replace(void);
> owl_global g;
>
> typedef struct _owl_options {
> - bool load_initial_subs;
> + gboolean no_subs;
> char *configfile;
> char *tty;
> char *confdir;
> - bool debug;
> - bool rm_debug;
> + gboolean debug;
> } owl_options;
>
> -void usage(void)
> -{
> - fprintf(stderr, "Barnowl version %s\n", OWL_VERSION_STRING);
> - fprintf(stderr, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n");
> - fprintf(stderr, " -n,--no-subs don't load zephyr subscriptions\n");
> - fprintf(stderr, " -d,--debug enable debugging\n");
> - fprintf(stderr, " -D,--remove-debug enable debugging and delete previous debug file\n");
> - fprintf(stderr, " -v,--version print the Barnowl version number and exit\n");
> - fprintf(stderr, " -h,--help print this help message\n");
> - fprintf(stderr, " -c,--config-file specify an alternate config file\n");
> - fprintf(stderr, " -s,--config-dir specify an alternate config dir (default ~/.owl)\n");
> - fprintf(stderr, " -t,--tty set the tty name\n");
> -}
> -
> -/* TODO: free owl_options after init is done? */
> -void owl_parse_options(int argc, char *argv[], owl_options *opts) {
> - static const struct option long_options[] = {
> - { "no-subs", 0, 0, 'n' },
> - { "config-file", 1, 0, 'c' },
> - { "config-dir", 1, 0, 's' },
> - { "tty", 1, 0, 't' },
> - { "debug", 0, 0, 'd' },
> - { "remove-debug", 0, 0, 'D' },
> - { "version", 0, 0, 'v' },
> - { "help", 0, 0, 'h' },
> - { NULL, 0, NULL, 0}
> +static void owl_parse_options(int argc, char *argv[], owl_options *opts) {
> + gboolean version = false;
> + GOptionEntry entries[] = {
> + { "version", 'v', 0, G_OPTION_ARG_NONE, &version,
> + "Print the BarnOwl version number and exit", NULL },
> + { "no-subs", 'n', 0, G_OPTION_ARG_NONE, &opts->no_subs,
> + "Don't load zephyr subscriptions", NULL },
> + { "debug", 'd', 0, G_OPTION_ARG_NONE, &opts->debug,
> + "Enable debugging", NULL },
> + { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &opts->configfile,
> + "Specify an alternate config file", "FILE" },
> + { "config-dir", 's', 0, G_OPTION_ARG_FILENAME, &opts->confdir,
> + "Specify an alternative config dir (default ~/.owl)", "DIR" },
> + { "tty", 't', 0, G_OPTION_ARG_FILENAME, &opts->tty,
> + "Set the tty name", "TTY" },
> + { NULL }
> };
> - char c;
> -
> - while((c = getopt_long(argc, argv, "nc:t:s:dDvh",
> - long_options, NULL)) != -1) {
> - switch(c) {
> - case 'n':
> - opts->load_initial_subs = 0;
> - break;
> - case 'c':
> - opts->configfile = owl_strdup(optarg);
> - break;
> - case 's':
> - opts->confdir = owl_strdup(optarg);
> - break;
> - case 't':
> - opts->tty = owl_strdup(optarg);
> - break;
> - case 'D':
> - opts->rm_debug = 1;
> - /* fallthrough */
> - case 'd':
> - opts->debug = 1;
> - break;
> - case 'v':
> - printf("This is barnowl version %s\n", OWL_VERSION_STRING);
> - exit(0);
> - case 'h':
> - default:
> - usage();
> - exit(1);
> - }
> + GError *error = NULL;
> + GOptionContext *context;
> +
> + context = g_option_context_new("");
> + g_option_context_add_main_entries(context, entries, NULL);
> + g_option_context_set_summary(context, "BarnOwl version " OWL_VERSION_STRING);
> +
> + if (!g_option_context_parse(context, &argc, &argv, &error)) {
> + fprintf(stderr, "Option parsing failed: %s\n", error->message);
> + exit(1);
> + }
> +
> + if (version) {
> + printf("BarnOwl %s\n", OWL_VERSION_STRING);
> + exit(0);
> }
> }
>
> @@ -482,16 +454,14 @@ int main(int argc, char **argv, char **env)
> setlocale(LC_ALL, "");
>
> memset(&opts, 0, sizeof opts);
> - opts.load_initial_subs = 1;
> owl_parse_options(argc, argv, &opts);
> - g.load_initial_subs = opts.load_initial_subs;
> + g.load_initial_subs = !opts.no_subs;
>
> owl_register_signal_handlers();
> owl_start_curses();
>
> /* owl global init */
> owl_global_init(&g);
> - if (opts.rm_debug) unlink(OWL_DEBUG_FILE);
> if (opts.debug) owl_global_set_debug_on(&g);
> if (opts.confdir) owl_global_set_confdir(&g, opts.confdir);
> owl_function_debugmsg("startup: first available debugging message");
> --
> 1.7.3.3.399.g06ddf
>