Skip to content

fix: Return non-zero value arg_cmd_disptach does not find the sub-com… - #157

Closed
salahfh wants to merge 1 commit into
argtable:masterfrom
salahfh:master
Closed

salahfh wants to merge 1 commit into
argtable:masterfrom
salahfh:master

Conversation

@salahfh

@salahfh salahfh commented Sep 2, 2026

Copy link
Copy Markdown

Hi,

I've started learning C and experiementing wtih CLI arg parsing for small program. While using this library, I've encountered the issue arg_cmd_disptach assertion failing and not allowing the program to handle the error.

I'd like to share this PR to fix this behavior to match the documentation. Also I've included some more details in the commit message.

Please review and merge if it's correct.

Thank you :)

…mand

This change to fix the behavior of arg_cmd_disptach to match the
documentation for the function.

from argtable3.h documentation:
  If the sub-command is found, its handler function is called with the given
  arguments and context. If the sub-command is not found, **an error message may
  be written to the result buffer, and a nonzero error code is returned.**

but current behavior if command is not found is raises an assertion error.

Example of output from the multicmd example:

```
multicmd: vendored/argtable3.c:4966: arg_cmd_dispatch: Assertion `cmd_info != NULL' failed.
Aborted                    (core dumped) multicmd lit
```
@salahfh salahfh closed this Sep 2, 2026
@salahfh

salahfh commented Sep 2, 2026

Copy link
Copy Markdown
Author

The help command seems to be broken after this change.

@tomghuang

Copy link
Copy Markdown
Contributor

@salahfh: The reason why I use assert instead of checking if it is NULL, is because it does not make sense that, when you want to dispatch a command, and the command does not exit. That should never happen. The assertion failure can remind you that you forgot to register the command.

@salahfh

salahfh commented Sep 3, 2026 •

Copy link
Copy Markdown
Author

@tomghuang I've tried to work based on examples/multicmd. When running the example with invalid command, it crashes. What's approach do you suggest to validate user input?

$ ./bin/app helpp
app: vendored/argtable3.c:4961: arg_cmd_dispatch: Assertion `cmd_info != NULL' failed.
Aborted                    (core dumped) ./bin/app helpp

@tomghuang

Copy link
Copy Markdown
Contributor

@salahfh: Very good point. To prevent this, before calling arg_cmd_dispatch, you can search the command first by using arg_cmd_itr_search:

/**
 * Searches for a sub-command by key using the sub-command iterator.
 *
 * The `arg_cmd_itr_search` function searches the sub-command table for a
 * sub-command whose key matches the specified value, starting from the current
 * position of the iterator. If a match is found, the iterator is positioned at
 * the found entry.
 *
 * This function is useful for efficiently locating a specific sub-command in
 * applications that support multiple commands (such as `git` or `docker`-style
 * interfaces), especially when iterating over or managing a dynamic set of
 * sub-commands.
 *
 * Example usage:
 * ```
 * arg_cmd_itr_t itr = arg_cmd_itr_create();
 * if (arg_cmd_itr_search(itr, "list")) {
 *     arg_cmd_info_t* info = arg_cmd_itr_value(itr);
 *     printf("Found sub-command: %s - %s\n", info->name, info->description);
 * }
 * arg_cmd_itr_destroy(itr);
 * ```
 *
 * @param itr The sub-command iterator.
 * @param k   The key to search for (typically a string with the sub-command
 * name).
 * @return    1 if the key is found and the iterator is positioned at the entry,
 *            0 otherwise.
 *
 * @see arg_cmd_itr_create, arg_cmd_itr_value, arg_cmd_info_t
 */
ARG_EXTERN int arg_cmd_itr_search(arg_cmd_itr_t itr, void* k);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants