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..e81204f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +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 diff --git a/Readme.md b/Readme.md index a571162..735701a 100644 --- a/Readme.md +++ b/Readme.md @@ -10,6 +10,14 @@ $ clib install clibs/commander ``` +# Build + + Build this project with cmake. + +``` +$ mkdir build &&cd build && cmake ../ +``` + ## Automated --help The [example](#example) below would produce the following `--help`: @@ -107,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. - 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);