Parameter parsing error vinf getopt not option please refer to the

In C, getopt_long does not parse the optional arguments to command line parameters parameters. When I run the program, the optional argument is not recognized like the example run below. $ ./resp...

The man page certainly doesn’t document it very well, but the source code helps a little.

Briefly: you’re supposed to do something like the following (though this may be a little over-pedantic):

if(   !optarg
   && optind < argc // make sure optind is valid
   && NULL != argv[optind] // make sure it's not a null string
   && '' != argv[optind][0] // ... or an empty string
   && '-' != argv[optind][0] // ... or another option
  ) {
  // update optind so the next getopt_long invocation skips argv[optind]
  my_optarg = argv[optind++];
}
/* ... */

From among the comments preceding _getopt_internal:

If getopt finds another option character, it returns that character,
updating optind and nextchar so that the next call to getopt can
resume the scan with the following option character or ARGV-element.

If there are no more option characters, getopt returns -1.
Then optind is the index in ARGV of the first ARGV-element
that is not an option. (The ARGV-elements have been permuted
so that those that are not options now come last.) <-- a note from me:
if the 3rd argument to getopt_long starts with a dash, argv will not
be permuted

If a char in OPTSTRING is followed by a colon, that means it wants an arg,
so the following text in the same ARGV-element, or the text of the following
ARGV-element, is returned in optarg. Two colons mean an option that
wants an optional arg; if there is text in the current ARGV-element,
it is returned in optarg, otherwise optarg is set to zero.

… though you have to do some reading between the lines. The following does what you want:

#include <stdio.h>
#include <getopt.h>

int main(int argc, char* argv[] ) {
  int getopt_ret;
  int option_index;
  static struct option long_options[] = {
      {"praise",  required_argument, 0, 'p'}
    , {"blame",  optional_argument, 0, 'b'}
    , {0, 0, 0, 0}
  };

  while( -1 != ( getopt_ret = getopt_long(  argc
                                          , argv
                                          , "p:b::"
                                          , long_options
                                          , &option_index) ) ) {
    const char *tmp_optarg = optarg;
    switch( getopt_ret ) {
      case 0: break;
      case 1:
        // handle non-option arguments here if you put a `-`
        // at the beginning of getopt_long's 3rd argument
        break;
      case 'p':
        printf("Kudos to %sn", optarg); break;
      case 'b':
        if(   !optarg
           && NULL != argv[optind]
           && '-' != argv[optind][0] ) {
          // This is what makes it work; if `optarg` isn't set
          // and argv[optind] doesn't look like another option,
          // then assume it's our parameter and overtly modify optind
          // to compensate.
          //
          // I'm not terribly fond of how this is done in the getopt
          // API, but if you look at the man page it documents the
          // existence of `optarg`, `optind`, etc, and they're
          // not marked const -- implying they expect and intend you
          // to modify them if needed.
          tmp_optarg = argv[optind++];
        }
        printf( "You suck" );
        if (tmp_optarg) {
          printf (", %s!n", tmp_optarg);
        } else {
          printf ("!n");
        }
        break;
      case '?':
        printf("Unknown optionn");
        break;
      default:
        printf( "Unknown: getopt_ret == %dn", getopt_ret );
        break;
    }
  }
  return 0;
}

