varch/test/test_arg.c
2024-11-16 13:36:54 +08:00

84 lines
1.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_arg)
#include <varch/command.h>
#include <varch/arg.h>
#else
#include "init.h"
#include "command.h"
#include "arg.h"
#endif
static void usage(void)
{
printf(
"Usage: arg [opt] [arg]\n"
"\n"
"options:\n"
" -h Print help\n"
" -v Print version\n"
"\n"
"argument:\n"
" <read> Test default read function\n"
" <write> Test default write function\n"
);
}
static int test(int argc, char *argv[])
{
/* reset getopt */
command_opt_init();
while (1)
{
int opt = command_getopt(argc, argv, "hv");
if (opt == -1) break;
switch (opt)
{
case 'v' :
printf("arg version %d.%d.%d\r\n", ARG_V_MAJOR, ARG_V_MINOR, ARG_V_PATCH);
return 0;
case '?':
printf("Unknown option `%c`\r\n", command_optopt);
return -1;
case 'h' :
default:
usage();
return 0;
}
}
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"));
return 0;
}
#if defined(TEST_TARGET_arg)
int main(int argc, char *argv[])
{
return test(argc, argv);
}
#else
void test_arg(void)
{
command_export("arg", test);
}
init_export_app(test_arg);
#endif