依星源码资源网,依星资源网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 327|回复: 0

windows 实现 getopt 调用(getopt.h和getopt.c)

[复制链接] 主动推送

5449

主题

5496

帖子

6837

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6837
发表于 2023-8-16 14:30:13 | 显示全部楼层 |阅读模式
windows 实现 getopt 调用(getopt.h和getopt.c)
1、linux系统环境下可以调用getopt
2、windwos系统环境下无法调用getopt
3、windows系统环境下模拟实现getopt

函数说明 getopt() 可以用来分析命令行参数。
  1. int getopt(int argc,char * const argv[ ],const char * optstring);
复制代码
参数argc和argv分别代表参数个数和内容,跟main()函数的命令行参数是一样的。参数 optstring为选项字符串, 告知 getopt()可以处理哪个选项以及哪个选项需要参数,如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。

这么好用的函数只能在 Linux 下使用,如果你在 Windows 下进行开发,也想实现命令行可以传入多个自定义参数的功能,这篇文章可以解决你的需求。

将下面两个源文件和头文件加入到你的项目里即可。


getopt.h
  1. # ifndef __GETOPT_H_
  2. # define __GETOPT_H_

  3. # ifdef _GETOPT_API
  4. #     undef _GETOPT_API
  5. # endif
  6. //------------------------------------------------------------------------------
  7. # if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT)
  8. #     error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT \
  9. can only be used individually"
  10. # elif defined(STATIC_GETOPT)
  11. #     pragma message("Warning static builds of getopt violate the Lesser GNU \
  12. Public License")
  13. #     define _GETOPT_API
  14. # elif defined(EXPORTS_GETOPT)
  15. #     pragma message("Exporting getopt library")
  16. #     define _GETOPT_API __declspec(dllexport)
  17. # else
  18. #     pragma message("Importing getopt library")
  19. #     define _GETOPT_API __declspec(dllimport)
  20. # endif

  21. # include <tchar.h>
  22. // Standard GNU options
  23. # define null_argument           0 /*Argument Null*/
  24. # define no_argument             0 /*Argument Switch Only*/
  25. # define required_argument       1 /*Argument Required*/
  26. # define optional_argument       2 /*Argument Optional*/
  27. // Shorter Versions of options
  28. # define ARG_NULL 0 /*Argument Null*/
  29. # define ARG_NONE 0 /*Argument Switch Only*/
  30. # define ARG_REQ  1 /*Argument Required*/
  31. # define ARG_OPT  2 /*Argument Optional*/
  32. // Change behavior for C\C++
  33. # ifdef __cplusplus
  34. #     define _BEGIN_EXTERN_C extern "C" {
  35. #     define _END_EXTERN_C }
  36. #     define _GETOPT_THROW throw()
  37. # else
  38. #     define _BEGIN_EXTERN_C
  39. #     define _END_EXTERN_C
  40. #     define _GETOPT_THROW
  41. # endif
  42. _BEGIN_EXTERN_C
  43. extern _GETOPT_API TCHAR *optarg;
  44. extern _GETOPT_API int    optind;
  45. extern _GETOPT_API int    opterr;
  46. extern _GETOPT_API int    optopt;
  47. struct option
  48. {
  49. /* The predefined macro variable __STDC__ is defined for C++, and it has the in-
  50.    teger value 0 when it is used in an #if statement, indicating that the C++ l-
  51.    anguage is not a proper superset of C, and that the compiler does not confor-
  52.    m to C. In C, __STDC__ has the integer value 1. */
  53. # if defined (__STDC__) && __STDC__
  54.     const TCHAR* name;
  55. # else
  56.     TCHAR* name;
  57. # endif
  58.     int has_arg;
  59.     int *flag;
  60.     TCHAR val;
  61. };
  62. extern _GETOPT_API int getopt( int argc, TCHAR *const *argv
  63.                              , const TCHAR *optstring ) _GETOPT_THROW;
  64. extern _GETOPT_API int getopt_long
  65.                              ( int ___argc, TCHAR *const *___argv
  66.                              , const TCHAR *__shortopts
  67.                              , const struct option *__longopts
  68.                              , int *__longind ) _GETOPT_THROW;
  69. extern _GETOPT_API int getopt_long_only
  70.                              ( int ___argc, TCHAR *const *___argv
  71.                              , const TCHAR *__shortopts
  72.                              , const struct option *__longopts
  73.                              , int *__longind ) _GETOPT_THROW;
  74. // harly.he add for reentrant 12.09/2013
  75. extern _GETOPT_API void getopt_reset() _GETOPT_THROW;
  76. _END_EXTERN_C
  77. // Undefine so the macros are not included
  78. # undef _BEGIN_EXTERN_C
  79. # undef _END_EXTERN_C
  80. # undef _GETOPT_THROW
  81. # undef _GETOPT_API
  82. # endif  // __GETOPT_H_