/** @file * IPRT — Command Line Parsing. */ /* * Copyright (C) 2007-2012 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the «COPYING» file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. * * The contents of this file may alternatively be used under the terms * of the Common Development and Distribution License Version 1.0 * (CDDL) only, as it comes in the «COPYING.CDDL» file of the * VirtualBox OSE distribution, in which case the provisions of the * CDDL are applicable instead of those of the GPL. * * You may elect to license modified versions of this file under the * terms and conditions of either the GPL or the CDDL or both. */ #ifndef ___iprt_getopt_h #define ___iprt_getopt_h #include <iprt/cdefs.h> #include <iprt/types.h> RT_C_DECLS_BEGIN /** @defgroup grp_rt_getopt RTGetOpt — Command Line Parsing * @ingroup grp_rt * @{ */ /** @name Values for RTGETOPTDEF::fFlags and the fFlags parameter of * RTGetOptFetchValue. * * @remarks When neither of the RTGETOPT_FLAG_HEX, RTGETOPT_FLAG_OCT and RTGETOPT_FLAG_DEC * flags are specified with a integer value format, RTGetOpt will default to * decimal but recognize the 0x prefix when present. RTGetOpt will not look for * for the octal prefix (0). * @{ */ /** Requires no extra argument. * (Can be assumed to be 0 for ever.) */ #define RTGETOPT_REQ_NOTHING 0 /** A value is required or error will be returned. */ #define RTGETOPT_REQ_STRING 1 /** The value must be a valid signed 8-bit integer or an error will be returned. */ #define RTGETOPT_REQ_INT8 2 /** The value must be a valid unsigned 8-bit integer or an error will be returned. */ #define RTGETOPT_REQ_UINT8 3 /** The value must be a valid signed 16-bit integer or an error will be returned. */ #define RTGETOPT_REQ_INT16 4 /** The value must be a valid unsigned 16-bit integer or an error will be returned. */ #define RTGETOPT_REQ_UINT16 5 /** The value must be a valid signed 32-bit integer or an error will be returned. */ #define RTGETOPT_REQ_INT32 6 /** The value must be a valid unsigned 32-bit integer or an error will be returned. */ #define RTGETOPT_REQ_UINT32 7 /** The value must be a valid signed 64-bit integer or an error will be returned. */ #define RTGETOPT_REQ_INT64 8 /** The value must be a valid unsigned 64-bit integer or an error will be returned. */ #define RTGETOPT_REQ_UINT64 9 /** The value must be a valid IPv4 address. * (Not a name, but 4 values in the 0..255 range with dots separating them). */ #define RTGETOPT_REQ_IPV4ADDR 10 /** The value must be a valid IPv4 CIDR. * As with RTGETOPT_REQ_IPV4ADDR, no name. */ #define RTGETOPT_REQ_IPV4CIDR 11 #if 0 /* take placers */ /** The value must be a valid IPv6 addr * @todo: Add types and parsing routines in (iprt/net.h) */ #define RTGETOPT_REQ_IPV6ADDR 12 /** The value must be a valid IPv6 CIDR * @todo: Add types and parsing routines in (iprt/net.h) */ #define RTGETOPT_REQ_IPV6CIDR 13 #endif /** The value must be a valid ethernet MAC address. */ #define RTGETOPT_REQ_MACADDR 14 /** The value must be a valid UUID. */ #define RTGETOPT_REQ_UUID 15 /** The value must be a string with value as «on» or «off». */ #define RTGETOPT_REQ_BOOL_ONOFF 16 /** Boolean option accepting a wide range of typical ways of * expression true and false. */ #define RTGETOPT_REQ_BOOL 17 /** The mask of the valid required types. */ #define RTGETOPT_REQ_MASK 31 /** Treat the value as hexadecimal — only applicable with the RTGETOPT_REQ_*INT*. */ #define RTGETOPT_FLAG_HEX RT_BIT(16) /** Treat the value as octal — only applicable with the RTGETOPT_REQ_*INT*. */ #define RTGETOPT_FLAG_OCT RT_BIT(17) /** Treat the value as decimal — only applicable with the RTGETOPT_REQ_*INT*. */ #define RTGETOPT_FLAG_DEC RT_BIT(18) /** The index value is attached to the argument — only valid for long arguments. */ #define RTGETOPT_FLAG_INDEX RT_BIT(19) /** Treat the long option as case insensitive. */ #define RTGETOPT_FLAG_ICASE RT_BIT(20) /** Mask of valid bits — for validation. */ #define RTGETOPT_VALID_MASK ( RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC | RTGETOPT_FLAG_INDEX | RTGETOPT_FLAG_ICASE) /** @} */ /** * An option definition. */ typedef struct RTGETOPTDEF { /** The long option. * This is optional */ const char *pszLong; /** The short option character. * This doesn’t have to be a character, it may also be a #define or enum value if * there isn’t any short version of this option. Must be greater than 0. */ int iShort; /** The flags (RTGETOPT_*). */ unsigned fFlags; } RTGETOPTDEF; /** Pointer to an option definition. */ typedef RTGETOPTDEF *PRTGETOPTDEF; /** Pointer to an const option definition. */ typedef const RTGETOPTDEF *PCRTGETOPTDEF; /** * Option argument union. * * What ends up here depends on argument format in the option definition. * * @remarks Integers will bet put in the a i and a u members and sign/zero extended * according to the signedness indicated by the a fFlags. So, you can choose * use which ever of the integer members for accessing the value regardless * of restrictions indicated in the a fFlags. */ typedef union RTGETOPTUNION { /** Pointer to the definition on failure or when the option doesn’t take an argument. * This can be NULL for some errors. */ PCRTGETOPTDEF pDef; /** A RTGETOPT_REQ_STRING option argument. */ const char *psz; /** A RTGETOPT_REQ_INT8 option argument. */ int8_t i8; /** A RTGETOPT_REQ_UINT8 option argument . */ uint8_t u8; /** A RTGETOPT_REQ_INT16 option argument. */ int16_t i16; /** A RTGETOPT_REQ_UINT16 option argument . */ uint16_t u16; /** A RTGETOPT_REQ_INT16 option argument. */ int32_t i32; /** A RTGETOPT_REQ_UINT32 option argument . */ uint32_t u32; /** A RTGETOPT_REQ_INT64 option argument. */ int64_t i64; /** A RTGETOPT_REQ_UINT64 option argument. */ uint64_t u64; #ifdef ___iprt_net_h /** A RTGETOPT_REQ_IPV4ADDR option argument. */ RTNETADDRIPV4 IPv4Addr; /** A RTGETOPT_REQ_IPV4CIDR option argument. */ struct { RTNETADDRIPV4 IPv4Network; RTNETADDRIPV4 IPv4Netmask; } CidrIPv4; #endif /** A RTGETOPT_REQ_MACADDR option argument. */ RTMAC MacAddr; /** A RTGETOPT_REQ_UUID option argument. */ RTUUID Uuid; /** A boolean flag. */ bool f; } RTGETOPTUNION; /** Pointer to an option argument union. */ typedef RTGETOPTUNION *PRTGETOPTUNION; /** Pointer to a const option argument union. */ typedef RTGETOPTUNION const *PCRTGETOPTUNION; /** * RTGetOpt state. */ typedef struct RTGETOPTSTATE { /** The next argument. */ int iNext; /** Argument array. */ char **argv; /** Number of items in argv. */ int argc; /** Option definition array. */ PCRTGETOPTDEF paOptions; /** Number of items in paOptions. */ size_t cOptions; /** The next short option. * (For parsing ls -latrT4 kind of option lists.) */ const char *pszNextShort; /** The option definition which matched. NULL otherwise. */ PCRTGETOPTDEF pDef; /** The index of an index option, otherwise UINT32_MAX. */ uint32_t uIndex; /** The flags passed to RTGetOptInit. */ uint32_t fFlags; /** Number of non-options that we’re skipping during a sorted get. The value * INT32_MAX is used to indicate that there are no more options. This is used * to implement ‘—‘. */ int32_t cNonOptions; /* More members may be added later for dealing with new features. */ } RTGETOPTSTATE; /** Pointer to RTGetOpt state. */ typedef RTGETOPTSTATE *PRTGETOPTSTATE; /** * Initialize the RTGetOpt state. * * The passed in argument vector may be sorted if fFlags indicates that this is * desired (to be implemented). * * @returns VINF_SUCCESS, VERR_INVALID_PARAMETER or VERR_INVALID_POINTER. * @param pState The state. * * @param argc Argument count, to be copied from what comes in with * main(). * @param argv Argument array, to be copied from what comes in with * main(). This may end up being modified by the * option/argument sorting. * @param paOptions Array of RTGETOPTDEF structures, which must specify what * options are understood by the program. * @param cOptions Number of array items passed in with paOptions. * @param iFirst The argument to start with (in argv). * @param fFlags The flags, see RTGETOPTINIT_FLAGS_XXX. */ RTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv, PCRTGETOPTDEF paOptions, size_t cOptions, int iFirst, uint32_t fFlags); /** @name RTGetOptInit flags. * @{ */ /** Sort the arguments so that options comes first, then non-options. */ #define RTGETOPTINIT_FLAGS_OPTS_FIRST RT_BIT_32(0) /** Prevent add the standard version and help options: * — «—help», «-h» and «-?» returns ‘h’. * — «—version» and «-V» return ‘V’. */ #define RTGETOPTINIT_FLAGS_NO_STD_OPTS RT_BIT_32(1) /** @} */ /** * Command line argument parser, handling both long and short options and checking * argument formats, if desired. * * This is to be called in a loop until it returns 0 (meaning that all options * were parsed) or a negative value (meaning that an error occurred). How non-option * arguments are dealt with depends on the flags passed to RTGetOptInit. The default * (fFlags = 0) is to return VINF_GETOPT_NOT_OPTION with pValueUnion->psz pointing to * the argument string. * * For example, for a program which takes the following options: * * —optwithstring (or -s) and a string argument; * —optwithint (or -i) and a 32-bit signed integer argument; * —verbose (or -v) with no arguments, * * code would look something like this: * * @code int main(int argc, char **argv) { int rc = RTR3Init(); if (RT_FAILURE(rc)) return RTMsgInitFailure(rc); static const RTGETOPTDEF s_aOptions[] = { { «—optwithstring», ‘s’, RTGETOPT_REQ_STRING }, { «—optwithint», ‘i’, RTGETOPT_REQ_INT32 }, { «—verbose», ‘v’, 0 }, }; int ch; RTGETOPTUNION ValueUnion; RTGETOPTSTATE GetState; RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0); while ((ch = RTGetOpt(&GetState, &ValueUnion))) { // for options that require an argument, ValueUnion has received the value switch (ch) { case ‘s’: // —optwithstring or -s // string argument, copy ValueUnion.psz break; case ‘i’: // —optwithint or -i // integer argument, copy ValueUnion.i32 break; case ‘v’: // —verbose or -v g_fOptVerbose = true; break; case VINF_GETOPT_NOT_OPTION: // handle non-option argument in ValueUnion.psz. break; default: return RTGetOptPrintError(ch, &ValueUnion); } } return RTEXITCODE_SUCCESS; } @endcode * * @returns 0 when done parsing. * @returns the iShort value of the option. pState->pDef points to the option * definition which matched. * @returns IPRT error status on parse error. * @returns VINF_GETOPT_NOT_OPTION when encountering a non-option argument and * RTGETOPT_FLAG_SORT was not specified. pValueUnion->psz points to the * argument string. * @returns VERR_GETOPT_UNKNOWN_OPTION when encountering an unknown option. * pValueUnion->psz points to the option string. * @returns VERR_GETOPT_REQUIRED_ARGUMENT_MISSING and pValueUnion->pDef if * a required argument (aka value) was missing for an option. * @returns VERR_GETOPT_INVALID_ARGUMENT_FORMAT and pValueUnion->pDef if * argument (aka value) conversion failed. * * @param pState The state previously initialized with RTGetOptInit. * @param pValueUnion Union with value; in the event of an error, psz member * points to erroneous parameter; otherwise, for options * that require an argument, this contains the value of * that argument, depending on the type that is required. */ RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion); /** * Fetch a value. * * Used to retrive a value argument in a manner similar to what RTGetOpt does * (@a fFlags -> @a pValueUnion). This can be used when handling * VINF_GETOPT_NOT_OPTION, but is equally useful for decoding options that * takes more than one value. * * @returns VINF_SUCCESS on success. * @returns IPRT error status on parse error. * @returns VERR_INVALID_PARAMETER if the flags are wrong. * @returns VERR_GETOPT_UNKNOWN_OPTION when pState->pDef is null. * @returns VERR_GETOPT_REQUIRED_ARGUMENT_MISSING if there are no more * available arguments. pValueUnion->pDef is NULL. * @returns VERR_GETOPT_INVALID_ARGUMENT_FORMAT and pValueUnion->pDef is * unchanged if value conversion failed. * * @param pState The state previously initialized with RTGetOptInit. * @param pValueUnion Union with value; in the event of an error, psz member * points to erroneous parameter; otherwise, for options * that require an argument, this contains the value of * that argument, depending on the type that is required. * @param fFlags What to get, that is RTGETOPT_REQ_XXX. */ RTDECL(int) RTGetOptFetchValue(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion, uint32_t fFlags); /** * Print error messages for a RTGetOpt default case. * * Uses RTMsgError. * * @returns Suitable exit code. * * @param ch The RTGetOpt return value. * @param pValueUnion The value union returned by RTGetOpt. */ RTDECL(RTEXITCODE) RTGetOptPrintError(int ch, PCRTGETOPTUNION pValueUnion); /** * Parses the @a pszCmdLine string into an argv array. * * This is useful for converting a response file or similar to an argument * vector that can be used with RTGetOptInit(). * * This function aims at following the bourne shell string quoting rules. * * @returns IPRT status code. * * @param ppapszArgv Where to return the argument vector. This must be * freed by calling RTGetOptArgvFree. * @param pcArgs Where to return the argument count. * @param pszCmdLine The string to parse. * @param pszSeparators String containing the argument separators. If NULL, * then space, tab, line feed (\n) and return (\r) * are used. */ RTDECL(int) RTGetOptArgvFromString(char ***ppapszArgv, int *pcArgs, const char *pszCmdLine, const char *pszSeparators); /** * Frees and argument vector returned by RTGetOptStringToArgv. * * @param papszArgv Argument vector. NULL is fine. */ RTDECL(void) RTGetOptArgvFree(char **paArgv); /** * Turns an argv array into a command line string. * * This is useful for calling CreateProcess on Windows, but can also be used for * displaying an argv array. * * This function aims at following the bourn shell string quoting rules. * * @returns IPRT status code. * * @param ppszCmdLine Where to return the command line string. This must * be freed by calling RTStrFree. * @param papszArgs The argument vector to convert. * @param fFlags A combination of the RTGETOPTARGV_CNV_XXX flags. */ RTDECL(int) RTGetOptArgvToString(char **ppszCmdLine, const char * const *papszArgv, uint32_t fFlags); /** @name RTGetOptArgvToString and RTGetOptArgvToUtf16String flags * @{ */ /** Quote strings according to the Microsoft CRT rules. */ #define RTGETOPTARGV_CNV_QUOTE_MS_CRT UINT32_C(0) /** Quote strings according to the Unix Bourne Shell. */ #define RTGETOPTARGV_CNV_QUOTE_BOURNE_SH UINT32_C(1) /** Mask for the quoting style. */ #define RTGETOPTARGV_CNV_QUOTE_MASK UINT32_C(1) /** @} */ /** * Convenience wrapper around RTGetOpArgvToString and RTStrToUtf16. * * @returns IPRT status code. * * @param ppwszCmdLine Where to return the command line string. This must * be freed by calling RTUtf16Free. * @param papszArgs The argument vector to convert. * @param fFlags A combination of the RTGETOPTARGV_CNV_XXX flags. */ RTDECL(int) RTGetOptArgvToUtf16String(PRTUTF16 *ppwszCmdLine, const char * const *papszArgv, uint32_t fFlags); /** @} */ RT_C_DECLS_END #endif

