Example #1
0
File: beeuniq.c Project: bee/bee
int main(int argc, char *argv[])
{
    int  option_index = 0;
    int  c = 0;
    int  max, i;

    char delimiter = ' ';

    struct option long_options[] = {
        {"delimiter",   required_argument, 0, OPT_DELIMITER},

        {"version",     no_argument, 0, OPT_VERSION},
        {"help",        no_argument, 0, OPT_HELP},

        {0, 0, 0, 0}
    };

    while ((c = getopt_long_only(argc, argv, "hvd:", long_options, &option_index)) != -1) {

        switch (c) {
            case OPT_DELIMITER:
                if (!optarg[0] || optarg[1]) {
                     fprintf(stderr, "invalid delimiter '%s'\n", optarg);
                     exit(EXIT_FAILURE);
                }
                delimiter = optarg[0];
                break;

            case OPT_HELP:
                print_version();
                printf("\n");
                print_full_usage();
                printf("\n");
                exit(EXIT_SUCCESS);

            case OPT_VERSION:
                print_version();
                exit(EXIT_SUCCESS);
        }
    }  /* end while getopt_long_only */

    if(argc-optind < 1) {
        print_full_usage();
        exit(EXIT_FAILURE);
    }

    max  = bee_uniq(argc-optind, &argv[optind]);
    max += optind-1;

    for(i=optind; i <= max; i++) {
        fputs(argv[i], stdout);
        if(max-i)
            putchar(delimiter);
    }

    putchar('\n');

    return(EXIT_SUCCESS);
}
Example #2
0
int print_command_usage(struct command *cmds, char *command)
{
	struct command	*cmd;
	struct command	nullcmd = {NULL, NULL, 0, NULL, NULL};

	for (cmd = cmds; memcmp(cmd, &nullcmd, sizeof(nullcmd)) != 0; cmd++) {
		if (cmd->name)
			if (strcmp(cmd->name, command) == 0) {
				print_full_usage(cmd);
				return 0;
			}
	}
	printf("invalid command: %s\n", command);
	return 	1;
}
Example #3
0
File: beecut.c Project: bee/bee
int main(int argc, char *argv[])
{
    int  option_index = 0;
    int  c = 0;

    char delimiter = '.';

    char opt_short   = 0;
    char opt_newline = 0;

    char *opt_prepend = "";
    char *opt_append  = opt_prepend;

    struct option long_options[] = {
        {"delimiter",   required_argument, 0, OPT_DELIMITER},

        {"prepend",     required_argument, 0, OPT_PREPEND},
        {"append",      required_argument, 0, OPT_APPEND},

        {"short",       no_argument, 0, OPT_SHORT},
        {"newline",     no_argument, 0, OPT_NEWLINE},

        {"version",     no_argument, 0, OPT_VERSION},
        {"help",        no_argument, 0, OPT_HELP},

        {0, 0, 0, 0}
    };

    while ((c = getopt_long_only(argc, argv, "p:a:d:sn", long_options, &option_index)) != -1) {

        switch (c) {
            case OPT_DELIMITER:
                if (!optarg[0] || optarg[1]) {
                     fprintf(stderr, "invalid delimiter '%s'\n", optarg);
                     exit(EXIT_FAILURE);
                }
                delimiter = optarg[0];
                break;

            case OPT_PREPEND:
                opt_prepend = optarg;
                break;

            case OPT_APPEND:
                opt_append = optarg;
                break;

            case OPT_SHORT:
                opt_short = 1;
                break;

            case OPT_NEWLINE:
                opt_newline = 1;
                break;

            case OPT_HELP:
                printf("\n");
                print_version();
                printf("\n");
                print_full_usage();
                exit(EXIT_SUCCESS);

            case OPT_VERSION:
                print_version();
                exit(EXIT_SUCCESS);

            case '?':
                exit(EXIT_FAILURE);
        }
    }  /* end while getopt_long_only */

    if(argc-optind < 1) {
        print_full_usage();
        exit(EXIT_FAILURE);
    }

    while(optind < argc)
        cut_and_print(argv[optind++], delimiter, opt_short,
                      opt_newline, opt_prepend, opt_append);

    return(0);
}