Click here to Skip to main content
6,596,602 members and growing! (19,238 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate License: The Code Project Open License (CPOL)

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

By Hans Dietrich

XGetopt provides drop-in Unix-style command line processing for Win32 and MFC applications
VC6Win2K, WinXP, MFC, Dev
Posted:9 Mar 2002
Updated:20 May 2003
Views:112,618
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
45 votes for this article.
Popularity: 7.69 Rating: 4.65 out of 5

1
1 vote, 4.3%
2
1 vote, 4.3%
3
2 votes, 8.7%
4
19 votes, 82.6%
5

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)

About the Author

Hans Dietrich


Member
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.
Occupation: Software Developer (Senior)
Company: Hans Dietrich Software
Location: United States United States

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralBuggy Pinmemberchengiz512:12 24 Feb '09  
GeneralDecent job but *does not* pertain to standard in many ways [modified] Pinmemberchengiz59:56 24 Feb '09  
GeneralNow available for C# PinmvpHans Dietrich14:59 8 Jun '07  
GeneralThanks Pinmemberjimchung0:00 26 Feb '07  
GeneralVery useful - Cheers! Pinmemberalcoholic_satan7:09 29 Jul '06  
GeneralThank you very much PinsussAnonymous8:29 8 Aug '05  
GeneralHelpUsing XGetopt Pinmemberjames2003_james9:11 1 Aug '05  
Generalgetopt() source code available on MSDN PinmemberZele8:01 27 May '03  
GeneralRe: getopt() source code available on MSDN PinsussAnonymous23:44 18 Sep '03  
GeneralRe: getopt() source code available on MSDN PinmemberZele0:17 19 Sep '03  
GeneralBrilliant PinmemberDave J Smith12:34 21 Dec '02  
Generalnon option arguments Pinmemberexo6:49 13 Mar '02  
GeneralRe: non option arguments PinmemberHans Dietrich8:23 13 Mar '02  
GeneralThanks a lot ,Very good! Pinmemberjeasonzhao21:40 10 Mar '02  
GeneralVery good stuff :-) PinmemberNish [BusterBoy]7:06 10 Mar '02  
GeneralQuick idea PinmemberGiles4:49 10 Mar '02  
GeneralRe: Quick idea PinmemberGiles4:50 10 Mar '02  
GeneralRe: Quick idea PinmemberMatt Newman4:56 10 Mar '02  
GeneralRe: Quick idea PinmemberGiles5:29 10 Mar '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 May 2003
Editor: Chris Maunder
Copyright 2002 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project