The man page certainly doesn’t document it very well, but the source code helps a little.

Briefly: you’re supposed to do something like the following (though this may be a little over-pedantic):

if(   !optarg
   && optind < argc // make sure optind is valid
   && NULL != argv[optind] // make sure it's not a null string
   && '' != argv[optind][0] // ... or an empty string
   && '-' != argv[optind][0] // ... or another option
  ) {
  // update optind so the next getopt_long invocation skips argv[optind]
  my_optarg = argv[optind++];
}
/* ... */

From among the comments preceding _getopt_internal:

If getopt finds another option character, it returns that character,
updating optind and nextchar so that the next call to getopt can
resume the scan with the following option character or ARGV-element.

If there are no more option characters, getopt returns -1.
Then optind is the index in ARGV of the first ARGV-element
that is not an option. (The ARGV-elements have been permuted
so that those that are not options now come last.) <-- a note from me:
if the 3rd argument to getopt_long starts with a dash, argv will not
be permuted

If a char in OPTSTRING is followed by a colon, that means it wants an arg,
so the following text in the same ARGV-element, or the text of the following
ARGV-element, is returned in optarg. Two colons mean an option that
wants an optional arg; if there is text in the current ARGV-element,
it is returned in optarg, otherwise optarg is set to zero.

… though you have to do some reading between the lines. The following does what you want:

