Click here to Skip to main content
Click here to Skip to main content

XGetopt - A Unix-compatible getopt() for MFC and Win32

, 20 May 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
XGetopt provides drop-in Unix-style command line processing for Win32 and MFC applications


I have ported many apps (probably too many!) from Unix to Windows, and most of them need some command line processing. In Unix, standard method is to call getopt(), but there is no equivalent in Windows. I have gotten so used to getopt() that I like to use it even in new Windows apps - it has a clean interface that is very understandable and self-documenting.

XGetopt Demo

So here is my version of getopt() for Windows. Please note it does not have all features of latest GNU getopt() - the limitation are documented in source header. It is pretty much a drop-in replacement for any app already using getopt().

The demo project provides a sample app that shows how command line is processed. Press "Call getopt" button and getopt() will be invoked with command line you have entered.

      XGetopt screenshot

How To Use

To integrate XGetopt into your own app, you first need to add following files to your project:

  • XGetopt.cpp
  • XGetopt.h

Next, include header file XGetopt.h in dialog's .cpp file (if it is a dialog-based app) or your CWinApp .cpp, and create a ProcessCommandLine() function, like the one in the demo app. See XGetoptTestDlg.cpp for an example.

Revision History

Version 1.2 - 2003 May 17

  • Added Unicode support

Version 1.1 - 2002 March 10

  • Added example to XGetopt.cpp module header


The XGetopt demo uses code from Matt Pietrek's LIBCTINY library. The source code for LIBCTINY may be found here.


This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please consider posting the new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see

Comments and Discussions

GeneralMy vote of 5 PinmemberJacques Abada16-Feb-12 7:53 
GeneralMy vote of 5 Pinmemberrrarey6-Feb-12 15:42 
QuestionHelpful PinmemberJim Chung6-Sep-11 23:39 
GeneralBuggy Pinmemberchengiz524-Feb-09 12:12 
GeneralDecent job but *does not* pertain to standard in many ways [modified] Pinmemberchengiz524-Feb-09 9:56 
Problems wrt unix standard getopt:
1. optopt is not supported.
2. opterr is declared but not defined or used. The code behaves as if opterr is 0, ie. no error messages are written.
3. optind is supposed to be 1 before first call to getopt. Here it is 0. FWIW the doc does mention this. (*)
4. optind is not supposed to change for "together" options, as it is supposed to be the index of the next arg to be processed. For example, for "exec -ab" (both -a, -b are options), optind is supposed to be 1 after -a is processed; thus 1 before (from 3. above), 1 after. Here it is 0 before, 2 after.

I'm sorry, but standard compatibility is 80% of the work of such low level code. OTOH lack of it makes it a true MS getopt Smile | :)

(*) The reason optind is 1 is because argv starts to be processed at index optind, which works out well for command line args. At least I can use a 'standard' getopt for any set of such args (which dont have the 'program name' in front) by simply presetting optind to 0. With this implementation, I cannot do anything other than prefixing a dummy argument to my array.

modified on Tuesday, February 24, 2009 3:19 PM

GeneralNow available for C# PinmvpHans Dietrich8-Jun-07 14:59 
GeneralThanks Pinmemberjimchung26-Feb-07 0:00 
GeneralVery useful - Cheers! Pinmemberalcoholic_satan29-Jul-06 7:09 
GeneralThank you very much PinsussAnonymous8-Aug-05 8:29 
GeneralHelpUsing XGetopt Pinmemberjames2003_james1-Aug-05 9:11 
Generalgetopt() source code available on MSDN PinmemberZele27-May-03 8:01 
GeneralRe: getopt() source code available on MSDN PinsussAnonymous18-Sep-03 23:44 
GeneralRe: getopt() source code available on MSDN PinmemberZele19-Sep-03 0:17 
GeneralBrilliant PinmemberDave J Smith21-Dec-02 12:34 
Generalnon option arguments Pinmemberexo13-Mar-02 6:49 
GeneralRe: non option arguments PinmemberHans Dietrich13-Mar-02 8:23 
GeneralThanks a lot ,Very good! Pinmemberjeasonzhao10-Mar-02 21:40 
GeneralVery good stuff :-) PinmemberNish [BusterBoy]10-Mar-02 7:06 
GeneralQuick idea PinmemberGiles10-Mar-02 4:49 
GeneralRe: Quick idea PinmemberGiles10-Mar-02 4:50 
GeneralRe: Quick idea PinmemberMatt Newman10-Mar-02 4:56 
GeneralRe: Quick idea PinmemberGiles10-Mar-02 5:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.150414.1 | Last Updated 21 May 2003
Article Copyright 2002 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid