Exemple #1
0
void scan_options(struct option_t* option, int argc, char **argv)
{
    int i;
    if (argc <= 1) {
            option->help = TRUE;
            return;
    }
    for (i = 1; i < argc; i++) {
        if (is_groovy_help_option(argv[i])) {
            option->help = TRUE;
            return;
        }

        if (is_client_option(argv[i])) {
            char* name = argv[i]+strlen(CLIENT_OPTION_PREFIX);
            char* argvi_copy = argv[i];
            argv[i] = NULL;
            struct option_info_t* opt = what_option(name);

            if (opt == NULL) {
                fprintf(stderr, "ERROR: unknown option %s\n", argvi_copy);
                usage();
                exit(1);
            }

            char* value = NULL;
            if (opt->take_value == TRUE) {
                if (i >= argc-1) {
                    fprintf(stderr, "ERROR: option %s require param\n", argvi_copy);
                    usage();
                    exit(1);
                }
                i++;
                value = argv[i];
                argv[i] = NULL;
            }

            switch (opt->type) {
            case OPT_WITHOUT_INVOCATION_SERVER:
                option->without_invocation_server = TRUE;
                break;
            case OPT_PORT:
                if (sscanf(value, "%d", &option->port) != 1) {
                    fprintf(stderr, "ERROR: port number %s of option %s error\n", value, argvi_copy);
                    exit(1);
                }
                break;
            case OPT_KILL_SERVER:
                if (option->restart) {
                    fprintf(stderr, "ERROR: can't specify both of kill & restart\n");
                    exit(1);
                }
                option->kill = TRUE;
                break;
            case OPT_RESTART_SERVER:
                if (option->kill) {
                    fprintf(stderr, "ERROR: can't specify both of kill & restart\n");
                    exit(1);
                }
                option->restart = TRUE;
                break;
            case OPT_QUIET:
                option->quiet = TRUE;
                break;
            case OPT_ENV:
                assert(opt->take_value == TRUE);
                set_mask_option(option->env_include_mask, name, value);
                break;
            case OPT_ENV_ALL:
                option->env_all = TRUE;
                break;
            case OPT_ENV_EXCLUDE:
                assert(opt->take_value == TRUE);
                set_mask_option(option->env_exclude_mask, name, value);
                break;
            case OPT_HELP:
                usage();
                exit(1);
                break;
            default:
                assert(FALSE);
            }
        }
    }
}
Exemple #2
0
void scan_options(struct option_t* option, int argc, char **argv)
{
    int i;
    if (argc <= 1) {
        option->help = TRUE;
        return;
    }
    for (i = 1; i < argc; i++) {
        if (is_groovy_help_option(argv[i])) {
            option->help = TRUE;
        }

        if (is_groovy_version_option(argv[i])) {
            option->version = TRUE;
        }

        if (is_client_option(argv[i])) {
            char* name = argv[i] + strlen(CLIENT_OPTION_PREFIX);
            char* argvi_copy = argv[i];
            argv[i] = NULL;
            struct option_info_t* opt = what_option(name);

            if (opt == NULL) {
                fprintf(stderr, "ERROR: unknown option %s\n", argvi_copy);
                usage();
                exit(1);
            }

            char* value = NULL;
            if (opt->take_value == TRUE) {
                if (i >= argc-1) {
                    fprintf(stderr, "ERROR: option %s requires param\n", argvi_copy);
                    usage();
                    exit(1);
                }
                i++;
                value = argv[i];
                argv[i] = NULL;
            }

            switch (opt->type) {
            case OPT_WITHOUT_INVOCATION_SERVER:
                option->without_invocation_server = TRUE;
                break;
            case OPT_HOST:
                assert(opt->take_value == TRUE);
                option->host = value;
                break;
            case OPT_PORT:
                assert(opt->take_value == TRUE);
                if (sscanf(value, "%d", &option->port) != 1) {
                    fprintf(stderr, "ERROR: port number %s of option %s error\n", value, argvi_copy);
                    exit(1);
                }
                break;
            case OPT_AUTHTOKEN:
                assert(opt->take_value == TRUE);
                option->authtoken = value;
                break;
            case OPT_KILL_SERVER:
                option->kill = TRUE;
                break;
            case OPT_RESTART_SERVER:
                option->restart = TRUE;
                break;
            case OPT_QUIET:
                option->quiet = TRUE;
                break;
            case OPT_ENV:
                assert(opt->take_value == TRUE);
                set_mask_option(option->env_include_mask, name, value);
                break;
            case OPT_ENV_ALL:
                option->env_all = TRUE;
                break;
            case OPT_ENV_EXCLUDE:
                assert(opt->take_value == TRUE);
                set_mask_option(option->env_exclude_mask, name, value);
                break;
            case OPT_HELP:
                usage();
                exit(0); // because client's usage is printable without server communication
                break;
            case OPT_VERSION:
                version();
                exit(0); // because client's version is printable without server communication
                break;
            default:
                assert(FALSE);
            }
        }
    }

    // check unavailable combination of options
    if (option->kill && option->restart) {
        fprintf(stderr, "ERROR: cannot specify both of kill & restart\n");
        exit(1);
    }
    if (option->host != NULL) {
        if (option->restart) {
            fprintf(stderr, "ERROR: cannot specify restart-server to explicitly specified host\n");
            exit(1);
        }
        if (option->kill) {
            fprintf(stderr, "ERROR: cannot specify kill-server to explicitly specified host\n");
            exit(1);
        }
    }
}