1

Arg parsing

This commit is contained in:
Alex Yatskov 2016-11-28 20:42:10 -08:00
parent 5c2422332a
commit d072599c49

99
main.c
View File

@ -16,47 +16,96 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdlib.h> #include <string.h>
#include <getopt.h> #include <stdbool.h>
#include <argp.h>
#include "util.h"
#include "book.h" #include "book.h"
#include "gaiji.h" #include "gaiji.h"
/*
* Local types
*/
typedef struct {
char dict_path[256];
char font_path[256];
bool pretty_print;
bool markup;
} Options;
/*
* Local functions
*/
static error_t argp_parser(int key, char* arg, struct argp_state* state) {
Options* options = (Options *)state->input;
switch (key) {
case 'd':
if (arg != NULL) {
strncpy(options->dict_path, arg, ARRSIZE(options->dict_path));
options->dict_path[ARRSIZE(options->dict_path) - 1] = 0;
}
break;
case 'f':
if (arg != NULL) {
strncpy(options->font_path, arg, ARRSIZE(options->font_path));
options->font_path[ARRSIZE(options->font_path) - 1] = 0;
}
break;
case 'm':
options->markup = true;
break;
case 'p':
options->pretty_print = true;
break;
case ARGP_KEY_END:
if (strlen(options->dict_path) == 0) {
argp_usage(state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* /*
* Entry point * Entry point
*/ */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
bool pretty_print = false; const struct argp_option argp_options[] = {
bool markup = false; { "dict", 'd', "PATH", 0, "dictionary resource to process", 0 },
{ "font", 'f', "PATH", OPTION_ARG_OPTIONAL, "definition file for font translation", 0 },
{ "pretty-print", 'p', NULL, OPTION_ARG_OPTIONAL, "pretty print JSON output", 0 },
{ "markup", 'm', NULL, OPTION_ARG_OPTIONAL, "markup JSON output with formatting tags", 0 },
{ }
};
char opt = 0; const struct argp argp = {
while ((opt = getopt(argc, argv, "pm")) != -1) { argp_options,
switch (opt) { argp_parser,
case 'p': NULL,
pretty_print = true; NULL,
break; NULL,
case 'm': NULL,
markup = true; NULL,
break; };
default:
exit(EXIT_FAILURE);
break;
}
}
if (optind >= argc) { Options options = { };
fprintf(stderr, "Usage: %s [-p] dictionary_path\n", argv[0]); argp_parse(&argp, argc, argv, 0, 0, &options);
exit(EXIT_FAILURE);
}
Gaiji_Context context; Gaiji_Context context;
gaiji_context_init(&context, "gaiji.json"); gaiji_context_init(&context, options.font_path);
Book book; Book book;
book_init(&book); book_init(&book);
book_export(&book, &context, argv[optind], markup); book_export(&book, &context, options.dict_path, options.markup);
book_dump(&book, pretty_print, stdout); book_dump(&book, options.pretty_print, stdout);
book_free(&book); book_free(&book);
gaiji_context_destroy(&context); gaiji_context_destroy(&context);