]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: Improve parameter checking and usage in cgconfigparser
authorDhaval Giani <dhaval@linux.vnet.ibm.com>
Tue, 9 Dec 2008 11:14:24 +0000 (11:14 +0000)
committerDhaval Giani <dhaval@linux.vnet.ibm.com>
Tue, 9 Dec 2008 11:14:24 +0000 (11:14 +0000)
From: Sripathi Kodi <sripathik@in.ibm.com>

The following patch improves cgconfigparser's parameter checking and
usage(). It provides:

1) Support for long options. Not really needed now, but good to have.
2) Improved usage() functionality.

Signed-off-by: Sripathi Kodi <sripathik@in.ibm.com>
Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com>
git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@227 4f4bb910-9a46-0410-90c8-c897d4f1cd53

cgconfig.c

index 16ff63b8eeb73a9de598a5abe24145bb037aa47a..fab3a720239141a7c8b1bd6ab059fe9cc18a77d5 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <getopt.h>
+
+
+static void usage(char *progname)
+{
+       printf("Usage: %s [OPTION] [FILE]\n", basename(progname));
+       printf("Parse and load the specified cgroups configuration file\n");
+       printf("\n");
+       printf("  -h, --help            Display this help\n");
+       printf("  -l, --load=FILE       Parse and load the cgroups configuration file\n");
+       exit(2);
+}
 
 int main(int argc, char *argv[])
 {
        int c;
        char filename[PATH_MAX];
        int ret;
+       static struct option options[] = {
+               {"help", 0, 0, 'h'},
+               {"load", 1, 0, 'l'},
+               {0, 0, 0, 0}
+       };
 
-       if (argc < 2) {
-               fprintf(stderr, "usage is %s <option> <config file>\n",
-                       argv[0]);
-               exit(2);
-       }
+       if (argc < 2)
+               usage(argv[0]); /* usage() exits */
 
-       while ((c = getopt(argc, argv, "l:")) > 0) {
+       while ((c = getopt_long(argc, argv, "hl:", options, NULL)) > 0) {
                switch (c) {
+               case 'h':
+                       usage(argv[0]);
+                       break;
                case 'l':
                        strncpy(filename, optarg, PATH_MAX);
                        ret = cgroup_config_load_config(filename);
@@ -50,11 +67,9 @@ int main(int argc, char *argv[])
                        }
                        return 0;
                default:
-                       fprintf(stderr, "Invalid command line option\n");
+                       usage(argv[0]);
                        break;
                }
        }
-       fprintf(stderr, "usage is %s <option> <config file>\n",
-               argv[0]);
        return 0;
 }