varch/doc/arg.en.md

54 lines
1.4 KiB
Markdown

## Introduction
The `arg` module is primarily designed for scenarios that involve variable numbers of arguments, allowing you to retrieve the count of those arguments and access specific positional arguments.
## Interface
### Functions
```c
#define ARG_MAX // Maximum number of supported variable arguments
#define ARGC(...) // Current number of variable arguments, does not support distinguishing empty arguments
#define ARGC0(...) // Current number of variable arguments, supports distinguishing empty arguments but requires more compile-time space; use as needed
#define ARGS(x, ...) // Retrieve the argument at a specified position
```
In usage, you only need to focus on the macro definitions above; the rest can be ignored.
Example usage:
```c
static void test(void)
{
printf("ARG_MAX %d\r\n", ARG_MAX);
printf("ARGC %d\r\n", ARGC());
printf("ARGC %d\r\n", ARGC(A));
printf("ARGC %d\r\n", ARGC(A, B));
printf("ARGC %d\r\n", ARGC(A, B, C));
printf("ARGC %d\r\n", ARGC0());
printf("ARGC %d\r\n", ARGC0(A));
printf("ARGC %d\r\n", ARGC0(A, B));
printf("ARGC %d\r\n", ARGC0(A, B, C));
printf("ARGS %s\r\n", ARGS(0, "arg0", "arg1", "arg2"));
printf("ARGS %s\r\n", ARGS(1, "arg0", "arg1", "arg2"));
printf("ARGS %s\r\n", ARGS(2, "arg0", "arg1", "arg2"));
}
```
Results:
```
ARG_MAX 124
ARGC 1
ARGC 1
ARGC 2
ARGC 3
ARGC 0
ARGC 1
ARGC 2
ARGC 3
ARGS arg0
ARGS arg1
ARGS arg2
```