cmdline: display help even if iterate() callback return -1
[libcmdline.git] / src / lib / cmdline_parse.c
index dfc17f1..2822dcb 100644 (file)
@@ -470,7 +470,7 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
        char helpbuf[CMDLINE_MAX_DSTBUF_SIZE];
        char tmpbuf[CMDLINE_MAX_DSTBUF_SIZE];
        char *help_str;
-       int n, len, iterate;
+       int n, len, iterate, iterate_count;
 
        cmdline_preparse(&preparse, buf);
 
@@ -512,29 +512,10 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
                                                         &preparse.opaque);
                }
 
-               if (iterate == 0) {
-                       /* get token dynamic help string */
-                       if ((token->ops->help == NULL) ||
-                           (token->ops->help(token, tmpbuf, sizeof(tmpbuf)) < 0))
-                               snprintf(tmpbuf, sizeof(tmpbuf), "unknown");
-
-                       /* get instruction static help string */
-                       help_str = inst->help_str;
-                       if (help_str == NULL)
-                               help_str = "No help";
-
-                       /* send it to callback function */
-                       n = snprintf(helpbuf, sizeof(helpbuf),
-                                    "[%s]: %s\n", tmpbuf, help_str);
-                       if (n >= 0)
-                               write_buf(opaque, helpbuf, n);
-
-                       continue;
-               }
-
-
                /* iterate over all possible completion for this inst */
-               while (token->ops->complete_iterate(token,
+               iterate_count = 0;
+               while (iterate == 1 &&
+                      token->ops->complete_iterate(token,
                                                    &preparse.opaque,
                                                    tmpbuf,
                                                    sizeof(tmpbuf)) == 0) {
@@ -554,6 +535,8 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
                                continue;
                        len = n;
 
+                       iterate_count++;
+
                        /* get token dynamic help */
                        if ((token->ops->help == NULL) ||
                            (token->ops->help(token, tmpbuf, sizeof(tmpbuf)) < 0))
@@ -572,8 +555,28 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
                }
 
                /* no more completion, go to next inst */
-               if (token->ops->complete_end != NULL)
+               if (iterate == 1 && token->ops->complete_end != NULL)
                        token->ops->complete_end(token, &preparse.opaque);
+
+               if (iterate == 0 || iterate_count == 0) {
+                       /* get token dynamic help string */
+                       if ((token->ops->help == NULL) ||
+                           (token->ops->help(token, tmpbuf, sizeof(tmpbuf)) < 0))
+                               snprintf(tmpbuf, sizeof(tmpbuf), "unknown");
+
+                       /* get instruction static help string */
+                       help_str = inst->help_str;
+                       if (help_str == NULL)
+                               help_str = "No help";
+
+                       /* send it to callback function */
+                       n = snprintf(helpbuf, sizeof(helpbuf),
+                                    "[%s]: %s\n", tmpbuf, help_str);
+                       if (n >= 0)
+                               write_buf(opaque, helpbuf, n);
+
+                       continue;
+               }
        }
 
        return 0;