Skip to content

Commit c15296a

Browse files
committed
Fixing unexecuted validators in subcommand
1 parent 5f116ec commit c15296a

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
### Fixed
1919
- Disabled pedantic option on library compilation.
2020
- Defining `argus_init()` as a `static inline` function.
21+
- Fixed unexecuted validators in subcommand.
2122

2223
### Removed
2324
- Group description option.

source/core/parsing/post_parse_validation.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ static int validate_conflicts(argus_t *argus, argus_option_t *options, argus_opt
4747

4848
static int call_validators(argus_t *argus, argus_option_t *option)
4949
{
50-
if (!option->validators) {
50+
if (!option->validators)
5151
return ARGUS_SUCCESS;
52-
}
5352

5453
for (size_t i = 0; option->validators[i] != NULL; ++i) {
5554
validator_entry_t *validator = option->validators[i];
@@ -122,5 +121,12 @@ int post_parse_validation(argus_t *argus)
122121
if (status != ARGUS_SUCCESS)
123122
return status;
124123

124+
for (size_t i = 0; i < argus->subcommand_depth; ++i) {
125+
const argus_option_t *subcommand = argus->subcommand_stack[i];
126+
status = validate_options_set(argus, (argus_option_t *)subcommand->sub_options);
127+
if (status != ARGUS_SUCCESS)
128+
return (status);
129+
}
130+
125131
return status;
126132
}

tests/integration/test_subcommand_edge_case.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ARGUS_OPTIONS(
1919
foo_options,
2020
HELP_OPTION(),
2121
OPTION_FLAG('v', "verbose", HELP("Enable verbose output")),
22-
POSITIONAL_INT("value", HELP("A numerical value")),
22+
POSITIONAL_INT("value", HELP("A numerical value"), VALIDATOR(V_RANGE(-50, 50))),
2323
)
2424

2525
ARGUS_OPTIONS(
@@ -238,6 +238,21 @@ Test(subcommand_edge, subcommand_negative_number, .init = setup_subcommand)
238238
argus_free(&argus);
239239
}
240240

241+
// Test Validator invalid value in subcommands
242+
Test(subcommand_edge, subcommand_invalid_value, .init = setup_subcommand)
243+
{
244+
char *argv[] = {"test", "nested", "foo", "100"};
245+
int argc = sizeof(argv) / sizeof(char *);
246+
247+
argus_t argus = argus_init(cmd_options, "test", "1.0.0");
248+
int status = argus_parse(&argus, argc, argv);
249+
250+
cr_assert_neq(status, ARGUS_SUCCESS, "Subcommand with invalid number should fail");
251+
cr_assert(argus_has_command(&argus), "argus_has_command should return true");
252+
253+
argus_free(&argus);
254+
}
255+
241256
// Helper function for format tests
242257
void setup_error_redirect(void)
243258
{

0 commit comments

Comments
 (0)