From 21f37259d1720478846bbd72be78d886fdaf9656 Mon Sep 17 00:00:00 2001 From: jingyuexing Date: Tue, 11 May 2021 18:53:02 +0800 Subject: [PATCH 1/4] * add cmake file --- .gitignore | 3 +++ CMakeLists.txt | 4 ++++ src/commander.c | 30 ++++++++++-------------------- src/commander.h | 17 ++++++----------- test.c | 12 ++++-------- 5 files changed, 27 insertions(+), 39 deletions(-) create mode 100644 CMakeLists.txt diff --git a/.gitignore b/.gitignore index 76758b0..f5e6893 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ *.o test +.exe +build/ +.vscode/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..82bcd94 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 2.8) +project(commander) +aux_source_directory(${PROJECT_SOURCE_DIR}/src commander_source) +add_library(commander SHARED ${commander_source}) \ No newline at end of file diff --git a/src/commander.c b/src/commander.c index 8298d1a..cef04aa 100644 --- a/src/commander.c +++ b/src/commander.c @@ -15,8 +15,7 @@ * Output error and exit. */ -static void -error(char *msg) { +static void error(char *msg) { fprintf(stderr, "%s\n", msg); exit(1); } @@ -25,8 +24,7 @@ error(char *msg) { * Output command version. */ -static void -command_version(command_t *self) { +static void command_version(command_t *self) { printf("%s\n", self->version); command_free(self); exit(0); @@ -36,8 +34,7 @@ command_version(command_t *self) { * Output command help. */ -void -command_help(command_t *self) { +void command_help(command_t *self) { printf("\n"); printf(" Usage: %s %s\n", self->name, self->usage); printf("\n"); @@ -62,8 +59,7 @@ command_help(command_t *self) { * Initialize with program `name` and `version`. */ -void -command_init(command_t *self, const char *name, const char *version) { +void command_init(command_t *self, const char *name, const char *version) { self->arg = NULL; self->name = name; self->version = version; @@ -78,8 +74,7 @@ command_init(command_t *self, const char *name, const char *version) { * Free up commander after use. */ -void -command_free(command_t *self) { +void command_free(command_t *self) { int i; for (i = 0; i < self->option_count; ++i) { @@ -102,8 +97,7 @@ command_free(command_t *self) { * with "--required" and `arg` with "". */ -static void -parse_argname(const char *str, char *flag, char *arg) { +static void parse_argname(const char *str, char *flag, char *arg) { int buffer = 0; size_t flagpos = 0; size_t argpos = 0; @@ -130,8 +124,7 @@ parse_argname(const char *str, char *flag, char *arg) { * "foo -abc --scm git" -> "foo -a -b -c --scm git" */ -static char ** -normalize_args(int *argc, char **argv) { +static char ** normalize_args(int *argc, char **argv) { int size = 0; int alloc = *argc + 1; char **nargv = malloc(alloc * sizeof(char *)); @@ -168,8 +161,7 @@ normalize_args(int *argc, char **argv) { * Define an option. */ -void -command_option(command_t *self, const char *small, const char *large, const char *desc, command_callback_t cb) { +void command_option(command_t *self, const char *small, const char *large, const char *desc, command_callback_t cb) { if (self->option_count == COMMANDER_MAX_OPTIONS) { command_free(self); error("Maximum option definitions exceeded"); @@ -196,8 +188,7 @@ command_option(command_t *self, const char *small, const char *large, const char * see `normalize_args`. */ -static void -command_parse_args(command_t *self, int argc, char **argv) { +static void command_parse_args(command_t *self, int argc, char **argv) { int literal = 0; int i, j; @@ -261,8 +252,7 @@ command_parse_args(command_t *self, int argc, char **argv) { * Parse `argv` (public). */ -void -command_parse(command_t *self, int argc, char **argv) { +void command_parse(command_t *self, int argc, char **argv) { self->nargv = normalize_args(&argc, argv); command_parse_args(self, argc, self->nargv); self->argv[self->argc] = NULL; diff --git a/src/commander.h b/src/commander.h index 141fedf..2b8c38c 100644 --- a/src/commander.h +++ b/src/commander.h @@ -55,7 +55,7 @@ typedef struct { * Command. */ -typedef struct command { +typedef struct command_t{ void *data; const char *usage; const char *arg; @@ -70,19 +70,14 @@ typedef struct command { // prototypes -void -command_init(command_t *self, const char *name, const char *version); +void command_init(command_t *self, const char *name, const char *version); -void -command_free(command_t *self); +void command_free(command_t *self); -void -command_help(command_t *self); +void command_help(command_t *self); -void -command_option(command_t *self, const char *small, const char *large, const char *desc, command_callback_t cb); +void command_option(command_t *self, const char *small, const char *large, const char *desc, command_callback_t cb); -void -command_parse(command_t *self, int argc, char **argv); +void command_parse(command_t *self, int argc, char **argv); #endif /* COMMANDER_H */ diff --git a/test.c b/test.c index e96b576..a3d35ae 100644 --- a/test.c +++ b/test.c @@ -2,23 +2,19 @@ #include #include "src/commander.h" -static void -verbose() { +static void verbose() { printf("verbose: enabled\n"); } -static void -required(command_t *self) { +static void required(command_t *self) { printf("required: %s\n", self->arg); } -static void -optional(command_t *self) { +static void optional(command_t *self) { printf("optional: %s\n", self->arg); } -int -main(int argc, char **argv){ +int main(int argc, char **argv){ command_t cmd; command_init(&cmd, argv[0], "0.0.1"); command_option(&cmd, "-v", "--verbose", "enable verbose stuff", verbose); From 555f3b3837fc1ef63b51e5e3683bfa9e74b96853 Mon Sep 17 00:00:00 2001 From: jingyuexing Date: Tue, 11 May 2021 18:56:20 +0800 Subject: [PATCH 2/4] * update README --- Readme.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Readme.md b/Readme.md index a571162..837083f 100644 --- a/Readme.md +++ b/Readme.md @@ -10,6 +10,13 @@ $ clib install clibs/commander ``` +# Build + Build this projecta with cmake + +``` +$ mkdir build &&cd build && cmake ../ +``` + ## Automated --help The [example](#example) below would produce the following `--help`: From 9e416fa93bae525368187b08ce5d15f755e28c74 Mon Sep 17 00:00:00 2001 From: jingyuexing Date: Tue, 11 May 2021 19:07:19 +0800 Subject: [PATCH 3/4] * cmake add c89 support --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 82bcd94..e81204f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,5 @@ cmake_minimum_required(VERSION 2.8) project(commander) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=89 -Wall -Wextra") aux_source_directory(${PROJECT_SOURCE_DIR}/src commander_source) add_library(commander SHARED ${commander_source}) \ No newline at end of file From 29ebac62ae224fd48901481490e2bb419db3c91b Mon Sep 17 00:00:00 2001 From: jingyuexing Date: Tue, 11 May 2021 22:14:37 +0800 Subject: [PATCH 4/4] Update Readme.md Co-authored-by: Bruno Dias --- Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 837083f..735701a 100644 --- a/Readme.md +++ b/Readme.md @@ -11,7 +11,8 @@ $ clib install clibs/commander ``` # Build - Build this projecta with cmake + + Build this project with cmake. ``` $ mkdir build &&cd build && cmake ../ @@ -114,4 +115,3 @@ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -