]> git.itanic.dy.fi Git - rrdd/commitdiff
main: Implement command line parsing
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 25 Apr 2012 15:22:34 +0000 (18:22 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 25 Apr 2012 15:22:34 +0000 (18:22 +0300)
The first command line option we support is the number of jobs to run
in parallel.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
main.c

diff --git a/main.c b/main.c
index cde3010aa6c4e523ecc48aec7bcbb172153d5cff..d0ba0adbff76e8006215f3984ebb8601bcbe28db 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,5 +1,6 @@
 #include <sys/types.h>
 #include <unistd.h>
+#include <getopt.h>
 
 #include "process.h"
 #include "rrdtool.h"
 
 #include "database.h"
 
+struct user_options {
+       int max_jobs;
+};
+
+int read_args(int argc, char *argv[], struct user_options *opts)
+{
+       int option_index = 0, c;
+       static struct option long_options[] = {
+               { .val = 'j', .name = "jobs", .has_arg = 1, },
+       };
+       char short_options[] = "j:";
+
+       while (1) {
+               c = getopt_long(argc, argv, short_options, long_options,
+                               &option_index);
+
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 'j':
+                       opts->max_jobs = atoi(optarg);
+                       break;
+
+               case '?':
+                       return -1;
+               }
+       }
+       return 0;
+}
 
 int main(int argc, char *argv[])
 {
+       struct user_options opts;
        struct rrd_database *db;
        int sleeptime;
 
+       bzero(&opts, sizeof(opts));
+
+       if (read_args(argc, argv, &opts) < 0)
+               return -1;
+
        rrdtool_create_missing_databases(all_rrds);
 
-       init_max_jobs(0);
+       init_max_jobs(opts.max_jobs);
 
        while (1) {
                pr_info("loop start\n");