#include <stdio.h>
#include <getopt.h>

int main(int argc, char* argv[] ) {
  int getopt_ret;
  int option_index;
  static struct option long_options[] = {
      {"praise",  required_argument, 0, 'p'}
    , {"blame",  optional_argument, 0, 'b'}
    , {0, 0, 0, 0}
  };

  while( -1 != ( getopt_ret = getopt_long(  argc
                                          , argv
                                          , "p:b::"
                                          , long_options
                                          , &option_index) ) ) {
    const char *tmp_optarg = optarg;
    switch( getopt_ret ) {
      case 0: break;
      case 1:
        // handle non-option arguments here if you put a `-`
        // at the beginning of getopt_long's 3rd argument
        break;
      case 'p':
        printf("Kudos to %sn", optarg); break;
      case 'b':
        if(   !optarg
           && NULL != argv[optind]
           && '-' != argv[optind][0] ) {
          // This is what makes it work; if `optarg` isn't set
          // and argv[optind] doesn't look like another option,
          // then assume it's our parameter and overtly modify optind
          // to compensate.
          //
          // I'm not terribly fond of how this is done in the getopt
          // API, but if you look at the man page it documents the
          // existence of `optarg`, `optind`, etc, and they're
          // not marked const -- implying they expect and intend you
          // to modify them if needed.
          tmp_optarg = argv[optind++];
        }
        printf( "You suck" );
        if (tmp_optarg) {
          printf (", %s!n", tmp_optarg);
        } else {
          printf ("!n");
        }
        break;
      case '?':
        printf("Unknown optionn");
        break;
      default:
        printf( "Unknown: getopt_ret == %dn", getopt_ret );
        break;
    }
  }
  return 0;
}

