Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
4.87/5 (33 votes)
20 May 2003CPOL2 min read 301.3K   12.3K   82   23
XGetopt provides drop-in Unix-style command line processing for Win32 and MFC applications

Introduction

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

Acknowledgments

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

Usage

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.

License

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


Written By
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 www.hdsoft.org.






Comments and Discussions

 
QuestionMessage Closed Pin
25-Nov-20 20:55
pukaly25-Nov-20 20:55 
Generalgetopt.h Pin
FrankLucido30-May-19 23:38
FrankLucido30-May-19 23:38 
GeneralMy vote of 5 Pin
Jacques Abada16-Feb-12 6:53
Jacques Abada16-Feb-12 6:53 
GeneralMy vote of 5 Pin
rrarey6-Feb-12 14:42
rrarey6-Feb-12 14:42 
QuestionHelpful Pin
Jim Chung6-Sep-11 22:39
Jim Chung6-Sep-11 22:39 
GeneralBuggy Pin
chengiz524-Feb-09 11:12
chengiz524-Feb-09 11:12 
GeneralDecent job but *does not* pertain to standard in many ways [modified] Pin
chengiz524-Feb-09 8:56
chengiz524-Feb-09 8:56 
GeneralNow available for C# Pin
Hans Dietrich8-Jun-07 13:59
mentorHans Dietrich8-Jun-07 13:59 
GeneralThanks Pin
jimchung25-Feb-07 23:00
jimchung25-Feb-07 23:00 
GeneralVery useful - Cheers! Pin
alcoholic_satan29-Jul-06 6:09
alcoholic_satan29-Jul-06 6:09 
GeneralThank you very much Pin
Anonymous8-Aug-05 7:29
Anonymous8-Aug-05 7:29 
GeneralHelpUsing XGetopt Pin
james2003_james1-Aug-05 8:11
james2003_james1-Aug-05 8:11 
Generalgetopt() source code available on MSDN Pin
Zele27-May-03 7:01
Zele27-May-03 7:01 
GeneralRe: getopt() source code available on MSDN Pin
Anonymous18-Sep-03 22:44
Anonymous18-Sep-03 22:44 
GeneralRe: getopt() source code available on MSDN Pin
Zele18-Sep-03 23:17
Zele18-Sep-03 23:17 
GeneralBrilliant Pin
Dave J Smith21-Dec-02 11:34
Dave J Smith21-Dec-02 11:34 
Generalnon option arguments Pin
brakes200213-Mar-02 5:49
brakes200213-Mar-02 5:49 
GeneralRe: non option arguments Pin
Hans Dietrich13-Mar-02 7:23
mentorHans Dietrich13-Mar-02 7:23 
GeneralThanks a lot ,Very good! Pin
Jeason Zhao10-Mar-02 20:40
Jeason Zhao10-Mar-02 20:40 
GeneralVery good stuff :-) Pin
Nish Nishant10-Mar-02 6:06
sitebuilderNish Nishant10-Mar-02 6:06 
GeneralQuick idea Pin
Giles10-Mar-02 3:49
Giles10-Mar-02 3:49 
GeneralRe: Quick idea Pin
Giles10-Mar-02 3:50
Giles10-Mar-02 3:50 
GeneralRe: Quick idea Pin
Matt Newman10-Mar-02 3:56
Matt Newman10-Mar-02 3:56 
GeneralRe: Quick idea Pin
Giles10-Mar-02 4:29
Giles10-Mar-02 4:29 

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.