]> git.notmuchmail.org Git - notmuch/commitdiff
cli: change argument parsing convention for subcommands
authorDavid Bremner <bremner@debian.org>
Fri, 21 Oct 2011 12:19:17 +0000 (09:19 -0300)
committerDavid Bremner <bremner@debian.org>
Sat, 22 Oct 2011 22:42:54 +0000 (19:42 -0300)
previously we deleted the subcommand name from argv before passing to
the subcommand. In this version, the deletion is done in the actual
subcommands. Although this causes some duplication of code, it allows
us to be more flexible about how we parse command line arguments in
the subcommand, including possibly using off-the-shelf routines like
getopt_long that expect the name of the command in argv[0].

notmuch-config.c
notmuch-count.c
notmuch-dump.c
notmuch-new.c
notmuch-reply.c
notmuch-restore.c
notmuch-search.c
notmuch-show.c
notmuch-tag.c
notmuch.c

index 485fa72a759b5e61f74dfefb11d4a56f58a3b971..1a7ed580ec290c85881871c434b762a03bc87b92 100644 (file)
@@ -735,6 +735,8 @@ notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])
 int
 notmuch_config_command (void *ctx, int argc, char *argv[])
 {
+    argc--; argv++; /* skip subcommand argument */
+
     if (argc < 2) {
        fprintf (stderr, "Error: notmuch config requires at least two arguments.\n");
        return 1;
index 39f08c6ca94476757c5591c7a171c74c16875018..0d700a9e12f8205fbe92f3309cb162dd7cc15d95 100644 (file)
@@ -35,6 +35,8 @@ notmuch_count_command (void *ctx, int argc, char *argv[])
     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
 #endif
 
+    argc--; argv++; /* skip subcommand argument */
+
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
        if (strcmp (argv[i], "--") == 0) {
            i++;
index 409c86ada6dd709461da7b20e7732a3a345a3880..126593d177d50f5b84f0d84636ae015853fe7298 100644 (file)
@@ -41,6 +41,8 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
     if (notmuch == NULL)
        return 1;
 
+    argc--; argv++; /* skip subcommand argument */
+
     if (argc && strcmp (argv[0], "--") != 0) {
        fprintf (stderr, "Warning: the output file argument of dump is deprecated.\n");
        output = fopen (argv[0], "w");
index 96a1e31e732da0b4d16cbdc374848d810904eb96..81a93500ced96e971df3562a3730e911f81819e1 100644 (file)
@@ -815,6 +815,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     add_files_state.verbose = 0;
     add_files_state.output_is_a_tty = isatty (fileno (stdout));
 
+    argc--; argv++; /* skip subcommand argument */
+
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
        if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
            add_files_state.verbose = 1;
index 8f23cea23129108e7abf3985950f65bb43eed225..7ac879f94419aa4d7783a0cb6d0be8912baad79b 100644 (file)
@@ -628,6 +628,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
     params.part = -1;
     params.cryptoctx = NULL;
 
+    argc--; argv++; /* skip subcommand argument */
+
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
        if (strcmp (argv[i], "--") == 0) {
            i++;
index e4a5355beb4f2e5fd9e1eb12ef7dd9816d292951..f530bb4ed9388833e91a70cecd321c9daeb521d2 100644 (file)
@@ -44,6 +44,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 
     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
 
+    argc--; argv++; /* skip subcommand argument */
+
     if (argc) {
        input = fopen (argv[0], "r");
        if (input == NULL) {
index faccaf7deec10961f5f2c2c7bdc5fa9bef4521f7..6f04d9a45d719a37d7c17056d3a3e623560232db 100644 (file)
@@ -395,6 +395,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     int i, ret;
     output_t output = OUTPUT_SUMMARY;
 
+    argc--; argv++; /* skip subcommand argument */
+
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
        if (strcmp (argv[i], "--") == 0) {
            i++;
index 912999e1a65e645b7b893ac49d1df55ad197d51c..603992a612448c8892888580a4fae68f5aa123dc 100644 (file)
@@ -936,6 +936,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     params.cryptoctx = NULL;
     params.decrypt = 0;
 
+    argc--; argv++; /* skip subcommand argument */
+
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
        if (strcmp (argv[i], "--") == 0) {
            i++;
index 6204ae3cefcaddb78051a8b5955ec642db596b08..dded39ea5df645799eced2657fa4db1ee20bbe52 100644 (file)
@@ -65,6 +65,8 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
        return 1;
     }
 
+    argc--; argv++; /* skip subcommand argument */
+
     for (i = 0; i < argc; i++) {
        if (strcmp (argv[i], "--") == 0) {
            i++;
index 640ad8da56d2a112a79a3a645b03d4dce91d18f7..4232bee7c4e97a93b9ccb81fbc4500c8b3cbbd53 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -465,6 +465,8 @@ notmuch_help_command (unused (void *ctx), int argc, char *argv[])
     command_t *command;
     unsigned int i;
 
+    argc--; argv++; /* Ignore "help" */
+
     if (argc == 0) {
        printf ("The notmuch mail system.\n\n");
        usage (stdout);
@@ -639,7 +641,7 @@ main (int argc, char *argv[])
        command = &commands[i];
 
        if (strcmp (argv[1], command->name) == 0)
-           return (command->function) (local, argc - 2, &argv[2]);
+           return (command->function) (local, argc - 1, &argv[1]);
     }
 
     fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",