у меня есть программа, которая принимает различные аргументы командной строки. Для упрощения мы скажем, что требуется 3 флага,-a, -b и -c, и используйте следующий код для разбора моих аргументов:

    int c;
    while((c =  getopt(argc, argv, ":a:b:c")) != EOF)
    {
        switch (c)
        {
             case 'a':
                 cout << optarg << endl;
                 break;
             case 'b':
                 cout << optarg << endl;
                 break;
             case ':':
                 cerr << "Missing option." << endl;
                 exit(1);
                 break;
        }
    }

Примечание: a и b принимают параметры после флага.

но я сталкиваюсь с проблемой, если я вызываю свою программу, скажем, с

./myprog -a -b parameterForB

где я забыл parameterForA, parameterForA (представленный optarg) возвращается как -b и parameterForB считается опцией без параметра, а optind устанавливается в индекс parameterForB в argv.

желаемым поведением в этой ситуации было бы то, что ':' возвращается после того, как аргумент не найден для -a и Missing option. выводится в стандартный поток ошибок. Однако это происходит только в том случае,-a — последний параметр, переданный в программу.

я думаю, вопрос в том, есть ли способ сделать getopt() предположим, что никакие параметры не будут начинаются с -?

5 ответов


посмотреть стандартное определение POSIX на getopt. Он говорит, что