复制代码
getopt.c
  1. # ifndef _CRT_SECURE_NO_WARNINGS
  2. #     define _CRT_SECURE_NO_WARNINGS
  3. # endif

  4. # include <stdlib.h>
  5. # include <stdio.h>
  6. # include <tchar.h>
  7. # include "getopt.h"

  8. # ifdef __cplusplus
  9. #     define _GETOPT_THROW throw()
  10. # else
  11. #     define _GETOPT_THROW
  12. # endif

  13. enum ENUM_ORDERING
  14. {
  15.     REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
  16. };

  17. struct _getopt_data
  18. {
  19.     int     optind;
  20.     int     opterr;
  21.     int     optopt;
  22.     TCHAR*  optarg;
  23.     int     __initialized;
  24.     TCHAR*  __nextchar;
  25.     int     __ordering;
  26.     int     __posixly_correct;
  27.     int     __first_nonopt;
  28.     int     __last_nonopt;
  29. };
  30. static struct _getopt_data  getopt_data = { 0, 0, 0, NULL, 0, NULL, 0, 0, 0, 0 };

  31. TCHAR*  optarg  = NULL;
  32. int     optind  = 1;
  33. int     opterr  = 1;
  34. int     optopt  = _T( '?' );

  35. static void exchange( TCHAR** argv, struct _getopt_data* d )
  36. {
  37.     int     bottom  = d->__first_nonopt;
  38.     int     middle  = d->__last_nonopt;
  39.     int     top     = d->optind;
  40.     TCHAR*  tem;
  41.     while ( top > middle && middle > bottom )
  42.     {
  43.         if ( top - middle > middle - bottom )
  44.         {
  45.             int len = middle - bottom;
  46.             register int i;
  47.             for ( i = 0; i < len; i++ )
  48.             {
  49.                 tem = argv[bottom + i];
  50.                 argv[bottom + i] = argv[top - ( middle - bottom ) + i];
  51.                 argv[top - ( middle - bottom ) + i] = tem;
  52.             }
  53.             top -= len;
  54.         }
  55.         else
  56.         {
  57.             int len = top - middle;
  58.             register int i;
  59.             for ( i = 0; i < len; i++ )
  60.             {
  61.                 tem = argv[bottom + i];
  62.                 argv[bottom + i] = argv[middle + i];
  63.                 argv[middle + i] = tem;
  64.             }
  65.             bottom += len;
  66.         }
  67.     }
  68.     d->__first_nonopt += ( d->optind - d->__last_nonopt );
  69.     d->__last_nonopt = d->optind;
  70. }

  71. static const TCHAR* _getopt_initialize( const TCHAR* optstring
  72.                                       , struct _getopt_data* d
  73.                                       , int posixly_correct )
  74. {
  75.     d->__first_nonopt = d->__last_nonopt = d->optind;
  76.     d->__nextchar = NULL;
  77.     d->__posixly_correct = posixly_correct
  78.                          | !!_tgetenv( _T( "POSIXLY_CORRECT" ) );
  79.     if ( optstring[0] == _T( '-' ) )
  80.     {
  81.         d->__ordering = RETURN_IN_ORDER;
  82.         ++optstring;
  83.     }
  84.     else if ( optstring[0] == _T( '+' ) )
  85.     {
  86.         d->__ordering = REQUIRE_ORDER;
  87.         ++optstring;
  88.     }
  89.     else if ( d->__posixly_correct )
  90.     {
  91.         d->__ordering = REQUIRE_ORDER;
  92.     }
  93.     else
  94.     {
  95.         d->__ordering = PERMUTE;
  96.     }
  97.     return optstring;
  98. }

  99. int _getopt_internal_r( int argc
  100.                       , TCHAR *const * argv
  101.                       , const TCHAR* optstring
  102.                       , const struct option* longopts
  103.                       , int* longind
  104.                       , int long_only
  105.                       , struct _getopt_data* d
  106.                       , int posixly_correct )
  107. {
  108.     int print_errors    = d->opterr;
  109.     if ( argc < 1 )
  110.     {
  111.         return -1;
  112.     }
  113.     d->optarg = NULL;
  114.     if ( d->optind == 0 || !d->__initialized )
  115.     {
  116.         if ( d->optind == 0 )
  117.         {
  118.             d->optind = 1;
  119.         }
  120.         optstring = _getopt_initialize( optstring, d, posixly_correct );
  121.         d->__initialized = 1;
  122.     }
  123.     else if ( optstring[0] == _T( '-' ) || optstring[0] == _T( '+' ) )
  124.     {
  125.         optstring++;
  126.     }
  127.     if ( optstring[0] == _T( ':' ) )
  128.     {
  129.         print_errors = 0;
  130.     }
  131.     if ( d->__nextchar == NULL || *d->__nextchar == _T( '\0' ) )
  132.     {
  133.         if ( d->__last_nonopt > d->optind )
  134.         {
  135.             d->__last_nonopt = d->optind;
  136.         }
  137.         if ( d->__first_nonopt > d->optind )
  138.         {
  139.             d->__first_nonopt = d->optind;
  140.         }
  141.         if ( d->__ordering == PERMUTE )
  142.         {
  143.             if ( d->__first_nonopt != d->__last_nonopt
  144.               && d->__last_nonopt != d->optind )
  145.             {
  146.                 exchange( ( TCHAR * * ) argv, d );
  147.             }
  148.             else if ( d->__last_nonopt != d->optind )
  149.             {
  150.                 d->__first_nonopt = d->optind;
  151.             }
  152.             while ( d->optind
  153.                   < argc
  154.                  && ( argv[d->optind][0] != _T( '-' )
  155.                    || argv[d->optind][1] == _T( '\0' ) ) )
  156.             {
  157.                 d->optind++;
  158.             }
  159.             d->__last_nonopt = d->optind;
  160.         }
  161.         if ( d->optind != argc && !_tcscmp( argv[d->optind], _T( "--" ) ) )
  162.         {
  163.             d->optind++;
  164.             if ( d->__first_nonopt != d->__last_nonopt
  165.               && d->__last_nonopt != d->optind )
  166.             {
  167.                 exchange( ( TCHAR * * ) argv, d );
  168.             }
  169.             else if ( d->__first_nonopt == d->__last_nonopt )
  170.             {
  171.                 d->__first_nonopt = d->optind;
  172.             }
  173.             d->__last_nonopt = argc;
  174.             d->optind = argc;
  175.         }
  176.         if ( d->optind == argc )
  177.         {
  178.             if ( d->__first_nonopt != d->__last_nonopt )
  179.             {
  180.                 d->optind = d->__first_nonopt;
  181.             }
  182.             return -1;
  183.         }
  184.         if ( ( argv[d->optind][0] != _T( '-' )
  185.             || argv[d->optind][1] == _T( '\0' ) ) )
  186.         {
  187.             if ( d->__ordering == REQUIRE_ORDER )
  188.             {
  189.                 return -1;
  190.             }
  191.             d->optarg = argv[d->optind++];
  192.             return 1;
  193.         }
  194.         d->__nextchar = ( argv[d->optind]
  195.                         + 1 + ( longopts != NULL
  196.                              && argv[d->optind][1] == _T( '-' ) ) );
  197.     }
  198.     if ( longopts != NULL
  199.         && ( argv[d->optind][1] == _T( '-' )
  200.             || ( long_only && ( argv[d->optind][2]
  201.                                 || !_tcschr( optstring, argv[d->optind][1] ) )
  202.                )
  203.            )
  204.         )
  205.     {
  206.         TCHAR*                  nameend;
  207.         const struct option*    p;
  208.         const struct option*    pfound      = NULL;
  209.         int                     exact       = 0;
  210.         int                     ambig       = 0;
  211.         int                     indfound    = -1;
  212.         int                     option_index;
  213.         for ( nameend = d->__nextchar;
  214.               *nameend && *nameend != _T( '=' );
  215.               nameend++ )
  216.             ;
  217.         for ( p = longopts, option_index = 0; p->name; p++, option_index++ )
  218.         {
  219.             if ( !_tcsncmp( p->name, d->__nextchar, nameend - d->__nextchar ) )
  220.             {
  221.                 if ( ( unsigned int ) ( nameend - d->__nextchar )
  222.                   == ( unsigned int ) _tcslen( p->name ) )
  223.                 {
  224.                     pfound = p;
  225.                     indfound = option_index;
  226.                     exact = 1;
  227.                     break;
  228.                 }
  229.                 else if ( pfound == NULL )
  230.                 {
  231.                     pfound = p;
  232.                     indfound = option_index;
  233.                 }
  234.                 else if ( long_only
  235.                        || pfound->has_arg != p->has_arg
  236.                        || pfound->flag != p->flag
  237.                        || pfound->val != p->val )
  238.                 {
  239.                          ambig = 1;
  240.                 }
  241.             }
  242.         }
  243.         if ( ambig && !exact )
  244.         {
  245.             if ( print_errors )
  246.             {
  247.                 _ftprintf( stderr
  248.                          , _T( "%s: option '%s' is ambiguous\n" )
  249.                          , argv[0]
  250.                          , argv[d->optind] );
  251.             }
  252.             d->__nextchar += _tcslen( d->__nextchar );
  253.             d->optind++;
  254.             d->optopt = 0;
  255.             return _T( '?' );
  256.         }
  257.         if ( pfound != NULL )
  258.         {
  259.             option_index = indfound;
  260.             d->optind++;
  261.             if ( *nameend )
  262.             {
  263.                 if ( pfound->has_arg )
  264.                 {
  265.                     d->optarg = nameend + 1;
  266.                 }
  267.                 else
  268.                 {
  269.                     if ( print_errors )
  270.                     {
  271.                         if ( argv[d->optind - 1][1] == _T( '-' ) )
  272.                         {
  273.                             _ftprintf( stderr
  274.                                      , _T( "%s: option '--%s' doesn't allow " )
  275.                                        _T( "an argument\n" )
  276.                                      , argv[0]
  277.                                      , pfound->name );
  278.                         }
  279.                         else
  280.                         {
  281.                             _ftprintf( stderr
  282.                                      , _T( "%s: option '%c%s' doesn't allow " )
  283.                                        _T( "an argument\n" )
  284.                                      , argv[0]
  285.                                      , argv[d->optind - 1][0]
  286.                                      , pfound->name );
  287.                         }
  288.                     }
  289.                     d->__nextchar += _tcslen( d->__nextchar );
  290.                     d->optopt = pfound->val;
  291.                     return _T( '?' );
  292.                 }
  293.             }
  294.             else if ( pfound->has_arg == 1 )
  295.             {
  296.                 if ( d->optind < argc )
  297.                 {
  298.                     d->optarg = argv[d->optind++];
  299.                 }
  300.                 else
  301.                 {
  302.                     if ( print_errors )
  303.                     {
  304.                         _ftprintf( stderr
  305.                                  , _T( "%s: option '--%s' requires an " )
  306.                                    _T( "argument\n" )
  307.                                  , argv[0]
  308.                                  , pfound->name );
  309.                     }
  310.                     d->__nextchar += _tcslen( d->__nextchar );
  311.                     d->optopt = pfound->val;
  312.                     return optstring[0] == _T( ':' ) ? _T( ':' ) : _T( '?' );
  313.                 }
  314.             }
  315.             d->__nextchar += _tcslen( d->__nextchar );
  316.             if ( longind != NULL )
  317.             {
  318.                 *longind = option_index;
  319.             }
  320.             if ( pfound->flag )
  321.             {
  322.                 *( pfound->flag ) = pfound->val;
  323.                 return 0;
  324.             }
  325.             return pfound->val;
  326.         }
  327.         if ( !long_only
  328.           || argv[d->optind][1]
  329.           == _T( '-' )
  330.           || _tcschr( optstring
  331.                     , *d->__nextchar )
  332.           == NULL )
  333.         {
  334.             if ( print_errors )
  335.             {
  336.                 if ( argv[d->optind][1] == _T( '-' ) )
  337.                 {
  338.                     /* --option */
  339.                     _ftprintf( stderr
  340.                              , _T( "%s: unrecognized option '--%s'\n" )
  341.                              , argv[0]
  342.                              , d->__nextchar );
  343.                 }
  344.                 else
  345.                 {
  346.                     /* +option or -option */
  347.                     _ftprintf( stderr
  348.                              , _T( "%s: unrecognized option '%c%s'\n" )
  349.                              , argv[0]
  350.                              , argv[d->optind][0]
  351.                              , d->__nextchar );
  352.                 }
  353.             }
  354.             d->__nextchar = ( TCHAR * ) _T( "" );
  355.             d->optind++;
  356.             d->optopt = 0;
  357.             return _T( '?' );
  358.         }
  359.     }
  360.     {
  361.         TCHAR   c       = *d->__nextchar++;
  362.         TCHAR*  temp    = ( TCHAR* ) _tcschr( optstring, c );
  363.         if ( *d->__nextchar == _T( '\0' ) )
  364.         {
  365.             ++d->optind;
  366.         }
  367.         if ( temp == NULL || c == _T( ':' ) || c == _T( ';' ) )
  368.         {
  369.             if ( print_errors )
  370.             {
  371.                 _ftprintf( stderr
  372.                          , _T( "%s: invalid option -- '%c'\n" )
  373.                          , argv[0]
  374.                          , c );
  375.             }
  376.             d->optopt = c;
  377.             return _T( '?' );
  378.         }
  379.         if ( temp[0] == _T( 'W' ) && temp[1] == _T( ';' ) )
  380.         {
  381.             TCHAR*                  nameend;
  382.             const struct option*    p;
  383.             const struct option*    pfound      = NULL;
  384.             int                     exact       = 0;
  385.             int                     ambig       = 0;
  386.             int                     indfound    = 0;
  387.             int                     option_index;
  388.             if ( *d->__nextchar != _T( '\0' ) )
  389.             {
  390.                 d->optarg = d->__nextchar;
  391.                 d->optind++;
  392.             }
  393.             else if ( d->optind == argc )
  394.             {
  395.                 if ( print_errors )
  396.                 {
  397.                     _ftprintf( stderr
  398.                              , _T( "%s: option requires an argument -- '%c'\n" )
  399.                              , argv[0]
  400.                              , c );
  401.                 }
  402.                 d->optopt = c;
  403.                 if ( optstring[0] == _T( ':' ) )
  404.                 {
  405.                     c = _T( ':' );
  406.                 }
  407.                 else
  408.                 {
  409.                     c = _T( '?' );
  410.                 }
  411.                 return c;
  412.             }
  413.             else
  414.             {
  415.                 d->optarg = argv[d->optind++];
  416.             }
  417.             for ( d->__nextchar = nameend = d->optarg;
  418.                   *nameend && *nameend != _T( '=' );
  419.                   nameend++ )
  420.                 ;
  421.             for ( p = longopts, option_index = 0;
  422.                   p->name;
  423.                   p++, option_index++ )
  424.             {
  425.                 if ( !_tcsncmp( p->name
  426.                               , d->__nextchar
  427.                               , nameend - d->__nextchar ) )
  428.                 {
  429.                     if ( ( unsigned int ) ( nameend - d->__nextchar )
  430.                       == _tcslen( p->name ) )
  431.                     {
  432.                         pfound = p;
  433.                         indfound = option_index;
  434.                         exact = 1;
  435.                         break;
  436.                     }
  437.                     else if ( pfound == NULL )
  438.                     {
  439.                         pfound = p;
  440.                         indfound = option_index;
  441.                     }
  442.                     else if ( long_only
  443.                            || pfound->has_arg != p->has_arg
  444.                            || pfound->flag != p->flag
  445.                            || pfound->val != p->val )
  446.                     {
  447.                              ambig = 1;
  448.                     }
  449.                 }
  450.             }
  451.             if ( ambig && !exact )
  452.             {
  453.                 if ( print_errors )
  454.                 {
  455.                     _ftprintf( stderr
  456.                              , _T( "%s: option '-W %s' is ambiguous\n" )
  457.                              , argv[0]
  458.                              , d->optarg );
  459.                 }
  460.                 d->__nextchar += _tcslen( d->__nextchar );
  461.                 d->optind++;
  462.                 return _T( '?' );
  463.             }
  464.             if ( pfound != NULL )
  465.             {
  466.                 option_index = indfound;
  467.                 if ( *nameend )
  468.                 {
  469.                     if ( pfound->has_arg )
  470.                     {
  471.                         d->optarg = nameend + 1;
  472.                     }
  473.                     else
  474.                     {
  475.                         if ( print_errors )
  476.                         {
  477.                             _ftprintf( stderr
  478.                                      , _T( "%s: option '-W %s' doesn't allow " )
  479.                                        _T( "an argument\n" )
  480.                                      , argv[0]
  481.                                      , pfound->name );
  482.                         }
  483.                         d->__nextchar += _tcslen( d->__nextchar );
  484.                         return _T( '?' );
  485.                     }
  486.                 }
  487.                 else if ( pfound->has_arg == 1 )
  488.                 {
  489.                     if ( d->optind < argc )
  490.                     {
  491.                         d->optarg = argv[d->optind++];
  492.                     }
  493.                     else
  494.                     {
  495.                         if ( print_errors )
  496.                         {
  497.                             _ftprintf( stderr
  498.                                      , _T( "%s: option '-W %s' requires an " )
  499.                                        _T( "argument\n" )
  500.                                      , argv[0]
  501.                                      , pfound->name );
  502.                         }
  503.                         d->__nextchar += _tcslen( d->__nextchar );
  504.                         return optstring[0] == _T(':') ? _T(':') : _T('?');
  505.                     }
  506.                 }
  507.                 else
  508.                 {
  509.                     d->optarg = NULL;
  510.                 }
  511.                 d->__nextchar += _tcslen( d->__nextchar );
  512.                 if ( longind != NULL )
  513.                 {
  514.                     *longind = option_index;
  515.                 }
  516.                 if ( pfound->flag )
  517.                 {
  518.                     *( pfound->flag ) = pfound->val;
  519.                     return 0;
  520.                 }
  521.                 return pfound->val;
  522.             }
  523.             d->__nextchar = NULL;
  524.             return _T( 'W' );
  525.         }
  526.         if ( temp[1] == _T( ':' ) )
  527.         {
  528.             if ( temp[2] == _T( ':' ) )
  529.             {
  530.                 if ( *d->__nextchar != _T( '\0' ) )
  531.                 {
  532.                     d->optarg = d->__nextchar;
  533.                     d->optind++;
  534.                 }
  535.                 else
  536.                 {
  537.                     d->optarg = NULL;
  538.                 }
  539.                 d->__nextchar = NULL;
  540.             }
  541.             else
  542.             {
  543.                 if ( *d->__nextchar != _T( '\0' ) )
  544.                 {
  545.                     d->optarg = d->__nextchar;
  546.                     d->optind++;
  547.                 }
  548.                 else if ( d->optind == argc )
  549.                 {
  550.                     if ( print_errors )
  551.                     {
  552.                         _ftprintf( stderr
  553.                                  , _T( "%s: option requires an " )
  554.                                    _T( "argument -- '%c'\n" )
  555.                                  , argv[0]
  556.                                  , c );
  557.                     }
  558.                     d->optopt = c;
  559.                     if ( optstring[0] == _T( ':' ) )
  560.                     {
  561.                         c = _T( ':' );
  562.                     }
  563.                     else
  564.                     {
  565.                         c = _T( '?' );
  566.                     }
  567.                 }
  568.                 else
  569.                 {
  570.                     d->optarg = argv[d->optind++];
  571.                 }
  572.                 d->__nextchar = NULL;
  573.             }
  574.         }
  575.         return c;
  576.     }
  577. }

  578. int _getopt_internal( int argc
  579.                     , TCHAR *const * argv
  580.                     , const TCHAR* optstring
  581.                     , const struct option* longopts
  582.                     , int* longind
  583.                     , int long_only
  584.                     , int posixly_correct )
  585. {
  586.     int result;
  587.     getopt_data.optind = optind;
  588.     getopt_data.opterr = opterr;
  589.     result = _getopt_internal_r( argc
  590.                                , argv
  591.                                , optstring
  592.                                , longopts
  593.                                , longind
  594.                                , long_only
  595.                                , &getopt_data
  596.                                , posixly_correct );
  597.     optind = getopt_data.optind;
  598.     optarg = getopt_data.optarg;
  599.     optopt = getopt_data.optopt;
  600.     return result;
  601. }

  602. int getopt( int argc, TCHAR *const * argv, const TCHAR* optstring) _GETOPT_THROW
  603. {
  604.     return _getopt_internal( argc
  605.                            , argv
  606.                            , optstring
  607.                            , ( const struct option * ) 0
  608.                            , ( int * ) 0
  609.                            , 0
  610.                            , 0 );
  611. }

  612. int getopt_long( int argc
  613.                , TCHAR *const * argv
  614.                , const TCHAR* options
  615.                , const struct option* long_options
  616.                , int* opt_index ) _GETOPT_THROW
  617. {
  618.     return _getopt_internal( argc
  619.                            , argv
  620.                            , options
  621.                            , long_options
  622.                            , opt_index
  623.                            , 0
  624.                            , 0 );
  625. }

  626. int _getopt_long_r( int argc
  627.                   , TCHAR *const * argv
  628.                   , const TCHAR* options
  629.                   , const struct option* long_options
  630.                   , int* opt_index
  631.                   , struct _getopt_data* d )
  632. {
  633.     return _getopt_internal_r( argc
  634.                              , argv
  635.                              , options
  636.                              , long_options
  637.                              , opt_index
  638.                              , 0
  639.                              , d
  640.                              , 0 );
  641. }

  642. int getopt_long_only( int argc
  643.                     , TCHAR *const * argv
  644.                     , const TCHAR* options
  645.                     , const struct option* long_options
  646.                     , int* opt_index ) _GETOPT_THROW
  647. {
  648.     return _getopt_internal( argc
  649.                            , argv
  650.                            , options
  651.                            , long_options
  652.                            , opt_index
  653.                            , 1
  654.                            , 0 );
  655. }

  656. int _getopt_long_only_r( int argc
  657.                        , TCHAR *const * argv
  658.                        , const TCHAR* options
  659.                        , const struct option* long_options
  660.                        , int* opt_index
  661.                        , struct _getopt_data* d )
  662. {
  663.     return _getopt_internal_r( argc
  664.                              , argv
  665.                              , options
  666.                              , long_options
  667.                              , opt_index
  668.                              , 1
  669.                              , d
  670.                              , 0 );
  671. }

  672. void getopt_reset()
  673. {
  674.     optarg  = NULL;
  675.     optind  = 1;
  676.     opterr  = 1;
  677.     optopt  = _T( '?' );
  678.     //
  679.     getopt_data.optind            = 0;
  680.     getopt_data.opterr            = 0;
  681.     getopt_data.optopt            = 0;
  682.     getopt_data.optarg            = NULL;
  683.     getopt_data.__initialized     = 0;
  684.     getopt_data.__nextchar        = NULL;
  685.     getopt_data.__ordering        = 0;
  686.     getopt_data.__posixly_correct = 0;
  687.     getopt_data.__first_nonopt    = 0;
  688.     getopt_data.__last_nonopt     = 0;
  689. }
