Esempio n. 1
0
static int parse_cmdline_opts(int argc, char **argv)
{
    int c;
    int opt_index;

    struct option long_opts[] = {
        {"version",         no_argument,        &version_flag,      1},
        {"verbose",         no_argument,        &verbose_flag,      1},
        {"debug",           no_argument,        &debug_flag,        1},
        {"help",            no_argument,        &help_flag,         1},
        {"no-daemonize",    no_argument,        &daemonize_flag,    0},
        {"port",            required_argument,  NULL,               'p'},
        {"config-file",     required_argument,  NULL,               'c'},
        {"bin-dir",         required_argument,  NULL,               'b'},
        {NULL,              0,                  NULL,               0}
    };

    while(1) {
        opt_index = 0;
        c = getopt_long(argc, argv, "Dp:c:b:", long_opts, &opt_index);

        /* end of options */
        if (c == -1) {
            break;
        }

        switch(c) {
            case 0:
                /* option set flag, do nothing */
                break;
            case 'D':
                daemonize_flag = 0;
                break;
            case 'p':
                cport = optarg;
                break;
            case 'c':
                fs_set_config_file(optarg);
                break;
            case 'b':
                fsa_set_bin_dir(optarg);
                break;
            case '?':
                /* getopt_long has already printed err msg */
                print_usage(1);
                return -1;
            default:
                abort();
        }
    }

    /* get remaining command line args */
    /*
    if (optind < argc) {
        while (optind < argc) {
            printf("arg: %s\n", argv[optind++]);
        }
    }
    */
    return 0;
}
Esempio n. 2
0
/* Parse command line opts/args into variables */
static int parse_cmdline_opts(int argc)
{
    fsa_error(LOG_DEBUG, "parsing command line arguments/options");

    int c;
    int opt_index = 0;

    /* verbosity level flags */
    int verbose_flag = 0;
    int quiet_flag = 0;
    int debug_flag = 0;

    struct option long_opts[] = {
        {"help",        no_argument,        &help_flag,     1},
        {"quiet",       no_argument,        &quiet_flag,    1},
        {"verbose",     no_argument,        &verbose_flag,  1},
        {"debug",       no_argument,        &debug_flag,    1},
        {"version",     no_argument,        &version_flag,  1},
        {"config-file", required_argument,  NULL,           'c'},
        {NULL,          0,                  NULL,           0}
    };

    while(1) {
        c = getopt_long(argc, argv, "+c:", long_opts, &opt_index);

        /* end of options */
        if (c == -1) {
            break;
        }

        switch(c) {
            case 0:
                /* option set flag, do nothing */
                break;
            case '?':
                /* getopt_long has already printed err msg */
                print_usage(1);
                return 1;
            case 'c':
                fs_set_config_file(optarg);
                break;
            default:
                abort();
        }
    }

    /* set verbosity based on options */
    if (debug_flag) {
        verbosity = V_DEBUG;
        fsa_log_level = LOG_DEBUG;
    }
    else if (verbose_flag) {
        verbosity = V_VERBOSE;
    }
    else if (quiet_flag) {
        verbosity = V_QUIET;
    }
    else {
        verbosity = V_NORMAL;
    }

    /* get admin command */
    if (optind < argc) {
        cmd_index = optind;
        optind += 1; /* move to start of arguments to command */
        if (strcmp(argv[cmd_index], "help") == 0) {
            help_flag = 1;
        }
        else if (strcmp(argv[cmd_index], "version") == 0) {
            version_flag = 1;
        }

        if (optind < argc) {
            /* track start of arguments to command */
            args_index = optind;
        }
    }

    return 0;
}