Switch to portable argument parser
This commit is contained in:
parent
e1ee9ae1a6
commit
e9b315f4ec
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -4,3 +4,6 @@
|
|||||||
[submodule "jansson"]
|
[submodule "jansson"]
|
||||||
path = jansson
|
path = jansson
|
||||||
url = https://github.com/akheron/jansson.git
|
url = https://github.com/akheron/jansson.git
|
||||||
|
[submodule "argparse"]
|
||||||
|
path = argparse
|
||||||
|
url = https://github.com/FooSoft/argparse
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.5)
|
cmake_minimum_required(VERSION 3.5)
|
||||||
project(zero-epwing)
|
project(zero-epwing)
|
||||||
include_directories(eb)
|
include_directories(eb)
|
||||||
link_directories(eb/eb/.libs jansson/lib)
|
link_directories(eb/eb/.libs jansson/lib argparse)
|
||||||
add_executable(zero-epwing main.c book.c font.c convert.c hooks.c)
|
add_executable(zero-epwing main.c book.c font.c convert.c hooks.c)
|
||||||
target_link_libraries(zero-epwing libeb.a libz.a libjansson.a)
|
target_link_libraries(zero-epwing libm.a libeb.a libm.a libz.a libjansson.a libargparse.a m)
|
||||||
|
1
argparse
Submodule
1
argparse
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 1692ce12291a9d79cf6b425037a7d1a0e3c0aba5
|
2
eb
2
eb
@ -1 +1 @@
|
|||||||
Subproject commit 3c455a6426baeccde823809627991ee943b3ac15
|
Subproject commit 6c26d968ba9d9b4c8b6af9b4bdb04861c29374b3
|
94
main.c
94
main.c
@ -18,8 +18,8 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <argp.h>
|
|
||||||
|
|
||||||
|
#include "argparse/argparse.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "book.h"
|
#include "book.h"
|
||||||
#include "font.h"
|
#include "font.h"
|
||||||
@ -34,80 +34,38 @@ typedef struct {
|
|||||||
int flags;
|
int flags;
|
||||||
} Options;
|
} 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 'p':
|
|
||||||
options->flags |= FLAG_PRETTY_PRINT;
|
|
||||||
break;
|
|
||||||
case 'm':
|
|
||||||
options->flags |= FLAG_HOOK_MARKUP;
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
options->flags |= FLAG_FONT_TAGS;
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
options->flags |= FLAG_POSITIONS;
|
|
||||||
break;
|
|
||||||
case ARGP_KEY_END:
|
|
||||||
if (*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, const char *argv[]) {
|
||||||
const struct argp_option argp_options[] = {
|
char* dict_path = NULL;
|
||||||
{ "dict", 'd', "PATH", 0, "dictionary resource to process", 0 },
|
char* font_path = NULL;
|
||||||
{ "font", 'f', "PATH", OPTION_ARG_OPTIONAL, "definition file for font translation", 0 },
|
int flags = 0;
|
||||||
{ "pretty-print", 'p', NULL, OPTION_ARG_OPTIONAL, "output pretty-printed JSON", 0 },
|
|
||||||
{ "markup", 'm', NULL, OPTION_ARG_OPTIONAL, "output formatting tags", 0 },
|
struct argparse_option options[] = {
|
||||||
{ "positions", 's', NULL, OPTION_ARG_OPTIONAL, "output positional data", 0 },
|
OPT_HELP(),
|
||||||
{ "font-tags", 't', NULL, OPTION_ARG_OPTIONAL, "output missing font data tags", 0 },
|
OPT_STRING('d', "dict", &dict_path, "dictionary resource to process", NULL, 0, 0),
|
||||||
{ }
|
OPT_STRING('f', "font", &font_path, "font definition file for translation", NULL, 0, 0),
|
||||||
|
OPT_BOOLEAN('p', "pretty-print", &flags, "output pretty-printed JSON", NULL, 0, 0),
|
||||||
|
OPT_BOOLEAN('m', "markup", &flags, "output formatting tags", 0, 0, 0),
|
||||||
|
OPT_BOOLEAN('s', "positions", &flags, "output positional data", NULL, 0, 0),
|
||||||
|
OPT_BOOLEAN('t', "font-tags", &flags, "output missing font data tags", NULL, 0, 0),
|
||||||
|
OPT_END()
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct argp argp = {
|
struct argparse argparse;
|
||||||
argp_options,
|
|
||||||
argp_parser,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
};
|
|
||||||
|
|
||||||
Options options = {};
|
argparse_init(&argparse, options, NULL, 0);
|
||||||
argp_parse(&argp, argc, argv, 0, 0, &options);
|
argparse_parse(&argparse, argc, argv);
|
||||||
|
|
||||||
|
if (dict_path == NULL) {
|
||||||
|
argparse_usage(&argparse);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
Font_Context context;
|
Font_Context context;
|
||||||
if (!font_context_init(&context, *options.font_path == 0 ? NULL : options.font_path)) {
|
if (!font_context_init(&context, font_path == 0 ? NULL : font_path)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,8 +73,8 @@ int main(int argc, char *argv[]) {
|
|||||||
book_init(&book);
|
book_init(&book);
|
||||||
|
|
||||||
const bool success =
|
const bool success =
|
||||||
book_import(&book, &context, options.dict_path, options.flags) &&
|
book_import(&book, &context, dict_path, flags) &&
|
||||||
book_export(stdout, &book, options.flags);
|
book_export(stdout, &book, flags);
|
||||||
|
|
||||||
book_free(&book);
|
book_free(&book);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user