Permalink
Browse files

Merge pull request #18 from sjonnet19/master

Dependency cleanup and Makefile adjustment
  • Loading branch information...
2 parents 6ba1e99 + 5017e16 commit e1b59bfdcfe5cc30731a4de8fe8f4daf2992d844 @tj committed Dec 17, 2012
View
@@ -1,7 +1,7 @@
PREFIX ?= /usr/local
-SRC = src/mon.c deps/ms/ms.c deps/commander/src/commander.c
+SRC = src/mon.c deps/ms.c deps/commander.c
OBJ = $(SRC:.c=.o)
-CFLAGS = -D_GNU_SOURCE -std=c99 -I deps/ms -I deps/commander/src
+CFLAGS = -D_GNU_SOURCE -std=c99 -I deps/
mon: $(OBJ)
$(CC) $^ -o $@
@@ -1,2 +0,0 @@
-*.o
-test
View
@@ -1,8 +0,0 @@
-
-test: test.c src/commander.c
- $(CC) $^ -std=c99 -o $@
-
-clean:
- rm -f test
-
-.PHONY: clean
View
@@ -1,68 +0,0 @@
-
-# commander.c
-
- Commander option parser ported to C.
-
-## Example
-
-```c
-#include <stdio.h>
-#include "commander.h"
-
-static void
-verbose(command_t *self) {
- printf("verbose: enabled\n");
-}
-
-static void
-required(command_t *self) {
- printf("required: %s\n", self->arg);
-}
-
-static void
-optional(command_t *self) {
- printf("optional: %s\n", self->arg);
-}
-
-int
-main(int argc, const char **argv){
- command_t cmd;
- command_init(&cmd, argv[0], "0.0.1");
- command_option(&cmd, "-v", "--verbose", "enable verbose stuff", verbose);
- command_option(&cmd, "-r", "--required <arg>", "required arg", required);
- command_option(&cmd, "-o", "--optional [arg]", "optional arg", optional);
- command_parse(&cmd, argc, argv);
- printf("additional args:\n");
- for (int i = 0; i < cmd.argc; ++i) {
- printf(" - '%s'\n", cmd.argv[i]);
- }
- return 0;
-}
-```
-
-## Automated --help
-
- The previous example would produce the following `--help`:
-
-```
-
-Usage: example [options]
-
-Options:
-
- -V, --version output program version
- -h, --help output help information
- -v, --verbose enable verbose stuff
- -r, --required <arg> required arg
- -o, --optional [arg] optional arg
-
-```
-
-## Closure
-
- `cmd.data` is a `void *` so pass along a struct to the callbacks if you want.
-
-## Todo
-
- - unrecognized flags (suggestions?)
- - specify "Usage: " string
@@ -1,8 +0,0 @@
- name: commander
- version: 0.0.1
- description: option parser
- dependencies:
- tags: option parser
- author: TJ Holowaychuk <tj@vision-media.ca>
- url: http://github.com/visionmedia/commander.c
-
@@ -1,180 +0,0 @@
-
-//
-// commander.c
-//
-// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
-//
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include "commander.h"
-
-/*
- * Output error and exit.
- */
-
-static void
-error(char *msg) {
- fprintf(stderr, "%s\n", msg);
- exit(1);
-}
-
-/*
- * Output command version.
- */
-
-static void
-command_version(command_t *self) {
- printf("%s\n", self->version);
- exit(0);
-}
-
-/*
- * Output command help.
- */
-
-void
-command_help(command_t *self) {
- printf("\n");
- printf(" Usage: %s %s\n", self->name, self->usage);
- printf("\n");
- printf(" Options:\n");
- printf("\n");
- for (int i = 0; i < self->option_count; ++i) {
- command_option_t *option = &self->options[i];
- printf(" %s, %-25s %s\n"
- , option->small
- , option->large_with_arg
- , option->description);
- }
- printf("\n");
- exit(0);
-}
-
-/*
- * Initialize with program `name` and `version`.
- */
-
-void
-command_init(command_t *self, const char *name, const char *version) {
- self->arg = NULL;
- self->name = name;
- self->version = version;
- self->option_count = self->argc = 0;
- self->usage = "[options]";
- command_option(self, "-V", "--version", "output program version", command_version);
- command_option(self, "-h", "--help", "output help information", command_help);
-}
-
-/*
- * Parse argname from `str`. For example
- * Take "--required <arg>" and populate `flag`
- * with "--required" and `arg` with "<arg>".
- */
-
-static void
-parse_argname(const char *str, char *flag, char *arg) {
- int buffer = 0;
- size_t flagpos = 0;
- size_t argpos = 0;
- size_t len = strlen(str);
-
- for (int i = 0; i < len; ++i) {
- if (buffer || '[' == str[i] || '<' == str[i]) {
- buffer = 1;
- arg[argpos++] = str[i];
- } else {
- if (' ' == str[i]) continue;
- flag[flagpos++] = str[i];
- }
- }
-
- arg[argpos] = 0;
- flag[flagpos] = 0;
-}
-
-/*
- * Define an option.
- */
-
-void
-command_option(command_t *self, const char *small, const char *large, const char *desc, command_callback_t cb) {
- int n = self->option_count++;
- if (n == COMMANDER_MAX_OPTIONS) error("Maximum option definitions exceeded");
- command_option_t *option = &self->options[n];
- option->cb = cb;
- option->small = small;
- option->description = desc;
- option->required_arg = option->optional_arg = 0;
- option->large_with_arg = large;
- option->argname = malloc(strlen(large));
- assert(option->argname);
- option->large = malloc(strlen(large));
- assert(option->large);
- parse_argname(large, option->large, option->argname);
- if ('[' == option->argname[0]) option->optional_arg = 1;
- if ('<' == option->argname[0]) option->required_arg = 1;
-}
-
-/*
- * Parse `argv`.
- *
- * TODO: list of short opts (-vo as -v -o)
- */
-
-void
-command_parse(command_t *self, int argc, char **argv) {
- int literal = 0;
-
- for (int i = 1; i < argc; ++i) {
- const char *arg = argv[i];
- for (int j = 0; j < self->option_count; ++j) {
- command_option_t *option = &self->options[j];
-
- // match flag
- if (!strcmp(arg, option->small) || !strcmp(arg, option->large)) {
- self->arg = NULL;
-
- // required
- if (option->required_arg) {
- arg = argv[++i];
- if (!arg || '-' == arg[0]) {
- fprintf(stderr, "%s %s argument required\n", option->large, option->argname);
- exit(1);
- }
- self->arg = arg;
- }
-
- // optional
- if (option->optional_arg) {
- if (argv[i + 1] && '-' != argv[i + 1][0]) {
- self->arg = argv[++i];
- }
- }
-
- // invoke callback
- option->cb(self);
- goto match;
- }
- }
-
- // --
- if ('-' == arg[0] && '-' == arg[1] && 0 == arg[2]) {
- literal = 1;
- goto match;
- }
-
- // unrecognized
- if ('-' == arg[0] && !literal) {
- fprintf(stderr, "unrecognized flag %s\n", arg);
- exit(1);
- }
-
- int n = self->argc++;
- if (n == COMMANDER_MAX_ARGS) error("Maximum number of arguments exceeded");
- self->argv[n] = (char *) arg;
- match:;
- }
-}
@@ -1,84 +0,0 @@
-
-//
-// commander.h
-//
-// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
-//
-
-#ifndef COMMANDER_H
-#define COMMANDER_H
-
-/*
- * Max options that can be defined.
- */
-
-#ifndef COMMANDER_MAX_OPTIONS
-#define COMMANDER_MAX_OPTIONS 32
-#endif
-
-/*
- * Max arguments that can be passed.
- */
-
-#ifndef COMMANDER_MAX_ARGS
-#define COMMANDER_MAX_ARGS 32
-#endif
-
-/*
- * Command struct.
- */
-
-struct command;
-
-/*
- * Option callback.
- */
-
-typedef void (* command_callback_t)(struct command *self);
-
-/*
- * Command option.
- */
-
-typedef struct {
- int optional_arg;
- int required_arg;
- char *argname;
- char *large;
- const char *small;
- const char *large_with_arg;
- const char *description;
- command_callback_t cb;
-} command_option_t;
-
-/*
- * Command.
- */
-
-typedef struct command {
- void *data;
- const char *usage;
- const char *arg;
- const char *name;
- const char *version;
- int option_count;
- command_option_t options[COMMANDER_MAX_OPTIONS];
- int argc;
- char *argv[COMMANDER_MAX_ARGS];
-} command_t;
-
-// prototypes
-
-void
-command_init(command_t *self, const char *name, const char *version);
-
-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_parse(command_t *self, int argc, char **argv);
-
-#endif /* COMMANDER_H */
Oops, something went wrong.

0 comments on commit e1b59bf

Please sign in to comment.