Example #1
0
int subcommand1(int start, int argc, char **argv)
{
    int ret = 0;
    cargo_t mod1;
    if (!argv) return -1;
    if (cargo_init(&mod1, 0, "%s command1", argv[0]))
    {
        fprintf(stderr, "Failed to init command line parsing\n");
        return -1;
    }

    ret |= add_global_opts(mod1);
    ret |= add_module1_opts(mod1);
    assert(ret == 0);

    print_args(argc - start, &argv[start]);

    if (cargo_parse(mod1, 0, start, argc, argv)) return -1;

    printf("opt1 == %s\n", global_1);

    cargo_destroy(&mod1);

    return ret;
}
Example #2
0
int main(int argc, char **argv)
{
	cargo_t cargo;
	int ret = 0;
	int *integers = NULL;
	size_t integer_count = 0;
	accumulator_f accumulator = max_ints;
	int sum_flag = 0;

	printf("cargo version v%s\n", cargo_get_version());

	if (cargo_init(&cargo, 0, "%s", argv[0]))
	{
		fprintf(stderr, "Failed to init command line parsing\n");
		return -1;
	}

	cargo_set_description(cargo, "Process some integers.");

	ret |= cargo_add_option(cargo, 0, "integers",
							"An integer for the accumulator",
							"[i]+", &integers, &integer_count);
	ret |= cargo_add_option(cargo, 0, "--sum -s",
							"Sum the integers (default: find the max)",
							"b", &sum_flag);
	assert(ret == 0);

	if (cargo_parse(cargo, 0, 1, argc, argv)) return -1;
	if (sum_flag) accumulator = sum_ints;
	printf("%d\n", accumulator(integers, integer_count));
	cargo_destroy(&cargo);
	free(integers);
	return 0;
}
Example #3
0
int subcommand2(int start, int argc, char **argv)
{
    int ret = 0;
    cargo_t mod2;

    if (!argv) return -1;

    if (cargo_init(&mod2, 0,  "%s command2", argv[0]))
    {
        fprintf(stderr, "Failed to init command line parsing\n");
        return -1;
    }

    ret |= add_global_opts(mod2);

    // Module 1.
    ret |= add_module1_opts(mod2);

    // Module 2.
    ret |= cargo_add_group(mod2, 0, "module2", "Module 2 options", NULL);
    ret |= cargo_add_option(mod2, 0, "<module2> --og2_1", "String used for option 1 1", "s", &og1_1);
    assert(ret == 0);

    cargo_destroy(&mod2);

    return ret;
}
Example #4
0
int main(int argc, char **argv)
{
	int ret = 0;
	cargo_t cargo;
	debug_level_t debug_level = NONE;
	debug_level2_t debug_level2 = NONE2;

	if (cargo_init(&cargo, 0, "%s", argv[0]))
	{
		fprintf(stderr, "Failed to init command line parsing\n");
		return -1;
	}

	// Combine flags using OR.
	ret |= cargo_add_option(cargo, 0,
			"--verbose -v", "Verbosity level",
			"b|", &debug_level, 4, ERROR, WARN, INFO, DEBUG);

	// Store the flags and overwrite previous value.
	ret |= cargo_add_option(cargo, 0,
			"--loud -l", "Loudness level",
			"b_", &debug_level2, 4, ERROR2, WARN2, INFO2, DEBUG2);

	if (cargo_parse(cargo, 0, 1, argc, argv))
	{
		return -1;
	}

	printf("debug level: 0x%x\n", debug_level);
	if (debug_level & ERROR) printf("ERROR\n");
	if (debug_level & WARN)  printf("WARN\n");
	if (debug_level & INFO)  printf("INFO\n");
	if (debug_level & DEBUG) printf("DEBUG\n");

	printf("debug level2: %d\n", debug_level2);
	if (debug_level2 == ERROR2) printf("ERROR2\n");
	if (debug_level2 == WARN2)  printf("WARN2\n");
	if (debug_level2 == INFO2)  printf("INFO2\n");
	if (debug_level2 == DEBUG2) printf("DEBUG2\n");

	cargo_destroy(&cargo);

	return 0;
}
Example #5
0
int main(int argc, char **argv)
{
    cargo_t cargo;
    int ret = 0;
    int *integers = NULL;
    size_t i;
    size_t integer_count = 0;

    if (cargo_init(&cargo, 0, "%s", argv[0]))
    {
        fprintf(stderr, "Failed to init command line parsing\n");
        return -1;
    }

    ret |= cargo_add_option(cargo, 0, "--integers",
                        "A list of integers",
                        "[i]+", &integers, &integer_count);

    ret |= cargo_add_validation(cargo, 0, "--integers",
                                even_validate_int(20));

    assert(ret == 0);

    if (argc < 2)
    {
        cargo_print_usage(cargo, 0);
        return -1;
    }

    if (cargo_parse(cargo, 0, 1, argc, argv))
    {
        return -1;
    }

    for (i = 0; i < integer_count; i++)
    {
        printf("%2lu: %d\n", i+1, integers[i]);
    }

    cargo_destroy(&cargo);

    return 0;
}
Example #6
0
int main(int argc, char **argv)
{
    cargo_t cargo;
    int ret = 0;
    size_t i;
    char **args = NULL;
    size_t args_count = 0;
    int verbose = 0;
    int help = 0;
    int stop_index = 0;
    cmd_t cmd = INVALID_COMMAND;

    if (cargo_init(&cargo, CARGO_AUTOCLEAN | CARGO_NO_AUTOHELP, "%s", argv[0]))
    {
        fprintf(stderr, "Failed to init command line parsing\n");
        return -1;
    }

    //
    // Commands, one required, but only one at a time.
    //
    ret |= cargo_add_mutex_group(cargo,
            CARGO_MUTEXGRP_ONE_REQUIRED |
            CARGO_MUTEXGRP_GROUP_USAGE,
            "cmds", "Commands", NULL);
    ret |= cargo_mutex_group_set_metavar(cargo, "cmds", "COMMAND");

    // This option will stop the parsing.
    ret |= cargo_add_option(cargo, CARGO_OPT_NOT_REQUIRED | CARGO_OPT_STOP,
            "<!cmds> command1",
            "Silly example command", "c", parse_command_cb, &cmd);

    // This is just a dummy, it will not parse anything that is left to command1
    // but still show up in usage and so on.
    ret |= cargo_add_option(cargo, CARGO_OPT_NOT_REQUIRED,
            "<!cmds> command2",
            "Another silly example command", "c0", NULL, NULL);

    ret |= cargo_add_option(cargo, CARGO_OPT_NOT_REQUIRED,
            "<!cmds> command3",
            "The third silly command", "D");

    //
    // Some other arguments.
    //

    ret |= cargo_add_option(cargo, 0, "--help --usage -h", NULL, "b", &help);
    ret |= cargo_set_option_description(cargo, "--help",
            "Show this help.                         \n"
            "To get help for any of the commands:\n"
            " %s COMMAND --help", argv[0]);

    // Allow -vvv or multiple --verbosity and -v are counted to raise the count.
    ret |= cargo_add_option(cargo, 0, "--verbose -v",
            "Verbosity", "b!", &verbose);

    ret |= cargo_add_option(cargo, CARGO_OPT_NOT_REQUIRED, "args",
            "Some more args", "D", &args, &args_count);
    ret |= cargo_set_metavar(cargo, "args", "ARGS ...");

    assert(ret == 0);

    if (cargo_parse(cargo, CARGO_NOERR_OUTPUT,
                    1, argc, argv))
    {
        if (help)
        {
            cargo_print_usage(cargo, 0);
            return 0;
        }

        fprintf(stderr, "%s\n", cargo_get_error(cargo));
        return -1;
    }

    if (help)
    {
        cargo_print_usage(cargo, 0);
        return 0;
    }

    stop_index = cargo_get_stop_index(cargo);
    printf("Stop index: %d\n", stop_index);

    switch (cmd)
    {
        default:
        case INVALID_COMMAND: printf("Invalid command\n"); break;
        case COMMAND1: printf("Command 1\n"); subcommand1(stop_index, argc, argv); break;
        case COMMAND2: printf("Command 2\n"); subcommand2(stop_index, argc, argv); break;
        case COMMAND3: printf("Command 3\n"); break;
    }

    if (verbose)
    {
        printf("Got %lu extra arguments\n", args_count);
    }

    if (verbose >= 2)
    {
        for (i = 0; i < args_count; i++)
        {
            printf("%s\n", args[i]);
        }
    }

    cargo_destroy(&cargo);
    return 0;
}
Example #7
0
int main(int argc, char **argv)
{
    int ret = 0;
    cargo_t cargo;
    size_t i;
    const char **args = NULL;
    size_t args_count = 0;
    rect_t rect;
    rect_t *rects = NULL;
    size_t rect_count = 0;
    rect_t squares[4];
    size_t squares_count = 0;
    memset(&rect, 0, sizeof(rect));

    if (cargo_init(&cargo, 0, "%s", argv[0]))
    {
        fprintf(stderr, "Failed to init command line parsing\n");
        return -1;
    }

    ret |= cargo_add_option(cargo, 0, "--rect","The rect",
                            "c", parse_rect_cb, &rect);

    // Allocated array with "unlimited" count.
    ret |= cargo_add_option(cargo, 0, "--rectangles --rects -r", "Rectangles",
                            "[c]+", parse_rect_list_cb, &rects, &rect_count);

    // Fixed size array.
    ret |= cargo_add_option(cargo, 0, "--squares --sq -s", "Squares",
                            "[c]#", parse_rect_static_list_cb,
                            &squares, &squares_count,
                            sizeof(squares) / sizeof(squares[0])); // Max elements.
    assert(ret == 0);

    if (cargo_parse(cargo, 0, 1, argc, argv))
    {
        return -1;
    }

    args = cargo_get_args(cargo, &args_count);

    // Print remaining commandline arguments.
    for (i = 0; i < args_count; i++)
    {
        printf("Extra argument: %s\n", args[i]);
    }

    printf("Lone Rectangle: %d x %d\n", rect.width, rect.height);

    for (i = 0; i < rect_count; i++)
    {
        printf("Rectangle %lu: %d x %d\n",
              (i + 1), rects[i].width, rects[i].height);
    }

    for (i = 0; i < squares_count; i++)
    {
        printf("Square %lu: %d x %d\n",
              (i + 1), squares[i].width, squares[i].height);
    }


    cargo_destroy(&cargo);

    return 0;
}