aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerd Hoffmann <kraxel@redhat.com>2020-01-02 12:45:13 +0100
committerGerd Hoffmann <kraxel@redhat.com>2020-01-02 12:45:13 +0100
commit9ef3f868cbd8ab306fdb57e046f4bc7a90e72233 (patch)
tree06ffa7e9729bb419332b3638892943cd3e5754b0
parentad6c3b36bdc00ecee24c84c2ddf2537786cdd360 (diff)
downloaddrminfo-9ef3f868cbd8ab306fdb57e046f4bc7a90e72233.tar.gz
gtktest: long options
-rw-r--r--gtktest.c56
1 files changed, 54 insertions, 2 deletions
diff --git a/gtktest.c b/gtktest.c
index 63d76f1..12bfb22 100644
--- a/gtktest.c
+++ b/gtktest.c
@@ -1,4 +1,8 @@
#include <stdio.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <getopt.h>
+
#include <cairo.h>
#include <gtk/gtk.h>
@@ -26,13 +30,61 @@ static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
return FALSE;
}
+static void usage(FILE *fp)
+{
+ fprintf(fp,
+ "\n"
+ "usage: gtktest [ options ]\n"
+ "\n"
+ "options:\n"
+ " -h | --help print this\n"
+ " -i | --image <file> load and display image <file>\n"
+ "\n");
+}
+
+struct option long_opts[] = {
+ {
+ /* --- no argument --- */
+ .name = "help",
+ .has_arg = false,
+ .val = 'h',
+ },{
+
+ /* --- with argument --- */
+ .name = "image",
+ .has_arg = true,
+ .val = 'i',
+ },{
+ /* end of list */
+ }
+};
+
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
+ char *ifile = NULL;
+ int c;
gtk_init(&argc, &argv);
+ for (;;) {
+ c = getopt_long(argc, argv, "hi:", long_opts, NULL);
+ if (c == -1)
+ break;
+ switch (c) {
+ case 'i':
+ ifile = optarg;
+ break;
+ case 'h':
+ usage(stdout);
+ exit(0);
+ default:
+ usage(stderr);
+ exit(1);
+ }
+ }
+
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
darea = gtk_drawing_area_new();
@@ -47,9 +99,9 @@ int main(int argc, char *argv[])
gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
gtk_window_set_title(GTK_WINDOW(window), "gtktest");
- if (argv[1]) {
+ if (ifile) {
fprintf(stderr, "loading %s ...\n", argv[1]);
- image = load_image(argv[1]);
+ image = load_image(ifile);
}
gtk_widget_show_all(window);