[gnome-desktop/wip/hadess/thumbnail-test-stats: 3/6] thumbnail: Use GOption to parse command-line args in test




commit 2cac8672262831dd0d246a74661d812f45637b35
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Oct 14 11:51:29 2020 +0200

    thumbnail: Use GOption to parse command-line args in test

 libgnome-desktop/test-desktop-thumbnail.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
---
diff --git a/libgnome-desktop/test-desktop-thumbnail.c b/libgnome-desktop/test-desktop-thumbnail.c
index 0ded2030..6a03425d 100644
--- a/libgnome-desktop/test-desktop-thumbnail.c
+++ b/libgnome-desktop/test-desktop-thumbnail.c
@@ -21,6 +21,8 @@
  * Authors: Bastien Nocera <hadess hadess net>
  */
 
+#include <locale.h>
+
 #define GNOME_DESKTOP_USE_UNSTABLE_API
 #include <libgnome-desktop/gnome-desktop-thumbnail.h>
 
@@ -62,14 +64,35 @@ thumbnail_file (GnomeDesktopThumbnailFactory *factory,
 int main (int argc, char **argv)
 {
        g_autoptr(GnomeDesktopThumbnailFactory) factory = NULL;
+       char **filenames = NULL;
+       int ret = 0;
+       g_autoptr(GOptionContext) option_context = NULL;
+       g_autoptr(GError) error = NULL;
+       const GOptionEntry options[] = {
+               { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, "[INPUT FILE] [OUTPUT 
FILE]", NULL },
+               { NULL}
+       };
+
+       setlocale (LC_ALL, "");
+       option_context = g_option_context_new ("");
+       g_option_context_add_main_entries (option_context, options, NULL);
+
+       ret = g_option_context_parse (option_context, &argc, &argv, &error);
+       if (!ret) {
+               g_print ("Failed to parse arguments: %s", error->message);
+               return EXIT_FAILURE;
+       }
 
-       if (argc != 3) {
-               g_print ("Usage: %s [INPUT FILE] [OUTPUT FILE]\n", argv[0]);
+       if (filenames == NULL ||
+           g_strv_length (filenames) != 2) {
+               g_autofree char *help = NULL;
+               help = g_option_context_get_help (option_context, TRUE, NULL);
+               g_print ("%s", help);
                return 1;
        }
 
        factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE);
-       if (!thumbnail_file (factory, argv[1], argv[2]))
+       if (!thumbnail_file (factory, filenames[0], filenames[1]))
                return 1;
 
        return 0;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]