Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* add cmake file #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*.o
test
.exe
build/
.vscode/
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
9 changes: 8 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down Expand Up @@ -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.

30 changes: 10 additions & 20 deletions src/commander.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -102,8 +97,7 @@ command_free(command_t *self) {
* with "--required" and `arg` with "<arg>".
*/

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;
Expand All @@ -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 *));
Expand Down Expand Up @@ -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");
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
17 changes: 6 additions & 11 deletions src/commander.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ typedef struct {
* Command.
*/

typedef struct command {
typedef struct command_t{
void *data;
const char *usage;
const char *arg;
Expand All @@ -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 */
12 changes: 4 additions & 8 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
#include <stdio.h>
#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);
Expand Down