Skip to content

Commit 8cc83fc

Browse files
committed
docs: add inline comments explaining CLI argument parsing logic
1 parent effb07a commit 8cc83fc

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Cli/args.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
static int validate_args(const cli_args_t *opts);
1010
static void print_usage(const char *program_name);
1111

12+
// Table-driven mapping of CLI flags to their handler functions.
13+
// This allows parsing logic to remain generic and extensible.
1214
typedef struct
1315
{
1416
const char *flag;
@@ -36,6 +38,10 @@ int parse_args(int argc, char *argv[], cli_args_t *opts)
3638
memset(opts, 0, sizeof(cli_args_t));
3739
opts->encrypt_mode = -1;
3840

41+
42+
// Iterate over argv, attempting to match each argument to a known flag.
43+
// If a flag matches, its handler is invoked, which may consume additional arguments.
44+
// Unknown flags result in an error and early exit.
3945
for (int i = 1; i < argc; ++i)
4046
{
4147
int matched = 0;
@@ -46,6 +52,8 @@ int parse_args(int argc, char *argv[], cli_args_t *opts)
4652
continue;
4753
}
4854

55+
// Argument index 'i' is passed by pointer so the handler can
56+
// consume additional arguments (e.g., -k <key>) and advance 'i'.
4957
if (opt->handler(argc, argv, &i, opts))
5058
{
5159
return EXIT_FAILURE;

src/cli/args.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
static int validate_args(const cli_args_t *opts);
1010
static void print_usage(const char *program_name);
1111

12+
// Table-driven mapping of CLI flags to their handler functions.
13+
// This allows parsing logic to remain generic and extensible.
1214
typedef struct
1315
{
1416
const char *flag;
@@ -36,6 +38,10 @@ int parse_args(int argc, char *argv[], cli_args_t *opts)
3638
memset(opts, 0, sizeof(cli_args_t));
3739
opts->encrypt_mode = -1;
3840

41+
42+
// Iterate over argv, attempting to match each argument to a known flag.
43+
// If a flag matches, its handler is invoked, which may consume additional arguments.
44+
// Unknown flags result in an error and early exit.
3945
for (int i = 1; i < argc; ++i)
4046
{
4147
int matched = 0;
@@ -46,6 +52,8 @@ int parse_args(int argc, char *argv[], cli_args_t *opts)
4652
continue;
4753
}
4854

55+
// Argument index 'i' is passed by pointer so the handler can
56+
// consume additional arguments (e.g., -k <key>) and advance 'i'.
4957
if (opt->handler(argc, argv, &i, opts))
5058
{
5159
return EXIT_FAILURE;

0 commit comments

Comments
 (0)