Beispiel #1
0
/*
 *   Create option parser for program named [prog]
 */
optparse_t optparse_create (const char *prog)
{
    struct optparse_option help = {
        .name = "help",
        .key  = 'h',
        .usage = "Display this message.",
        .cb    = (optparse_cb_f) display_help,
    };

    struct opt_parser *p = malloc (sizeof (*p));
    if (!p)
        return NULL;

    if (!(p->program_name = strdup (prog))) {
        free (p);
        return NULL;
    }
    p->usage = NULL;
    p->option_list = list_create ((ListDelF) option_info_destroy);
    if (!p->option_list) {
        free (p);
        return NULL;
    }

    p->log_fn = &log_stderr;
    p->fatalerr_fn = &log_stderr_exit;
    p->fatalerr_handle = NULL;
    p->left_margin = 2;
    p->option_width = 25;

    /*
     *  Register -h, --help
     */
    help.arg = (void *) p;
    if (optparse_add_option (p, &help) != OPTPARSE_SUCCESS) {
        fprintf (stderr, "failed to register --help option: %s\n", strerror (errno));
        optparse_destroy (p);
        return (NULL);
    }

    return (p);
}

/*
 *  Search for option with [name] in parser [p] returns -1 if option
 *   was never registered with this parser, 0 if option was not used,
 *   and 1 if option was specified in args. Returns pointer to optargp
 *   if non-NULL.
 */
int optparse_getopt (optparse_t p, const char *name, const char **optargp)
{
    struct option_info *c;
    if (!(c = find_option_info (p, name)))
        return (-1);

    if (c->found) {
        if (optargp)
            *optargp = c->optarg;
        return (1);
    }
    return (0);
}

bool optparse_hasopt (optparse_t p, const char *name)
{
    int n;
    if ((n = optparse_getopt (p, name, NULL)) < 0) {
        (*p->fatalerr_fn) (p->fatalerr_handle, 1,
                           "%s: optparse error: no such argument '%s'\n",
                           p->program_name, name);
        return false;
    }
    return (n > 0);
}
Beispiel #2
0
static optparse_t * setup_optparse_parse_args (int argc, char *argv[])
{
    optparse_err_t e;
    struct optparse_option helpopt = {
        .name = "help", .key = 'h', .usage = "Display this message"
    };
    optparse_t *p = optparse_create ("flux");
    if (p == NULL)
        log_err_exit ("optparse_create");
    optparse_set (p, OPTPARSE_USAGE, "[OPTIONS] COMMAND ARGS");
    e = optparse_add_option_table (p, opts);
    if (e != OPTPARSE_SUCCESS)
        log_msg_exit ("optparse_add_option_table() failed");

    // Remove automatic `--help' in favor of our own usage() from above
    e = optparse_remove_option (p, "help");
    if (e != OPTPARSE_SUCCESS)
        log_msg_exit ("optparse_remove_option (\"help\")");
    e = optparse_add_option (p, &helpopt);
    if (e != OPTPARSE_SUCCESS)
        log_msg_exit ("optparse_add_option (\"help\")");

    // Don't print internal subcommands in --help (we print subcommands using
    //  emit_command_help() above.
    e = optparse_set (p, OPTPARSE_PRINT_SUBCMDS, 0);
    if (e != OPTPARSE_SUCCESS)
        log_msg_exit ("optparse_set (OPTPARSE_PRINT_SUBCMDS");

    register_builtin_subcommands (p);

    if (optparse_parse_args (p, argc, argv) < 0)
        exit (1);

    return (p);
}


