]> git.draconx.ca Git - upkg.git/blobdiff - src/upkg.c
upkg: Add gnulib getopt_long support and implement --help/--version.
[upkg.git] / src / upkg.c
index 8c26d27152ea489cf905d2011712ca206456706c..61f67a48f14a08c18b23c629ef7b0a0d01a85217 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <getopt.h>
+
 #include <glib-object.h>
 
 #include "upkg.h"
 #include "uobject.h"
 #include "exportable.h"
 
+static const char *progname = "upkg";
+
+static const char *sopts = "VH";
+static const struct option lopts[] = {
+       { "version",  0, NULL, 'V' },
+       { "help",     0, NULL, 'H' },
+       { 0 }
+};
+
+void print_version(void)
+{
+       printf("%s\n", PACKAGE_STRING);
+       puts("\
+Copyright (C) 2009 Nick Bowler.\n\
+This is free software: you are free to change and redistribute it.\n\
+There is NO WARRANTY, to the extent permitted by law."
+       );
+}
+
+void print_usage(void)
+{
+       printf("Usage: %s [options] package\n", progname);
+}
+
+void print_help(void)
+{
+       print_usage();
+       puts("Detailed help coming soon.  Until then, I'll just list my options.");
+       for (unsigned i = 0; lopts[i].name; i++) {
+               const struct option *o = &lopts[i];
+               printf("\t--%s", o->name);
+
+               if (o->has_arg == 1) {
+                       printf("=val");
+               } else if (o->has_arg == 2) {
+                       printf("[=val]");
+               }
+
+               printf(" (-%c)\n", o->val);
+       }
+}
+
 void print_upkg_flags(const char *prefix, unsigned long flags)
 {
        if (flags & UPKG_FLAG_ALLOW_DOWNLOAD)
@@ -56,6 +101,23 @@ void print_guid(unsigned char guid[static 16])
 int main(int argc, char **argv)
 {
        struct upkg *pkg;
+       int opt;
+
+       if (argc > 0) progname = argv[0];
+
+       while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
+               switch (opt) {
+               case 'V':
+                       print_version();
+                       return EXIT_SUCCESS;
+               case 'H':
+                       print_help();
+                       return EXIT_SUCCESS;
+               default:
+                       print_usage();
+                       return EXIT_FAILURE;
+               }
+       }
 
        if (argc < 2) {
                fprintf(stderr, "usage: upkg file\n");