Click here to Skip to main content
12,064,539 members (64,595 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

98.6K views
13K downloads
92 bookmarked
Posted

Full getopt Port for Unicode and Multibyte Microsoft Visual C, C++, or MFC Projects

, 5 Jul 2011 LGPL3
Rate this:
Please Sign up or sign in to vote.
Supports getopt, getopt_long, and getopt_long_only and POSIXLY_CORRECT environment flag
This is an old version of the currently published article.

Introduction

This code was written after searching the Internet to no avail for a fully functional Microsoft C and C++ implementation of getopt, getopt_long, and getopt_long_only which would work in both Unicode and Multibyte builds.

It is a modification of the Free Software Foundation, Inc. getopt library for parsing command line arguments and the purpose is to provide a Microsoft Visual C friendly derivative. This code provides functionality for both Unicode and Multibyte builds and supports getopt, getopt_long, and getopt_long_only and the POSIXLY_CORRECT environment flag. The library uses standard Microsoft C typedefs for char and wchar_t via the _UNICODE preprocessor directive.

The original GNU code used several header and implementation files containing numerous preprocessor directives specific to Linux environments which have been removed. After removing unneeded dependencies, it was condensed into a single header and implementation file which could be added to any Visual C, C++, or MFC project. For the sake of brevity, this article doesn't discuss how to use the getopt functions. Anyone new to using the getopt functions should refer to the GNU tutorial for using getopt. Since getopt is licensed under LGPL, it is free to use in proprietary software.

Sample Code Provided

To help with understanding how to use the code, many versions have been provided for download. The following downloads are provided:

  • Visual Studio .NET 2008 ANSI Project
  • Visual Studio .NET 2005 ANSI Project
  • Visual Studio .NET 2005 MFC Project
  • Visual Studio 6 ANSI Project
  • Visual Studio 6 MFC Project

Using the Code

The code is used identical to GNU getopt.

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

int _tmain(int argc, TCHAR** argv)
{
    static int verbose_flag;
    int c;

    while (1)
    {        
        static struct option long_options[] =
        {
            {_T("verbose"), ARG_NONE, &verbose_flag, 1},
            {_T("brief"),   ARG_NONE, &verbose_flag, 0},
            {_T("add"),     ARG_NONE, 0, _T('a')},
            {_T("append"),  ARG_NONE, 0, _T('b')},
            {_T("delete"),  ARG_REQ,  0, _T('d')},
            {_T("create"),  ARG_REQ,  0, _T('c')},
            {_T("file"),    ARG_REQ, 0 , _T('f')},
            { ARG_NULL , ARG_NULL , ARG_NULL , ARG_NULL }
        };

        int option_index = 0;
        c = getopt_long(argc, argv, _T("abc:d:f:"), long_options, &option_index);

        // Check for end of operation or error
        if (c == -1)
            break;

        // Handle options
        switch (c)
        {
        case 0:
            /* If this option set a flag, do nothing else now. */
            if (long_options[option_index].flag != 0)
                break;
            _tprintf (_T("option %s"), long_options[option_index].name);
            if (optarg)
                _tprintf (_T(" with arg %s"), optarg);
            _tprintf (_T("\n"));
            break;

        case _T('a'):
            _tprintf(_T("option -a\n"));
            break;

        case _T('b'):
            _tprintf(_T("option -b\n"));
            break;

        case _T('c'):
            _tprintf (_T("option -c with value `%s'\n"), optarg);
            break;

        case _T('d'):
            _tprintf (_T("option -d with value `%s'\n"), optarg);
            break;

        case _T('f'):
            _tprintf (_T("option -f with value `%s'\n"), optarg);
            break;

        case '?':
            /* getopt_long already printed an error message. */
            break;

        default:
            abort();
        }
    }

    if (verbose_flag)
        _tprintf (_T("verbose flag is set\n"));


    if (optind < argc)
    {
        _tprintf (_T("non-option ARGV-elements: "));
        while (optind < argc) _tprintf (_T("%s "), argv[optind++]);
        _tprintf (_T("\n"));
    }
    return 0;
}     

Using this Code with C++ Precompiled Headers

When using this code in a C++ project with precompiled headers, it is necessary to rename getopt.c to getopt.cpp in order to circumvent the following compiler error:

“C1853 - Precompiled header file is from a previous version of the compiler, 
or the precompiled header is C++ and you are using it from C (or vice versa).”

Additionally precompiled header file must be added as the first include of the getopt.c or getopt.cpp file. For example, if you are using "stdafx.h" as the precompiled header, the following would be expected:

// File comments removed
#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include "getopt.h"

History

  • 02/03/2011 - Initial release
  • 02/20/2011 - Fixed L4 compiler warnings
  • 07/05/2011 - Added no_argument, required_argument, optional_argument def

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

Ludvik Jerabek
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionHere's a version that's completely free Pin
majortool3-Sep-15 17:10
membermajortool3-Sep-15 17:10 
AnswerRe: Here's a version that's completely free Pin
Ludvik Jerabek3-Sep-15 19:08
memberLudvik Jerabek3-Sep-15 19:08 
QuestionThanks Pin
Bhatt Piyush11-Jul-15 10:33
memberBhatt Piyush11-Jul-15 10:33 
AnswerRe: Thanks Pin
Ludvik Jerabek24-Jul-15 7:41
memberLudvik Jerabek24-Jul-15 7:41 
Question!!getenv() Pin
jfwfmt22-Nov-14 3:52
professionaljfwfmt22-Nov-14 3:52 
AnswerRe: !!getenv() Pin
jfwfmt22-Nov-14 4:21
professionaljfwfmt22-Nov-14 4:21 
QuestionType mismatch? Pin
TheZoc23-Oct-14 15:10
memberTheZoc23-Oct-14 15:10 
AnswerRe: Type mismatch? Pin
Ludvik Jerabek19-Jun-15 7:12
memberLudvik Jerabek19-Jun-15 7:12 
AnswerRe: Type mismatch? Pin
Ludvik Jerabek19-Jun-15 9:59
memberLudvik Jerabek19-Jun-15 9:59 
GeneralMy vote of 4 Pin
lomo7410-Jul-12 0:19
memberlomo7410-Jul-12 0:19 
GeneralRe: My vote of 4 Pin
Ludvik Jerabek23-Jul-12 0:31
memberLudvik Jerabek23-Jul-12 0:31 
GeneralRe: My vote of 4 Pin
Ludvik Jerabek2-Aug-12 9:33
memberLudvik Jerabek2-Aug-12 9:33 
GeneralRe: My vote of 4 Pin
lomo742-Aug-12 22:31
memberlomo742-Aug-12 22:31 
GeneralRe: My vote of 4 Pin
Ludvik Jerabek19-Jun-15 8:10
memberLudvik Jerabek19-Jun-15 8:10 
Question_GETOPT_THROW in getopt.c Pin
azder55617-Nov-11 22:09
memberazder55617-Nov-11 22:09 
AnswerRe: _GETOPT_THROW in getopt.c Pin
Ludvik Jerabek18-Nov-11 18:13
memberLudvik Jerabek18-Nov-11 18:13 
Questionhow to USE it Pin
ericoporto24-Sep-11 12:39
memberericoporto24-Sep-11 12:39 
AnswerRe: how to USE it Pin
Ludvik Jerabek24-Sep-11 13:14
memberLudvik Jerabek24-Sep-11 13:14 
QuestionJust an advisory about the licence Pin
sgllama9-Aug-11 6:59
membersgllama9-Aug-11 6:59 
AnswerRe: Just an advisory about the licence Pin
Ludvik Jerabek9-Aug-11 13:31
memberLudvik Jerabek9-Aug-11 13:31 
GeneralRe: Just an advisory about the licence Pin
Member 112436138-Jan-15 12:26
professionalMember 112436138-Jan-15 12:26 
GeneralRe: Just an advisory about the licence Pin
Ludvik Jerabek20-Jun-15 20:02
memberLudvik Jerabek20-Jun-15 20:02 
GeneralMy vote of 5 Pin
MicroImaging8-Aug-11 10:02
memberMicroImaging8-Aug-11 10:02 
GeneralRe: My vote of 5 Pin
seachanged26-Dec-11 13:01
memberseachanged26-Dec-11 13:01 
GeneralRe: My vote of 5 Pin
Ludvik Jerabek20-Jun-15 20:06
memberLudvik Jerabek20-Jun-15 20:06 
GeneralMy vote of 1 Pin
kouhinn26-Jul-11 21:42
memberkouhinn26-Jul-11 21:42 
GeneralRe: My vote of 1 Pin
Ludvik Jerabek3-Aug-11 16:25
memberLudvik Jerabek3-Aug-11 16:25 
GeneralThanks & suggestion Pin
Lu.P11-Mar-11 12:27
memberLu.P11-Mar-11 12:27 
GeneralRe: Thanks & suggestion Pin
Ludvik Jerabek12-Mar-11 15:53
memberLudvik Jerabek12-Mar-11 15:53 
GeneralRe: Thanks & suggestion Pin
Lu.P13-Mar-11 7:16
memberLu.P13-Mar-11 7:16 
GeneralMy vote of 5 Pin
Member 16579821-Feb-11 21:48
memberMember 16579821-Feb-11 21:48 
GeneralVery nice! Pin
Hans Dietrich19-Feb-11 9:09
mentorHans Dietrich19-Feb-11 9:09 
GeneralRe: Very nice! Pin
Ludvik Jerabek19-Feb-11 14:23
memberLudvik Jerabek19-Feb-11 14:23 
GeneralRe: Very nice! Pin
Ludvik Jerabek20-Feb-11 12:52
memberLudvik Jerabek20-Feb-11 12:52 
GeneralMy vote of 5 Pin
Sharjith14-Feb-11 4:59
memberSharjith14-Feb-11 4:59 
GeneralRe: My vote of 5 Pin
Ludvik Jerabek14-Feb-11 6:07
memberLudvik Jerabek14-Feb-11 6:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160204.4 | Last Updated 5 Jul 2011
Article Copyright 2011 by Ludvik Jerabek
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid