mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-23 18:52:09 +01:00
gio-tool: Factor out repetitive lists of subcommands
Store their details in an array which can be iterated over instead. This introduces no functional changes, just a cleanup which will allow following commits to be neater. Signed-off-by: Philip Withnall <philip@tecnocode.co.uk>
This commit is contained in:
parent
d1eb463ce2
commit
7c97b93837
130
gio/gio-tool.c
130
gio/gio-tool.c
@ -219,34 +219,64 @@ handle_version (int argc, char *argv[], gboolean do_help)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef int (*HandleSubcommand) (int argc, char *argv[], gboolean do_help);
|
||||||
|
|
||||||
|
static const struct
|
||||||
|
{
|
||||||
|
const char *name; /* as used on the command line */
|
||||||
|
HandleSubcommand handle_func; /* (nullable) for "help" only */
|
||||||
|
const char *description; /* translatable */
|
||||||
|
} gio_subcommands[] = {
|
||||||
|
{ "help", NULL, N_("Print help") },
|
||||||
|
{ "version", handle_version, N_("Print version") },
|
||||||
|
{ "cat", handle_cat, N_("Concatenate files to standard output") },
|
||||||
|
{ "copy", handle_copy, N_("Copy one or more files") },
|
||||||
|
{ "info", handle_info, N_("Show information about locations") },
|
||||||
|
{ "launch", handle_launch, N_("Launch an application from a desktop file") },
|
||||||
|
{ "list", handle_list, N_("List the contents of locations") },
|
||||||
|
{ "mime", handle_mime, N_("Get or set the handler for a mimetype") },
|
||||||
|
{ "mkdir", handle_mkdir, N_("Create directories") },
|
||||||
|
{ "monitor", handle_monitor, N_("Monitor files and directories for changes") },
|
||||||
|
{ "mount", handle_mount, N_("Mount or unmount the locations") },
|
||||||
|
{ "move", handle_move, N_("Move one or more files") },
|
||||||
|
{ "open", handle_open, N_("Open files with the default application") },
|
||||||
|
{ "rename", handle_rename, N_("Rename a file") },
|
||||||
|
{ "remove", handle_remove, N_("Delete one or more files") },
|
||||||
|
{ "save", handle_save, N_("Read from standard input and save") },
|
||||||
|
{ "set", handle_set, N_("Set a file attribute") },
|
||||||
|
{ "trash", handle_trash, N_("Move files or directories to the trash") },
|
||||||
|
{ "tree", handle_tree, N_("Lists the contents of locations in a tree") },
|
||||||
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage (void)
|
usage (void)
|
||||||
{
|
{
|
||||||
g_printerr ("%s\n", _("Usage:"));
|
GString *out = NULL;
|
||||||
g_printerr (" gio %s %s\n", _("COMMAND"), _("[ARGS…]"));
|
size_t name_width = 0;
|
||||||
g_printerr ("\n");
|
|
||||||
g_printerr ("%s\n", _("Commands:"));
|
out = g_string_new ("");
|
||||||
g_printerr (" help %s\n", _("Print help"));
|
g_string_append_printf (out, "%s\n", _("Usage:"));
|
||||||
g_printerr (" version %s\n", _("Print version"));
|
g_string_append_printf (out, " gio %s %s\n", _("COMMAND"), _("[ARGS…]"));
|
||||||
g_printerr (" cat %s\n", _("Concatenate files to standard output"));
|
g_string_append_c (out, '\n');
|
||||||
g_printerr (" copy %s\n", _("Copy one or more files"));
|
g_string_append_printf (out, "%s\n", _("Commands:"));
|
||||||
g_printerr (" info %s\n", _("Show information about locations"));
|
|
||||||
g_printerr (" launch %s\n", _("Launch an application from a desktop file"));
|
/* Work out the maximum name length for column alignment. */
|
||||||
g_printerr (" list %s\n", _("List the contents of locations"));
|
for (size_t i = 0; i < G_N_ELEMENTS (gio_subcommands); i++)
|
||||||
g_printerr (" mime %s\n", _("Get or set the handler for a mimetype"));
|
name_width = MAX (name_width, strlen (gio_subcommands[i].name));
|
||||||
g_printerr (" mkdir %s\n", _("Create directories"));
|
|
||||||
g_printerr (" monitor %s\n", _("Monitor files and directories for changes"));
|
for (size_t i = 0; i < G_N_ELEMENTS (gio_subcommands); i++)
|
||||||
g_printerr (" mount %s\n", _("Mount or unmount the locations"));
|
{
|
||||||
g_printerr (" move %s\n", _("Move one or more files"));
|
g_string_append_printf (out, " %-*s %s\n",
|
||||||
g_printerr (" open %s\n", _("Open files with the default application"));
|
(int) name_width, gio_subcommands[i].name,
|
||||||
g_printerr (" rename %s\n", _("Rename a file"));
|
_(gio_subcommands[i].description));
|
||||||
g_printerr (" remove %s\n", _("Delete one or more files"));
|
}
|
||||||
g_printerr (" save %s\n", _("Read from standard input and save"));
|
|
||||||
g_printerr (" set %s\n", _("Set a file attribute"));
|
g_string_append_c (out, '\n');
|
||||||
g_printerr (" trash %s\n", _("Move files or directories to the trash"));
|
g_string_append_printf (out, _("Use %s to get detailed help.\n"), "“gio help COMMAND”");
|
||||||
g_printerr (" tree %s\n", _("Lists the contents of locations in a tree"));
|
|
||||||
g_printerr ("\n");
|
g_printerr ("%s", out->str);
|
||||||
g_printerr (_("Use %s to get detailed help.\n"), "“gio help COMMAND”");
|
|
||||||
|
g_string_free (out, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -306,44 +336,18 @@ main (int argc, char **argv)
|
|||||||
else if (g_str_equal (command, "--version"))
|
else if (g_str_equal (command, "--version"))
|
||||||
command = "version";
|
command = "version";
|
||||||
|
|
||||||
if (g_str_equal (command, "version"))
|
/* Work out which subcommand it is. */
|
||||||
return handle_version (argc, argv, do_help);
|
for (size_t i = 0; i < G_N_ELEMENTS (gio_subcommands); i++)
|
||||||
else if (g_str_equal (command, "cat"))
|
{
|
||||||
return handle_cat (argc, argv, do_help);
|
if (g_str_equal (command, gio_subcommands[i].name))
|
||||||
else if (g_str_equal (command, "copy"))
|
{
|
||||||
return handle_copy (argc, argv, do_help);
|
g_assert (gio_subcommands[i].handle_func != NULL);
|
||||||
else if (g_str_equal (command, "info"))
|
return gio_subcommands[i].handle_func (argc, argv, do_help);
|
||||||
return handle_info (argc, argv, do_help);
|
}
|
||||||
else if (g_str_equal (command, "launch"))
|
}
|
||||||
return handle_launch (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "list"))
|
/* Unknown subcommand. */
|
||||||
return handle_list (argc, argv, do_help);
|
usage ();
|
||||||
else if (g_str_equal (command, "mime"))
|
|
||||||
return handle_mime (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "mkdir"))
|
|
||||||
return handle_mkdir (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "monitor"))
|
|
||||||
return handle_monitor (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "mount"))
|
|
||||||
return handle_mount (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "move"))
|
|
||||||
return handle_move (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "open"))
|
|
||||||
return handle_open (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "rename"))
|
|
||||||
return handle_rename (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "remove"))
|
|
||||||
return handle_remove (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "save"))
|
|
||||||
return handle_save (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "set"))
|
|
||||||
return handle_set (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "trash"))
|
|
||||||
return handle_trash (argc, argv, do_help);
|
|
||||||
else if (g_str_equal (command, "tree"))
|
|
||||||
return handle_tree (argc, argv, do_help);
|
|
||||||
else
|
|
||||||
usage ();
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user