Skip to content

Commit

Permalink
Add long options in the manual + work on contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
p-gen committed Oct 5, 2019
1 parent 24037bd commit 0f41afe
Show file tree
Hide file tree
Showing 28 changed files with 1,010 additions and 579 deletions.
79 changes: 69 additions & 10 deletions ctxopt.c
Expand Up @@ -247,8 +247,8 @@ fatal(errors e, char * opt_par, char * opt_params, char * opt_name,
fprintf(stderr, "\n");
}

if (opt_par != NULL)
ctxopt_disp_usage(continue_after);
if (ctx_name != NULL)
ctxopt_ctx_disp_usage(ctx_name, continue_after);

va_end(args);

Expand All @@ -264,7 +264,8 @@ error(const char * format, ...)
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, "\n");
ctxopt_disp_usage(exit_after);

exit(EXIT_FAILURE);
}

/* ******************************** */
Expand Down Expand Up @@ -1418,8 +1419,9 @@ bst_print_ctx_cb(const void * node, walk_order_e kind, int level)
if (kind == postorder || kind == leaf)
if (strcmp(ctx->name, cur_ctx->name) != 0)
{
printf("\nOptions allowed in the context called %s:\n", cur_ctx->name);
list = cur_ctx->opt_list;

printf("\nAllowed options in the context %s:\n", cur_ctx->name);
print_options(list, &has_optional, &has_ellipsis, &has_rule,
&has_generic_arg, &has_ctx_change, &has_early_eval);
}
Expand Down Expand Up @@ -1734,7 +1736,7 @@ opt_parse(char * s, opt_t ** opt)
opt_optional = 1;
s++;
}
s = strtoken(s, token, sizeof(token), "[^] \t.]", &pos);
s = strtoken(s, token, sizeof(token), "[^] \n\t.]", &pos);
if (s == NULL)
return -1; /* empty string */

Expand Down Expand Up @@ -1967,7 +1969,7 @@ init_opts(char * spec, ctx_t * ctx)
bst_t * node;
int offset;

ltrim(spec, " \t");
ltrim(spec, " \n\t");
while (*spec)
{
if ((offset = opt_parse(spec, &opt)) > 0)
Expand All @@ -1976,7 +1978,7 @@ init_opts(char * spec, ctx_t * ctx)

if ((node = bst_find(opt, &options_bst, opt_compare)) != NULL)
{
int same_next_ctx;
int same_next_ctx = 0;

bst_opt = node->key; /* node extracted from the BST */

Expand All @@ -1987,7 +1989,7 @@ init_opts(char * spec, ctx_t * ctx)
else if (bst_opt->next_ctx != NULL && opt->next_ctx == NULL)
same_next_ctx = 0;
else
same_next_ctx = strcmp(bst_opt->next_ctx, opt->next_ctx) != 0;
same_next_ctx = strcmp(bst_opt->next_ctx, opt->next_ctx) == 0;

if (bst_opt->optional_args != opt->optional_args
|| bst_opt->multiple_args != opt->multiple_args
Expand Down Expand Up @@ -3072,6 +3074,51 @@ ctxopt_new_ctx(char * name, char * opts_specs)
bst_search(ctx, &contexts_bst, ctx_compare);
}

void
ctxopt_ctx_disp_usage(char * ctx_name, usage_behaviour action)
{
ctx_t * ctx;
ll_t * list;

int has_optional = 0;
int has_ellipsis = 0;
int has_rule = 0;
int has_generic_arg = 0;
int has_ctx_change = 0;
int has_early_eval = 0;

ctx = locate_ctx(ctx_name);
if (ctx == NULL)
fatal(CTXOPTINTERNAL, NULL, NULL, NULL, NULL, NULL, "%s: unknown context.",
ctx_name);

printf("\nSynopsis:\n");
list = ctx->opt_list;
print_options(list, &has_optional, &has_ellipsis, &has_rule, &has_generic_arg,
&has_ctx_change, &has_early_eval);

printf("\nExplanations:\n");
if (has_early_eval)
printf("* : the parameters for this option will be "
"evaluated first.\n");
if (has_ctx_change)
printf("> : given after the parameters, sets the next "
"context to go.\n");
if (has_generic_arg)
printf("# : generic name for an argument.\n");
if (has_optional)
printf("[...] : the object is square brackets is optional.\n");
if (has_ellipsis)
printf("... : the previous object can be repeated more "
"than one times.\n");
if (has_rule)
printf("[<|=|>]number: rules constraining the number of "
"parameters/arguments.\n");

if (action == exit_after)
exit(EXIT_FAILURE);
}

void
ctxopt_disp_usage(usage_behaviour action)
{
Expand All @@ -3087,27 +3134,39 @@ ctxopt_disp_usage(usage_behaviour action)
fatal(CTXOPTINTERNAL, NULL, NULL, NULL, NULL, NULL,
"At least one context must have been created.");

printf("\nOptions allowed in the default context:\n");
/* Usage for the first context */
/* """"""""""""""""""""""""""" */
printf("\nAllowed options in the default context:\n");
list = main_ctx->opt_list;
print_options(list, &has_optional, &has_ellipsis, &has_rule, &has_generic_arg,
&has_ctx_change, &has_early_eval);

/* Usage for the other contexts */
/* """""""""""""""""""""""""""" */
bst_walk(contexts_bst, bst_print_ctx_cb);

printf("\nExplanations:\n");
/* Contextual syntactic explanations */
/* """"""""""""""""""""""""""""""""" */
printf("\nSyntactic explanations:\n");

if (has_early_eval)
printf("* : the parameters for this option will be "
"evaluated first.\n");

if (has_ctx_change)
printf("> : given after the parameters, sets the next "
"context to go.\n");

if (has_generic_arg)
printf("# : generic name for an argument.\n");

if (has_optional)
printf("[...] : the object is square brackets is optional.\n");

if (has_ellipsis)
printf("... : the previous object can be repeated more "
"than one times.\n");

if (has_rule)
printf("[<|=|>]number: rules constraining the number of "
"parameters/arguments.\n");
Expand Down
6 changes: 3 additions & 3 deletions ctxopt.h
@@ -1,6 +1,3 @@
/* This file was automatically generated. Do not edit! */
/* This file was automatically generated. Do not edit! */

typedef enum
{
parameters,
Expand Down Expand Up @@ -49,6 +46,9 @@ ctxopt_evaluate(void);
void
ctxopt_new_ctx(char * name, char * opts_specs);

void
ctxopt_ctx_disp_usage(char * ctx_name, usage_behaviour action);

void
ctxopt_disp_usage(usage_behaviour action);

Expand Down

0 comments on commit 0f41afe

Please sign in to comment.