~brenns10/sc-argparse

bd0200c066acdbb37a73c88d48a917806923ffdf — Stephen Brennan 4 years ago 3012e14
Test and fix bugs in --arg=value args
2 files changed, 31 insertions(+), 9 deletions(-)

M src/sc-argparse.c
M tests/main.c
M src/sc-argparse.c => src/sc-argparse.c +7 -6
@@ 49,11 49,12 @@ static struct sc_arg *sc_get_arg_by_flag(struct sc_arg *argspec, char flag)
}

static struct sc_arg *sc_get_arg_by_name(struct sc_arg *argspec,
                                         const char *name)
                                         const char *name, int len)
{
	int i;
	for (i = 0; argspec[i].type != SC_ARG_TYPE_END; i++)
		if (strcmp(name, argspec[i].name) == 0)
		if ((strncmp(name, argspec[i].name, len) == 0) &&
		    argspec[i].name[len] == '\0')
			return &argspec[i];

	return NULL;


@@ 108,11 109,11 @@ static enum error sc_process_long_flag(struct state *state)
	arg = state->argv[state->argidx];
	equal = strchr(arg, '=');
	if (!equal) {
		arg_struct = sc_get_arg_by_name(state->argspec, arg);
		arg_struct =
		        sc_get_arg_by_name(state->argspec, arg, strlen(arg));
	} else {
		*equal = '\0';
		arg_struct = sc_get_arg_by_name(state->argspec, arg);
		*equal = '=';
		arg_struct =
		        sc_get_arg_by_name(state->argspec, arg, equal - arg);
	}
	if (!arg_struct)
		error(UNKNOWN_LONG_FLAG, state);

M tests/main.c => tests/main.c +24 -3
@@ 76,7 76,7 @@ void test_intargs(void)
	reset_args(argspec);
	argspec[0].val_int = 5; /* need to reset default */

	testarg = (char *[]){ "-b", "-500", "-c", "1234" };
	testarg = (char *[]){ "--argb=-500", "-c", "1234" };
	sc_argparse(argspec, 4, testarg);
	TEST_ASSERT_EQUAL(1234, argspec[0].val_int);
	TEST_ASSERT_EQUAL(-500, argspec[1].val_int);


@@ 130,13 130,19 @@ void test_stringargs(void)
	argspec[1].val_string = "default b";

	/* missing argument! */
	testarg = (char *[]){ "--arga" };
	testarg = (char *[]){ "-ca" };
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 1, testarg));

	reset_args(argspec);
	argspec[1].val_string = "default b";

	/* missing argument! */
	testarg = (char *[]){ "--arga" };
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 1, testarg));

	reset_args(argspec);
	argspec[1].val_string = "default b";

	testarg = (char *[]){ "--arga", "value" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL_STRING("value", argspec[0].val_string);


@@ 145,11 151,26 @@ void test_stringargs(void)
	reset_args(argspec);
	argspec[1].val_string = "default b";

	/* missing argument! */
	testarg = (char *[]){ "--arga", "value", "-b", "valforb" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 4, testarg));
	TEST_ASSERT_EQUAL_STRING("value", argspec[0].val_string);
	TEST_ASSERT_EQUAL_STRING("valforb", argspec[1].val_string);

	reset_args(argspec);
	argspec[1].val_string = "default b";

	testarg = (char *[]){ "--arga=value", "--argb", "valforb" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 3, testarg));
	TEST_ASSERT_EQUAL_STRING("value", argspec[0].val_string);
	TEST_ASSERT_EQUAL_STRING("valforb", argspec[1].val_string);

	reset_args(argspec);
	argspec[1].val_string = "default b";

	/* Should be able to have args after a short flag w/o arg */
	testarg = (char *[]){ "-ca", "a special value" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL_STRING("a special value", argspec[0].val_string);
}

int main(void)