diff options
author | Matthew Barnes <mbarnes@redhat.com> | 2014-11-14 14:00:13 -0500 |
---|---|---|
committer | Matthew Barnes <mbarnes@redhat.com> | 2014-11-24 19:36:07 -0500 |
commit | 97558276e4f855442337be01abc8f90cf0dd1810 (patch) | |
tree | cadf8676b5163ed41c810335a57ddd4cf0e95f26 /src/ostree/ot-builtin-admin.c | |
parent | 3a9127f103df3f4574aa441e044447cf36358578 (diff) | |
download | ostree-97558276e4f855442337be01abc8f90cf0dd1810.tar.gz |
Refactor command-line parsing
Refactor command-line parsing to better utilize GOptionContext. This
eliminates most of the manual parsing and global options are now shown
in the help output.
Here's a sample:
$ ostree admin --help
Usage:
ostree admin [OPTION...] --print-current-dir|COMMAND
Builtin "admin" Commands:
cleanup
config-diff
deploy
init-fs
instutil
os-init
status
switch
undeploy
upgrade
Help Options:
-h, --help Show help options
Application Options:
--sysroot=PATH Create a new OSTree sysroot at PATH
-v, --verbose Print debug information during command processing
--version Print version information and exit
https://bugzilla.gnome.org/show_bug.cgi?id=740295
Diffstat (limited to 'src/ostree/ot-builtin-admin.c')
-rw-r--r-- | src/ostree/ot-builtin-admin.c | 185 |
1 files changed, 61 insertions, 124 deletions
diff --git a/src/ostree/ot-builtin-admin.c b/src/ostree/ot-builtin-admin.c index 2165656c..47cf1b74 100644 --- a/src/ostree/ot-builtin-admin.c +++ b/src/ostree/ot-builtin-admin.c @@ -34,7 +34,7 @@ typedef struct { const char *name; - gboolean (*fn) (int argc, char **argv, OstreeSysroot *sysroot, GCancellable *cancellable, GError **error); + gboolean (*fn) (int argc, char **argv, GCancellable *cancellable, GError **error); } OstreeAdminCommand; static OstreeAdminCommand admin_subcommands[] = { @@ -51,19 +51,38 @@ static OstreeAdminCommand admin_subcommands[] = { { NULL, NULL } }; +static GOptionContext * +ostree_admin_option_context_new_with_commands (void) +{ + OstreeAdminCommand *command = admin_subcommands; + GOptionContext *context; + GString *summary; + + context = g_option_context_new ("--print-current-dir|COMMAND"); + + summary = g_string_new ("Builtin \"admin\" Commands:"); + + while (command->name != NULL) + { + g_string_append_printf (summary, "\n %s", command->name); + command++; + } + + g_option_context_set_summary (context, summary->str); + + g_string_free (summary, TRUE); + + return context; +} + gboolean -ostree_builtin_admin (int argc, char **argv, OstreeRepo *repo, GCancellable *cancellable, GError **error) +ostree_builtin_admin (int argc, char **argv, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; - const char *opt_sysroot = "/"; const char *subcommand_name = NULL; OstreeAdminCommand *subcommand; - gs_unref_object GFile *sysroot_path = NULL; - gs_unref_object OstreeSysroot *sysroot = NULL; - gboolean want_help = FALSE; - gboolean want_current_dir = FALSE; - int in, out, i; - gboolean skip; + gs_free char *prgname = NULL; + int in, out; /* * Parse the global options. We rearrange the options as @@ -76,148 +95,66 @@ ostree_builtin_admin (int argc, char **argv, OstreeRepo *repo, GCancellable *can /* The non-option is the command, take it out of the arguments */ if (argv[in][0] != '-') { - skip = (subcommand_name == NULL); if (subcommand_name == NULL) - subcommand_name = argv[in]; - } - - /* The global long options */ - else if (argv[in][1] == '-') - { - skip = FALSE; - - if (g_str_equal (argv[in], "--")) - { - break; - } - else if (g_str_equal (argv[in], "--help")) - { - want_help = TRUE; - } - else if (g_str_equal (argv[in], "--print-current-dir")) - { - want_current_dir = TRUE; - } - else if (g_str_equal (argv[in], "--sysroot") && in + 1 < argc) { - opt_sysroot = argv[in + 1]; - skip = TRUE; - in++; - } - else if (g_str_has_prefix (argv[in], "--sysroot=")) - { - opt_sysroot = argv[in] + 10; - skip = TRUE; - } - else if (subcommand_name == NULL) - { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, - "Unknown or invalid admin option: %s", argv[in]); - goto out; + subcommand_name = argv[in]; + out--; + continue; } } - /* The global short options */ - else + else if (g_str_equal (argv[in], "--")) { - skip = FALSE; - for (i = 1; argv[in][i] != '\0'; i++) - { - switch (argv[in][i]) - { - case 'h': - want_help = TRUE; - break; - - default: - if (subcommand_name == NULL) - { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, - "Unknown or invalid admin option: %s", argv[in]); - goto out; - } - break; - } - } + break; } - /* Skipping this argument? */ - if (skip) - out--; - else - argv[out] = argv[in]; + argv[out] = argv[in]; } argc = out; - if (subcommand_name == NULL && (want_help || !want_current_dir)) + subcommand = admin_subcommands; + while (subcommand->name) { - void (*print_func) (const gchar *format, ...) = want_help ? g_print : g_printerr; - - subcommand = admin_subcommands; - print_func ("usage: ostree admin [--sysroot=PATH] [--print-current-dir|COMMAND] [options]\n"); - print_func ("Builtin commands:\n"); - while (subcommand->name) - { - print_func (" %s\n", subcommand->name); - subcommand++; - } - - if (want_help) - ret = TRUE; - else - g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, - "No command specified"); - goto out; + if (g_strcmp0 (subcommand_name, subcommand->name) == 0) + break; + subcommand++; } - sysroot_path = g_file_new_for_path (opt_sysroot); - sysroot = ostree_sysroot_new (sysroot_path); - - if (want_current_dir) + if (!subcommand->name) { - gs_unref_ptrarray GPtrArray *deployments = NULL; - OstreeDeployment *first_deployment; - gs_unref_object GFile *deployment_file = NULL; - gs_free char *deployment_path = NULL; + GOptionContext *context; + gs_free char *help; - if (!ostree_sysroot_load (sysroot, cancellable, error)) - goto out; + context = ostree_admin_option_context_new_with_commands (); - deployments = ostree_sysroot_get_deployments (sysroot); - if (deployments->len == 0) + /* This will not return for some options (e.g. --version). */ + if (ostree_admin_option_context_parse (context, NULL, &argc, &argv, NULL, cancellable, error)) { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, - "Unable to find a deployment in sysroot"); - goto out; + if (subcommand_name == NULL) + { + g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, + "No \"admin\" subcommand specified"); + } + else + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, + "Unknown \"admin\" subcommand '%s'", subcommand_name); + } } - first_deployment = deployments->pdata[0]; - deployment_file = ostree_sysroot_get_deployment_directory (sysroot, first_deployment); - deployment_path = g_file_get_path (deployment_file); - g_print ("%s\n", deployment_path); - ret = TRUE; - goto out; - } + help = g_option_context_get_help (context, FALSE, NULL); + g_printerr ("%s", help); - subcommand = admin_subcommands; - while (subcommand->name) - { - if (g_strcmp0 (subcommand_name, subcommand->name) == 0) - break; - subcommand++; - } + g_option_context_free (context); - if (!subcommand->name) - { - g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, - "Unknown admin command '%s'", subcommand_name); goto out; } - g_set_prgname (g_strdup_printf ("ostree admin %s", subcommand_name)); + prgname = g_strdup_printf ("%s %s", g_get_prgname (), subcommand_name); + g_set_prgname (prgname); - if (!subcommand->fn (argc, argv, sysroot, cancellable, error)) + if (!subcommand->fn (argc, argv, cancellable, error)) goto out; ret = TRUE; |