C++ Konsolenparameter werden teilweise ausgelesen



  • Hi zusammen!

    Ich schreibe zur Zeit ein Konsolenprogramm. Dieses soll auch per Konsolenpamater verschiedene Dinge ausführen. Leider werden die Argumente "-h" und "-t" nicht ausgewertet. Der Parameter "-v" dagegen schon.

    Sieht jemand den Fehler? Anscheinend übersehe ich den Fehler...

    Der Code:

    main.cpp

    #include "getopt.h"
    
    #include <cstdio>
    #include <iostream>
    
    #define VERSION_MAJOR 0
    #define VERSION_MINOR 01
    
    void printHelp(char *progname) {
        std::cout << "Usage: %s -f filename [OPTIONS]" << std::endl;
        std::cout << "OPTIONS:" << std::endl;
        std::cout << "-t, --test - Test all functions in program" << std::endl;
        std::cout << "-v, --version - Print version and exit" << std::endl;
        std::cout << progname << std::endl;
    }
    
    void printHelp() {
        std::cout << "Usage: %s -f filename [OPTIONS]" << std::endl;
        std::cout << "OPTIONS:" << std::endl;
        std::cout << "-t, --test - Test all functions in program" << std::endl;
        std::cout << "-v, --version - Print version and exit" << std::endl;
    }
    
    int main(int argc, char** argv) {
    
        //int ret = 0;
    
        /** Parse arguments */
        while(true) {
            int c = 0;
            int option_index = 0;
    
            static struct option long_options[] = {
            {"help", no_argument,0,'h'},
            {"test", no_argument,0,'t'},
            {"version",no_argument, 0, 'v'},
            {0,0,0,0}
            };
    
            /* getopt_long stores the option index here. */
            option_index = 0;
    
            c = getopt_long(argc, argv, "h:t:v", long_options, &option_index);
    
            /* Detect the end of the options. */
            if(c == -1) {
                break;
            }
            switch(c) {
                case 'h':
                    printHelp();
                return 0;
                break;
                case 't':
                    std::cout << "test" << std::endl;
                return 0;
                break;
                case 'v':
                    std::cout << "programm version " << VERSION_MAJOR << "." << VERSION_MINOR << std::endl;
                    //printf("programm version %d.%d\n", VERSION_MAJOR, VERSION_MINOR);
                return 0;
                default:
                    ;
            }
        std::cout << "no argument" << std::endl;
        return 0;
    }
    

    getopt.h

    #ifndef __GETOPT_H__
    /*
     * getopt.h
     *
     * $Id$
     *
     * Defines constants and function prototypes required to implement
     * the `getopt', `getopt_long' and `getopt_long_only' APIs.
     *
     * This file is part of the MinGW32 package set.
     *
     * Contributed by Keith Marshall <keithmarshall@users.sourceforge.net>
     * Last modification: 04-Jan-2008
     *
     * THIS SOFTWARE IS NOT COPYRIGHTED
     *
     * This source code is offered for use in the public domain. You may
     * use, modify or distribute it freely.
     *
     * This code is distributed in the hope that it will be useful but
     * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
     * DISCLAIMED. This includes but is not limited to warranties of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     *
     * $Revision$
     * $Author$
     * $Date$
     *
     */
    #define __GETOPT_H__
    
    /* All the headers include this file. */
    //#include <_mingw.h>
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    extern int optind;		/* index of first non-option in argv      */
    extern int optopt;		/* single option character, as parsed     */
    extern int opterr;		/* flag to enable built-in diagnostics... */
    				/* (user may set to zero, to suppress)    */
    
    extern char *optarg;		/* pointer to argument of current option  */
    
    extern int getopt( int, char * const [], const char * );
    
    #ifdef __cplusplus
    }
    #endif
    /*
     * POSIX requires the `getopt' API to be specified in `unistd.h';
     * thus, `unistd.h' includes this header.  However, we do not want
     * to expose the `getopt_long' or `getopt_long_only' APIs, when
     * included in this manner.  Thus, close the standard __GETOPT_H__
     * declarations block, and open an additional __GETOPT_LONG_H__
     * specific block, only when *not* __UNISTD_H_SOURCED__, in which
     * to declare the extended API.
     */
    #endif /* !defined(__GETOPT_H__) */
    #if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__)
    #define __GETOPT_LONG_H__
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    struct option		/* specification for a long form option...	*/
    {
        const char  *name;      /* option name, without leading hyphens */
        int         has_arg;       /* does it take an argument?		*/
        int         *flag;		/* where to save its status, or NULL	*/
        int         val;		/* its associated status value		*/
    };
    
    enum    		/* permitted values for its `has_arg' field...	*/
    {
        no_argument = 0,      	/* option never takes an argument	*/
        required_argument,		/* option always requires an argument	*/
        optional_argument		/* option may take an argument		*/
    };
    
    extern int getopt_long( int, char * const [], const char *, const struct option *, int * );
    extern int getopt_long_only( int, char * const [], const char *, const struct option *, int * );
    /*
     * Previous MinGW implementation had...
     */
    #ifndef HAVE_DECL_GETOPT
    /*
     * ...for the long form API only; keep this for compatibility.
     */
    # define HAVE_DECL_GETOPT	1
    #endif
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */
    /* $RCSfile$Revision$: end of file */
    

    Danke



  • Ein kleiner Nachtrag:

    Auf der Linux Konsole wird als Beispiel ausgegeben:

    ./sammlung : option requires an argument -- 't'
    no argument

    bzw.

    programm versio 0.1

    Wieso wird beim Parameter "-t" nicht "Test" auf der Konsole ausgegeben?



  • s. getopt_long bzw. als Beispiel C: getopt_long example: Accessing command line arguments
    Durch die Angabe "h:t:v" verlangen 'h' und 't' noch jeweils ein weiteres Argument.
    Du willst nur "htv".



  • Danke für die schnell Hilfe.



  • Wenn ich gerne Parameter wie "-t1" (Test1) oder "-t2" (Test) bzw. längere Parameter (für mehrere passen Unterscheidungen), wie müsste es aussehen?

    Stichwort "getopts" in der bash. Da ich laut der Anleitung

    [url]
    https://stackoverflow.com/questions/10402536/how-can-i-get-getopts-source
    [/url]

    dies als Header erzeugen wollte, aber nicht funktionierte, bitte ich euch um Hilfe.

    Danke


Anmelden zum Antworten