Librairie Apr : tutoriel : getopt-sample.c

Note

Regardez le tutoriel au complet, en Anglais, ici.
Vous trouverez tout ce tutoriel séparé en plusieurs pages ici.
Ce fichier est l’exemple le plus simple pour mettre en oeuvre les fonctions apr_xx
Il vient d’ici.

/**
 * Exemple du tutoriel apr
 * http://dev.ariel-networks.com/apr/
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <apr_general.h>
#include <apr_getopt.h>

/**
 * command line options sample code
 * @remark Error checks omitted
 */
int main(int argc, const char *argv[])
{
    apr_status_t rv;
    apr_pool_t *mp;
    /* API is data structure driven */
    static const apr_getopt_option_t opt_option[] = {
        /* -i nomfichier or --in nomfichier : */
        { "in", 'i', TRUE, "fichier entrant" },
        /* -o nomfichier or --out nomfichier : */
        { "out", 'o', TRUE, "fichier sortant" },
        /* -h or --help : */
        { "help", 'h', FALSE, "voir l'aide" },
        /* sentinelle de fin : */
        { NULL, 0, 0, NULL },
    };
    apr_getopt_t *opt;
    int optch;
    const char *optarg;

    apr_initialize();
    apr_pool_create(&mp, NULL);

    /* initialize apr_getopt_t */
    apr_getopt_init(&opt, mp, argc, argv);

    /* parcourir toutes les options via opt_option[] */
    while ((rv = apr_getopt_long(opt,opt_option,
                                 &optch,
                                 &optarg)) == APR_SUCCESS) {
        switch (optch) {
        case 'i':
            printf("opt=i, %s\n", optarg);
            break;
        case 'o':
            printf("opt=o, %s\n", optarg);
            break;
        case 'h':
            printf("afficher l'aide\n");  /* no arg */
            break;
        }
    }
    if (rv != APR_EOF) {
        printf("mauvaises options\n");
    }

    apr_terminate();
    return 0;
}

One comment

  1. Ping : Librairie Apr : tutoriels 10 et 11

Poster un commentaire

Vous devriez utiliser le HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.