Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
20 May 2003 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here