int main (int argc, char *argv[])
{
    bool vopt = false;
    struct environment *env;
    optparse_t *p;
    const char *searchpath;
    const char *argv0 = argv[0];
    int flags = 0;

    log_init ("flux");

    p = setup_optparse_parse_args (argc, argv);

    if (!flux_is_installed ())
        flags |= CONF_FLAG_INTREE;
    optparse_set_data (p, "conf_flags", &flags);

    if (optparse_hasopt (p, "help")) {
        usage (p); // N.B. accesses "conf_flags"
        exit (0);
    }
    optind = optparse_optind (p);
    if (argc - optind == 0) {
        usage (p);
        exit (1);
    }
    vopt = optparse_hasopt (p, "verbose");

    /* prepare the environment 'env' that will be passed to subcommands.
     */
    env = environment_create ();

    /* Add PATH to env and prepend path to this executable if necessary.
     */
    setup_path (env, argv0);

    /* Prepend config values to env values.
     * Note special handling of lua ;; (default path).
     */
    environment_from_env (env, "LUA_CPATH", "", ';');
    environment_no_dedup_push_back (env, "LUA_CPATH", ";;");
    environment_push (env, "LUA_CPATH", flux_conf_get ("lua_cpath_add", flags));
    environment_push (env, "LUA_CPATH", getenv ("FLUX_LUA_CPATH_PREPEND"));

    environment_from_env (env, "LUA_PATH", "", ';');
    environment_no_dedup_push_back (env, "LUA_PATH", ";;");
    environment_push (env, "LUA_PATH", flux_conf_get ("lua_path_add", flags));
    environment_push (env, "LUA_PATH", getenv ("FLUX_LUA_PATH_PREPEND"));

    environment_from_env (env, "PYTHONPATH", "", ':');
    environment_push (env, "PYTHONPATH", flux_conf_get ("python_path", flags));
    environment_push (env, "PYTHONPATH", getenv ("FLUX_PYTHONPATH_PREPEND"));

    environment_from_env (env, "MANPATH", "", ':');
    environment_push (env, "MANPATH", flux_conf_get ("man_path", flags));

    environment_from_env (env, "FLUX_EXEC_PATH", "", ':');
    environment_push (env, "FLUX_EXEC_PATH",
                      flux_conf_get ("exec_path", flags));
    environment_push (env, "FLUX_EXEC_PATH", getenv ("FLUX_EXEC_PATH_PREPEND"));

    environment_from_env (env, "FLUX_CONNECTOR_PATH", "", ':');
    environment_push (env, "FLUX_CONNECTOR_PATH",
                      flux_conf_get ("connector_path", flags));
    environment_push (env, "FLUX_CONNECTOR_PATH",
                      getenv ("FLUX_CONNECTOR_PATH_PREPEND"));

    environment_from_env (env, "FLUX_MODULE_PATH", "", ':');
    environment_push (env, "FLUX_MODULE_PATH",
                      flux_conf_get ("module_path", flags));
    environment_push (env, "FLUX_MODULE_PATH",
                      getenv ("FLUX_MODULE_PATH_PREPEND"));

    /* Set FLUX_SEC_DIRECTORY, possibly to $HOME/.flux.
     */
    setup_keydir (env, flags);

    if (getenv ("FLUX_URI"))
        environment_from_env (env, "FLUX_URI", "", 0); /* pass-thru */

    environment_from_env (env, "FLUX_RC1_PATH",
                          flux_conf_get ("rc1_path", flags), 0);
    environment_from_env (env, "FLUX_RC3_PATH",
                          flux_conf_get ("rc3_path", flags), 0);
    environment_from_env (env, "FLUX_PMI_LIBRARY_PATH",
                          flux_conf_get ("pmi_library_path", flags), 0);
    environment_from_env (env, "FLUX_WRECK_LUA_PATTERN",
                          flux_conf_get ("wreck_lua_pattern", flags), 0);
    environment_from_env (env, "FLUX_WREXECD_PATH",
                          flux_conf_get ("wrexecd_path", flags), 0);

    environment_apply(env);
    optparse_set_data (p, "env", env);

    if (vopt)
        print_environment (env);
    if (optparse_get_subcommand (p, argv [optind])) {
        if (optparse_run_subcommand (p, argc, argv) < 0)
            exit (1);
    } else {
        searchpath = environment_get (env, "FLUX_EXEC_PATH");
        if (vopt)
            printf ("sub-command search path: %s\n", searchpath);
        exec_subcommand (searchpath, vopt, argv + optind);
    }

    environment_destroy (env);
    optparse_destroy (p);
    log_fini ();

    return 0;
}