diff options
author | Gerd Hoffmann <kraxel@redhat.com> | 2020-01-02 12:45:13 +0100 |
---|---|---|
committer | Gerd Hoffmann <kraxel@redhat.com> | 2020-01-02 12:45:13 +0100 |
commit | 9ef3f868cbd8ab306fdb57e046f4bc7a90e72233 (patch) | |
tree | 06ffa7e9729bb419332b3638892943cd3e5754b0 | |
parent | ad6c3b36bdc00ecee24c84c2ddf2537786cdd360 (diff) | |
download | drminfo-9ef3f868cbd8ab306fdb57e046f4bc7a90e72233.tar.gz |
gtktest: long options
-rw-r--r-- | gtktest.c | 56 |
1 files changed, 54 insertions, 2 deletions
@@ -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); |