Full getopt port for Unicode and Multibyte Microsoft Visual C, C++, or MFC projects
- Download Source - 4.46 KB
- Download Visual Studio 6 Ansi Project - 11.28 KB
- Download Visual Studio 6 MFC Project - 22.01 KB
- Download Visual Studio 2005 .NET Ansi Project - 9.51 KB
- Download Visual Studio 2005 .NET MFC Project - 13.56 KB
- Download Visual Studio 2008 .NET Ansi Project - 9.42 KB
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 can 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; int digit_optind = 0; 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
发表评论
tc5Nrx Respect to op , some wonderful information.
5NqWAK You, my pal, ROCK! I found exactly the info I already searched everywhere and simply could not find it. What a perfect web site.
hzFHcw You made some nice points there. I did a search on the topic and found most individuals will consent with your website.
pretty valuable material, overall I consider this is really worth a bookmark, thanks
bQWPj1 You got a very excellent website, Glad I noticed it through yahoo.
MMDzTm lectura cartas del tarot gratis tirada tarot gratis amor
QsxKwT So pleased to possess located this post.. My browsing efforts seem total.. thanks. Liking the article.. appreciate it Respect the entry you furnished..
gwQCR8 wow, awesome article.Really looking forward to read more. Cool.
53BEem Hey, thanks for the article post.Really looking forward to read more. Really Great.
fi0kAA F*ckin' awesome things here. I'm very glad to see your article. Thanks a lot and i am looking forward to contact you. Will you kindly drop me a e-mail?
ZTZwYP Wow! Thank you! I permanently needed to write on my blog something like that. Can I take a part of your post to my site?
UEy54A Great work! This is the type of info that should be shared around the web. Shame on the search engines for not positioning this post higher! Come on over and visit my site . Thanks =)
ZRxWFs Fantastic website. Plenty of useful information here. I'm sending it to a few friends ans also sharing in delicious. And of course, thanks for your sweat!
imyJNU We stumbled over here by a different website and thought I might check things out. I like what I see so now i am following you. Look forward to finding out about your web page yet again.
3CPmdO Excellent post however I was wanting to know if you could write a litte more on this topic? I'd be very thankful if you could elaborate a little bit further. Kudos!
3w2dmT I really like and appreciate your article.Really looking forward to read more. Great.
Aw7B58 I value the article.Really thank you! Cool.
HqyddH I really liked your post.Really looking forward to read more. Want more.
n5Bh98 I truly appreciate this post.Really thank you! Really Cool.
aa3kop Thanks again for the article post. Fantastic.
MedYlU Thanks a lot for the blog. Much obliged.
NpCr7d I really like and appreciate your blog.Thanks Again. Really Great.
JZ74aS Very good article post.Thanks Again.
9AJz0l I cannot thank you enough for the blog. Will read on...
OJTntb Im thankful for the post.Really thank you! Cool.
czACGJ Great, thanks for sharing this blog post.Thanks Again. Keep writing.
gGFX8k Thanks again for the article post.Really thank you! Really Cool.
sBiO2d Say, you got a nice blog.Really thank you! Great.