Пример #1
0
static char * test2() {
	int     bflag1 = -4;
	int     bflag2 = -5;
	int     intv1  = -6;
	int     intv2  = -7;
	int     intv3  = -8;
	double  fltv   = -9.5;
	char*   string = NULL;
	slls_t* plist  = NULL;
	string_array_t* parray  = NULL;
	ap_state_t* pstate = ap_alloc();

	ap_define_true_flag(pstate,        "-t",   &bflag1);
	ap_define_false_flag(pstate,       "-f",   &bflag2);
	ap_define_int_value_flag(pstate,   "-100", 100, &intv1);
	ap_define_int_value_flag(pstate,   "-200", 200, &intv2);
	ap_define_int_flag(pstate,         "-i",   &intv3);
	ap_define_float_flag(pstate,      "-d",   &fltv);
	ap_define_string_flag(pstate,      "-s",   &string);
	ap_define_string_list_flag(pstate, "-S",   &plist);
	ap_define_string_array_flag(pstate, "-A",   &parray);

	char* argv[] = {
		"test-verb",
		"-t",
		"-f",
		"-100",
		"-200",
		"-i", "555",
		"-d", "4.25",
		"-s", "hello",
		"-S", mlr_strdup_or_die("a,b,c,d,e"),
		"-A", mlr_strdup_or_die("x,y,z"),
		"do", "re", "mi",
		NULL
	};
	int argc = compute_argc(argv);
	char* verb = argv[0];
	int argi = 1;
	mu_assert_lf(ap_parse(pstate, verb, &argi, argc, argv) == TRUE);
	mu_assert_lf(bflag1 == TRUE);
	mu_assert_lf(bflag2 == FALSE);
	mu_assert_lf(intv1 == 100);
	mu_assert_lf(intv2 == 200);
	mu_assert_lf(intv3 == 555);
	mu_assert_lf(fltv == 4.25);
	mu_assert_lf(string != NULL);
	mu_assert_lf(streq(string, "hello"));
	mu_assert_lf(plist != NULL);
	mu_assert_lf(slls_equals(plist, slls_from_line(mlr_strdup_or_die("a,b,c,d,e"), ',', FALSE)));
	mu_assert_lf(parray != NULL);
	mu_assert_lf(parray->length == 3);
	mu_assert_lf(streq(parray->strings[0], "x"));
	mu_assert_lf(streq(parray->strings[1], "y"));
	mu_assert_lf(streq(parray->strings[2], "z"));
	mu_assert_lf(argi == 15);

	ap_free(pstate);
	return 0;
}
Пример #2
0
// ----------------------------------------------------------------
static mapper_t* mapper_put_parse_cli(int* pargi, int argc, char** argv) {
	char* verb = argv[(*pargi)++];
	char* mlr_dsl_expression = NULL;
	int   print_asts = FALSE;

	ap_state_t* pstate = ap_alloc();
	ap_define_true_flag(pstate, "-v", &print_asts);

	if (!ap_parse(pstate, verb, pargi, argc, argv)) {
		mapper_put_usage(argv[0], verb);
		return NULL;
	}

	if ((argc - *pargi) < 1) {
		mapper_put_usage(argv[0], verb);
		return NULL;
	}
	mlr_dsl_expression = argv[(*pargi)++];

	// Linked list of mlr_dsl_ast_node_t*.
	sllv_t* pasts = put_dsl_parse(mlr_dsl_expression);
	if (pasts == NULL) {
		mapper_put_usage(argv[0], verb);
		return NULL;
	}
	if (print_asts) {
		for (sllve_t* pe = pasts->phead; pe != NULL; pe = pe->pnext)
			mlr_dsl_ast_node_print(pe->pvdata);
	}

	return mapper_put_alloc(pasts);
}
Пример #3
0
// This sample application will parse its own command-line arguments.
int main(int argc, char **argv) {

    // We instantiate an argument parser, optionally supplying help text and a
    // version string. Supplying help text activates the automatic --help flag,
    // supplying a version string activates the automatic --version flag.
    // We can pass NULL for either parameter.
    ArgParser *parser = ap_new("Help!", "Version!");

    // Register a flag, --bopt, with a single-character alias, -b. A flag is a
    // boolean option - it's either present (true) or absent (false).
    ap_add_flag(parser, "bopt b");

    // Register a string option, --sopt <arg>, with a single-character alias,
    // -s <arg>. A string argument requires a default value, here 'defval'.
    ap_add_str(parser, "sopt s", "defval");

    // Register an integer option, --iopt <arg>. An integer option requires a
    // default value, here 123.
    ap_add_int(parser, "iopt", 123);

    // Register a float option, --fopt <arg>. A float option requires a default
    // value, here 1.0.
    ap_add_float(parser, "fopt", 1.0);

    // Register an integer list, --ilist <arg>, with a single-character alias,
    // -i <arg>. A list option accepts multiple values. The final parameter
    // specifies that the option is not 'greedy'.
    ap_add_int_list(parser, "ilist i", false);

    // Register a 'greedy' float list, --flist <args>, with a single-character
    // alias, -f <args>. A list option accepts multiple values; a 'greedy' list
    // attempts to parse multiple consecutive arguments.
    ap_add_float_list(parser, "flist f", true);

    // Register a command 'foo', with an alias 'bar'. We need to supply the
    // command's help text and callback method.
    ArgParser *cmd_parser = ap_add_cmd(parser, "foo bar", "Command!", callback);

    // Registering a command returns a new ArgParser instance dedicated to
    // parsing the command's arguments. We can register as many flags and
    // options as we like on this sub-parser. Note that the sub-parser can
    // reuse the parent's option names without interference.
    ap_add_flag(cmd_parser, "bopt b");
    ap_add_int(cmd_parser, "iopt i", 123);

    // Once all our options and commands have been registered we can call the
    // ap_parse() function with an array of argument strings. (Note that we
    // only need to call ap_parse() on the root parser - command arguments
    // are parsed automatically.)
    ap_parse(parser, argc, argv);

    // We can now retrieve our option and argument values from the parser
    // instance. Here we simply dump the parser to stdout.
    ap_print(parser);
}
Пример #4
0
int main(int argc, char** argv) {
	int     bflag  = TRUE;
	int     intv   = 0;
	double  dblv   = 0.0;
	char*   string = NULL;
	slls_t* plist  = NULL;
	ap_state_t* pstate = ap_alloc();

	ap_define_true_flag(pstate,        "-t",   &bflag);
	ap_define_false_flag(pstate,       "-f",   &bflag);
	ap_define_int_value_flag(pstate,   "-100", 100,      &intv);
	ap_define_int_value_flag(pstate,   "-200", 200,      &intv);
	ap_define_int_flag(pstate,         "-i",   &intv);
	ap_define_double_flag(pstate,      "-d",   &dblv);
	ap_define_string_flag(pstate,      "-s",   &string);
	ap_define_string_list_flag(pstate, "-S",   &plist);

	char* verb = "stub";
	int argi = 1;
	if (ap_parse(pstate, verb, &argi, argc, argv) == TRUE) {
		printf("OK\n");
	} else {
		printf("Usage!\n");
	}

	printf("argi  is %d\n", argi);
	printf("argc  is %d\n", argc);
	printf("rest  is");
	for (; argi < argc; argi++)
		printf(" %s", argv[argi]);
	printf("\n");
	printf("bflag is %d\n", bflag);
	printf("intv  is %d\n", intv);
	printf("dblv  is %g\n", dblv);

	if (string == NULL) {
		printf("string  is null\n");
	} else {
		printf("string  is \"%s\"\n", string);
	}

	if (plist == NULL) {
		printf("list  is null\n");
	} else {
		char* out = slls_join(plist, ',');
		printf("list  is %s\n", out);
		free(out);
	}

	ap_free(pstate);

	return 0;
}
Пример #5
0
static char * test2() {
	int     bflag1 = -4;
	int     bflag2 = -5;
	int     intv1  = -6;
	int     intv2  = -7;
	int     intv3  = -8;
	double  dblv   = -9.5;
	char*   string = NULL;
	slls_t* plist  = NULL;
	ap_state_t* pstate = ap_alloc();

	ap_define_true_flag(pstate,        "-t",   &bflag1);
	ap_define_false_flag(pstate,       "-f",   &bflag2);
	ap_define_int_value_flag(pstate,   "-100", 100, &intv1);
	ap_define_int_value_flag(pstate,   "-200", 200, &intv2);
	ap_define_int_flag(pstate,         "-i",   &intv3);
	ap_define_double_flag(pstate,      "-d",   &dblv);
	ap_define_string_flag(pstate,      "-s",   &string);
	ap_define_string_list_flag(pstate, "-S",   &plist);

	char* argv[] = {
		"test-verb",
		"-t",
		"-f",
		"-100",
		"-200",
		"-i", "555",
		"-d", "4.25",
		"-s", "hello",
		"-S", strdup("a,b,c,d,e"),
		"do", "re", "mi",
		NULL
	};
	int argc = compute_argc(argv);
	char* verb = argv[0];
	int argi = 1;
	mu_assert_lf(ap_parse(pstate, verb, &argi, argc, argv) == TRUE);
	mu_assert_lf(bflag1 == TRUE);
	mu_assert_lf(bflag2 == FALSE);
	mu_assert_lf(intv1 == 100);
	mu_assert_lf(intv2 == 200);
	mu_assert_lf(intv3 == 555);
	mu_assert_lf(dblv == 4.25);
	mu_assert_lf(string != NULL);
	mu_assert_lf(streq(string, "hello"));
	mu_assert_lf(plist != NULL);
	mu_assert_lf(slls_equals(plist, slls_from_line(strdup("a,b,c,d,e"), ',', FALSE)));
	mu_assert_lf(argi == 13);

	ap_free(pstate);
	return 0;
}
Пример #6
0
static int test(const char *str)
{
  char *dup = str != NULL ? strdup(str) : NULL;
  const char *a = "ADDR", *p = "PORT";

  int rc = ap_parse(dup, &a, &p);
  printf("%s `%s' `%s' `%s'\n",
         rc == 0 ? "PASS" : "FAIL",
         str != NULL ? str : "<NULL>", a, p);

  free(dup);

  return rc;
}
Пример #7
0
static mapper_t* mapper_head_parse_cli(int* pargi, int argc, char** argv) {
	int     head_count            = 10;
	slls_t* pgroup_by_field_names = slls_alloc();

	char* verb = argv[(*pargi)++];

	ap_state_t* pstate = ap_alloc();
	ap_define_int_flag(pstate, "-n", &head_count);
	ap_define_string_list_flag(pstate, "-g", &pgroup_by_field_names);

	if (!ap_parse(pstate, verb, pargi, argc, argv)) {
		mapper_head_usage(argv[0], verb);
		return NULL;
	}

	return mapper_head_alloc(pgroup_by_field_names, head_count);
}
Пример #8
0
static char * test1() {
	int     bflag1 = -4;
	int     bflag2 = -5;
	int     intv1  = -6;
	int     intv2  = -7;
	int     intv3  = -8;
	double  fltv   = -9.5;
	char*   string = NULL;
	slls_t* plist  = NULL;
	string_array_t* parray  = NULL;
	ap_state_t* pstate = ap_alloc();

	ap_define_true_flag(pstate,         "-t",   &bflag1);
	ap_define_false_flag(pstate,        "-f",   &bflag2);
	ap_define_int_value_flag(pstate,    "-100", 100, &intv1);
	ap_define_int_value_flag(pstate,    "-200", 200, &intv2);
	ap_define_int_flag(pstate,          "-i",   &intv3);
	ap_define_float_flag(pstate,       "-d",   &fltv);
	ap_define_string_flag(pstate,       "-s",   &string);
	ap_define_string_list_flag(pstate,  "-S",   &plist);
	ap_define_string_array_flag(pstate, "-A",   &parray);

	char* argv[] = { "test-verb", NULL };
	int argc = compute_argc(argv);
	char* verb = argv[0];
	int argi = 1;
	mu_assert_lf(ap_parse(pstate, verb, &argi, argc, argv) == TRUE);
	mu_assert_lf(bflag1 == -4);
	mu_assert_lf(bflag2 == -5);
	mu_assert_lf(intv1 == -6);
	mu_assert_lf(intv2 == -7);
	mu_assert_lf(intv3 == -8);
	mu_assert_lf(fltv == -9.5);
	mu_assert_lf(string == NULL);
	mu_assert_lf(plist == NULL);
	mu_assert_lf(parray == NULL);
	mu_assert_lf(argi == 1);

	ap_free(pstate);
	return 0;
}
Пример #9
0
// ----------------------------------------------------------------
static mapper_t* mapper_reorder_parse_cli(int* pargi, int argc, char** argv) {
	slls_t* pfield_name_list = NULL;
	int     put_at_end       = FALSE;

	char* verb = argv[(*pargi)++];

	ap_state_t* pstate = ap_alloc();
	ap_define_string_list_flag(pstate, "-f", &pfield_name_list);
	ap_define_true_flag(pstate, "-e", &put_at_end);

	if (!ap_parse(pstate, verb, pargi, argc, argv)) {
		mapper_reorder_usage(argv[0], verb);
		return NULL;
	}

	if (pfield_name_list == NULL) {
		mapper_reorder_usage(argv[0], verb);
		return NULL;
	}

	return mapper_reorder_alloc(pfield_name_list, put_at_end);
}
Пример #10
0
static mapper_t* mapper_step_parse_cli(int* pargi, int argc, char** argv) {
	slls_t* pstepper_names        = NULL;
	slls_t* pvalue_field_names    = NULL;
	slls_t* pgroup_by_field_names = slls_alloc();

	char* verb = argv[(*pargi)++];

	ap_state_t* pstate = ap_alloc();
	ap_define_string_list_flag(pstate, "-a", &pstepper_names);
	ap_define_string_list_flag(pstate, "-f", &pvalue_field_names);
	ap_define_string_list_flag(pstate, "-g", &pgroup_by_field_names);

	if (!ap_parse(pstate, verb, pargi, argc, argv)) {
		mapper_step_usage(argv[0], verb);
		return NULL;
	}

	if (pstepper_names == NULL || pvalue_field_names == NULL) {
		mapper_step_usage(argv[0], verb);
		return NULL;
	}

	return mapper_step_alloc(pstepper_names, pvalue_field_names, pgroup_by_field_names);
}