复制代码
示例test_getopt.c:
  1. # include <tchar.h>
  2. # include <ctype.h>
  3. # include <stdio.h>
  4. # include <stdlib.h>
  5. # include "getopt.h"

  6. int _tmain(int argc, TCHAR **argv)
  7. {
  8.     int aflag = 0;
  9.     int bflag = 0;
  10.     TCHAR *cvalue = NULL;
  11.     int index;
  12.     int c;
  13.     opterr = 0;
  14.     while((c = getopt(argc,argv, _T("abc:"))) != -1)
  15.     switch(c)
  16.     {
  17.     case 'a':
  18.         aflag = 1;
  19.         break;
  20.     case 'b':
  21.         bflag = 1;
  22.         break;
  23.     case 'c':
  24.         cvalue = optarg;
  25.         break;
  26.     case '?':
  27.         if(optopt == 'c')
  28.             _ftprintf(stderr,_T("Option -%c requires an argument.\n"),optopt);
  29.         else if(isprint(optopt))
  30.             _ftprintf(stderr,_T("Unknown option `-%c'.\n"),optopt);
  31.         else
  32.             _ftprintf(stderr,_T("Unknown option character `\\x%x'.\n"),optopt);
  33.         return 1;
  34.     default:
  35.         abort();
  36.     }
  37.     _tprintf(_T("aflag = %d, bflag = %d, cvalue = %s\n"), aflag,bflag,cvalue);
  38.     for(index = optind; index < argc; index++)
  39.         _tprintf(_T("Non-option argument %s\n"),argv[index]);
  40.     return 0;
  41. }
复制代码





扫码关注微信公众号,及时获取最新资源信息!下载附件优惠VIP会员5折;永久VIP免费
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。
 
在线客服
点击这里给我发消息 点击这里给我发消息 点击这里给我发消息
售前咨询热线
312337667

微信扫一扫,私享最新原创实用干货

QQ|免责声明|依星源码资源网 ( 鲁ICP备2021043233号-3 )|网站地图

GMT+8, 2024-4-27 22:20

Powered by Net188.com X3.4

邮箱:312337667@qq.com 客服QQ:312337667(工作时间:9:00~21:00)

快速回复 返回顶部 返回列表