Add example for command line parser [skip-ci]

This commit is contained in:
Alex Kotov 2020-12-06 03:58:34 +05:00
parent f69d86f813
commit 776e657b84
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 43 additions and 0 deletions

1
.gitignore vendored
View File

@ -30,6 +30,7 @@
/tests/test*.log
/tests/test*.trs
/examples/cmdline
/tests/multiboot2_print1
/tests/multiboot2_print2
/tests/test_cmdline

View File

@ -14,6 +14,7 @@ TESTS = \
noinst_PROGRAMS = \
$(TESTS) \
examples/cmdline \
tests/multiboot2_print1 \
tests/multiboot2_print2
@ -30,6 +31,10 @@ if ARCH_X86
libkernaux_a_SOURCES += src/arch/x86.S
endif
examples_cmdline_SOURCES = \
$(libkernaux_a_SOURCES) \
examples/cmdline.c
tests_multiboot2_print1_SOURCES = \
$(libkernaux_a_SOURCES) \
tests/multiboot2_print1.c

37
examples/cmdline.c Normal file
View File

@ -0,0 +1,37 @@
#include <kernaux/cmdline.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
static const unsigned int ARGV_COUNT_MAX = 100;
static const unsigned int ARG_SIZE_MAX = 4096;
static const char *const cmdline = "foo bar\\ car";
int main()
{
char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX];
unsigned int argc;
char *argv[ARGV_COUNT_MAX];
char buffer[ARGV_COUNT_MAX * ARG_SIZE_MAX];
assert(kernaux_cmdline_parse(
cmdline,
error_msg,
&argc,
argv,
buffer,
ARGV_COUNT_MAX,
ARG_SIZE_MAX
));
assert(strcmp(error_msg, "") == 0);
assert(argc == 2);
assert(strcmp(argv[0], "foo") == 0);
assert(strcmp(argv[1], "bar car") == 0);
printf("OK!\n");
return 0;
}