Skip to content

Commit fb45a11

Browse files
committed
- Fixed a segfault when commands are registrated but argument list is empty.
- Fixed build process.
1 parent ff9ddbc commit fb45a11

File tree

6 files changed

+463
-330
lines changed

6 files changed

+463
-330
lines changed

.vimrc

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
" Plugin 'altercation/vim-colors-solarized'
22

3+
Plugin 'vhdirk/vim-cmake'
4+
5+
let g:cmake_build_type = 'debug'
6+
37
let g:solarized_termcolors=256
48
syntax enable
59
set background=dark
610
colorscheme solarized
711

8-
" syntastic settings
12+
" Autoformat
913

10-
set statusline+=%#warningmsg#
11-
set statusline+=%{SyntasticStatuslineFlag()}
12-
set statusline+=%*
14+
au BufWrite * :Autoformat
15+
16+
let g:autoformat_autoindent = 0
17+
let g:autoformat_retab = 0
18+
let g:autoformat_remove_trailing_spaces = 0
19+
let g:formatdef_astyle_google_cpp = '"astyle --style=google"'
20+
let g:formatters_cpp = ['astyle_google_cpp']
21+
22+
" syntastic settings
1323

24+
"set statusline+=%#warningmsg#
25+
"set statusline+=%{SyntasticStatuslineFlag()}
26+
"set statusline+=%*
1427

1528
let g:sytastic_c_checkers = ['gcc']
1629
let g:syntastic_always_populate_loc_list = 1
@@ -21,7 +34,10 @@ let g:syntastic_check_on_wq = 0
2134
let g:syntastic_c_include_dirs = [ 'src' ]
2235
let g:syntastic_c_check_header = 1
2336

24-
let g:syntastic_cpp_include_dirs = [ 'src' ]
37+
let g:syntastic_cpp_compile = 'cmake'
38+
let g:syntastic_cpp_checkers = ['gcc']
39+
let g:syntastic_cpp_include_dirs = [ 'system/include', 'axis/include', 'common/include', 'tools/include', 'Debug/gtest-install/include', 'Debug/sap_1_0_latest-install/include' ]
40+
let g:syntastic_cpp_compiler_options = '-std=c++11 -Wall'
2541
let g:syntastic_cpp_check_header = 1
2642

2743

CMakeLists.txt

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
cmake_minimum_required (VERSION 2.8.7)
22
project (SimpleArgumentParser C CXX)
33

4-
set (SimpleArgumentParser_VERSION_MAJOR 0)
5-
set (SimpleArgumentParser_VERSION_MINOR 1)
4+
set (SimpleArgumentParser_VERSION_MAJOR 1)
5+
set (SimpleArgumentParser_VERSION_MINOR 0)
6+
set (SimpleArgumentParser_VERSION_PATCH 6)
67

7-
option(WITH-TESTS "Compile with test cases." OFF)
8-
option(STATIC "Compile shared/static library." "SHARED")
8+
option(WITH_TESTS "Compile with test cases." "OFF")
9+
option(SHARED "Compile shared/static library." "OFF")
910

10-
IF (STATIC)
11-
add_library(sap STATIC "${CMAKE_SOURCE_DIR}/src/sap.c" "${CMAKE_SOURCE_DIR}/src/sap.h")
12-
ELSE()
11+
IF (SHARED)
1312
add_library(sap SHARED "${CMAKE_SOURCE_DIR}/src/sap.c" "${CMAKE_SOURCE_DIR}/src/sap.h")
13+
ELSE()
14+
add_library(sap STATIC "${CMAKE_SOURCE_DIR}/src/sap.c" "${CMAKE_SOURCE_DIR}/src/sap.h")
1415
ENDIF()
1516

1617
set(CMAKE_C_FLAGS "--std=gnu99" ${CMAKE_C_FLAGS})
@@ -19,9 +20,14 @@ option(WITH-TESTS "Compile the test application." OFF)
1920

2021
include_directories(${PROJECT_SOURCE_DIR}/src)
2122

22-
add_executable(example-01 ${CMAKE_SOURCE_DIR}/examples/example_01.c ${CMAKE_SOURCE_DIR}/src/sap.c ${CMAKE_SOURCE_DIR}/src/sap.h)
23+
add_executable(example-01
24+
${CMAKE_SOURCE_DIR}/examples/example_01.c
25+
${CMAKE_SOURCE_DIR}/src/sap.c
26+
${CMAKE_SOURCE_DIR}/src/sap.h)
2327

24-
IF(WITH-TESTS)
28+
IF(WITH_TESTS)
29+
include(ExternalProject)
30+
include(thirdparty/googletest.cmake)
2531
add_subdirectory(${PROJECT_SOURCE_DIR}/tests)
2632
endif()
2733

examples/example_01.c

+41-23
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,60 @@
11

22
#include "sap.h"
33

4+
static int test_handler(sap_command_list_t* command, sap_option_list_t* option)
5+
{
46

5-
static int default_handler(sap_command_list_t* commands, sap_option_list_t* options) {
7+
printf("Executing the test handler\n");
8+
return 0;
69

7-
sap_command_t* cc = commands->first;
8-
while (cc) {
9-
printf("Command %s\n", cc->label);
10-
cc = cc->next;
11-
}
10+
}
11+
12+
static int default_handler(sap_command_list_t* commands, sap_option_list_t* options)
13+
{
14+
15+
printf("Executing the default handler.\n");
1216

13-
unsigned int curIndex = 0;
14-
sap_option_t* co = sap_get_option_by_index(options, curIndex);
15-
while (co) {
16-
if (co->is_flag) {
17-
printf("flag %s\n", co->label);
18-
} else {
19-
printf("option %s with value %s.\n", co->label, co->value);
20-
}
17+
sap_command_t* cc = commands->first;
18+
while (cc)
19+
{
20+
printf("Command %s\n", cc->label);
21+
cc = cc->next;
22+
}
2123

22-
co = sap_get_option_by_index(options, ++curIndex);
24+
unsigned int curIndex = 0;
25+
sap_option_t* co = sap_get_option_by_index(options, curIndex);
26+
while (co)
27+
{
28+
if (co->is_flag)
29+
{
30+
printf("flag %s\n", co->label);
31+
}
32+
else
33+
{
34+
printf("option %s with value %s.\n", co->label, co->value);
35+
}
2336

24-
}
37+
co = sap_get_option_by_index(options, ++curIndex);
38+
39+
}
2540

2641
}
2742

28-
int main(int argc, char** argv) {
43+
int main(int argc, char** argv)
44+
{
45+
46+
sap_t parser;
2947

30-
sap_t parser;
48+
sap_init(&parser, argc, argv);
3149

32-
sap_init(&parser, argc, argv);
50+
sap_set_default(&parser, default_handler);
3351

34-
sap_set_default(&parser, default_handler);
52+
sap_add_command(&parser, "test", test_handler);
3553

36-
int retVal = sap_execute(&parser);
54+
int retVal = sap_execute(&parser);
3755

38-
sap_free(&parser);
56+
sap_free(&parser);
3957

40-
return retVal;
58+
return retVal;
4159

4260
}

0 commit comments

Comments
 (0)