если он [getopt] обнаруживает отсутствие
option-аргумент, он должен вернуть
символ двоеточия ( ‘:’ ) если первый
символ optstring был двоеточием, или
знак вопроса (‘?’ )
иначе.

Что касается этого обнаружения,

  1. если опция была последним символом в строке, на которую указывает
    элемент argv, тогда optarg должны
    содержать следующий элемент argv и
    optind увеличивается на 2. Если
    результирующее значение optind
    больше, чем argc, это указывает на
    отсутствует параметр-аргумент и getopt()
    возвращает указание на ошибку.
  2. в противном случае optarg должен указывать на строку, следующую за опцией
    характер в этом элементе argv, и
    optind увеличивается на 1.

похоже getopt определен не делать то, что вы хотите, чтобы вы сами осуществили проверку. К счастью, вы можете сделать это, проверив *optarg и меняется optind себя.

int c, prev_ind;
while(prev_ind = optind, (c =  getopt(argc, argv, ":a:b:c")) != EOF)
{
    if ( optind == prev_ind + 2 && *optarg == '-' ) {
        c = ':';
        -- optind;
    }
    switch ( …


полное раскрытие: я не эксперт по этому вопросу.

б от gnu.org пригодится? Кажется, он справляется с ‘? характера в случаях, когда ожидаемый аргумент не был указан:

while ((c = getopt (argc, argv, "abc:")) != -1)
    switch (c)
    {
       case 'a':
         aflag = 1;
         break;
       case 'b':
         bflag = 1;
         break;
       case 'c':
         cvalue = optarg;
         break;
       case '?':
         if (optopt == 'c')
           fprintf (stderr, "Option -%c requires an argument.n", optopt);
         else if (isprint (optopt))
           fprintf (stderr, "Unknown option `-%c'.n", optopt);
         else
           fprintf (stderr,
                    "Unknown option character `x%x'.n",
                    optopt);
         return 1;
       default:
         abort ();
    }

обновление: возможно, следующее будет работать как исправить?

while((c =  getopt(argc, argv, ":a:b:c")) != EOF)
{
    if (optarg[0] == '-')
    {
        c = ':';
    }
    switch (c)
    {
        ...
    }
}

в качестве альтернативы для проектов без наддува у меня есть простая оболочка только для заголовка C++ для getopt (под лицензией BSD 3-Clause):https://github.com/songgao/flags.hh

принято от example.cc в репо:

#include "Flags.hh"

#include <cstdint>
#include <iostream>

int main(int argc, char ** argv) {
  uint64_t var1;
  uint32_t var2;
  int32_t var3;
  std::string str;
  bool b, help;

  Flags flags;

  flags.Var(var1, 'a', "var1", uint64_t(64), "This is var1!");
  flags.Var(var2, 'b', "var2", uint32_t(32), "var2 haahahahaha...");
  flags.Var(var3, 'c', "var3", int32_t(42), "var3 is signed!", "Group 1");
  flags.Var(str, 's', "str", std::string("Hello!"), "This is a string, and the description is too long to fit in one line and has to be wrapped blah blah blah blah...", "Group 1");
  flags.Bool(b, 'd', "bool", "this is a bool variable", "Group 2");

  flags.Bool(help, 'h', "help", "show this help and exit", "Group 3");

  if (!flags.Parse(argc, argv)) {
    flags.PrintHelp(argv[0]);
    return 1;
  } else if (help) {
    flags.PrintHelp(argv[0]);
    return 0;
  }

  std::cout << "var1: " << var1 << std::endl;
  std::cout << "var2: " << var2 << std::endl;
  std::cout << "var3: " << var3 << std::endl;
  std::cout << "str:  " << str << std::endl;
  std::cout << "b:    " << (b ? "set" : "unset") << std::endl;

  return 0;
}

существует довольно много различных версий getopt вокруг, поэтому, даже если вы можете заставить его работать для одной версии, вероятно, будет по крайней мере пять других, для которых ваш обходной путь сломается. Если у вас нет подавляющей причины использовать getopt, я бы рассмотрел что-то еще, например импульс.Program_options.


Понравилась статья? Поделить с друзьями:
  • Pantum ошибка 002
  • Pantum m7300fdn ошибка картриджа 2004
  • Pantum m7100dn ошибка 1001
  • Pantum m6500 error 01
  • Pandora номер телефона системы как изменить