Click here to Skip to main content
13,044,551 members (56,939 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 14 Jan 2002

Simple command line processing

, 14 Jan 2002
Rate this:
Please Sign up or sign in to vote.
A simple method of parsing command line arguments; similar to getopt
<!-- Download Links --> <!-- Add the rest of your HTML here -->


I wanted a simple method to parse command line parameters that would compile on both Windows and Unix. I was familiar with getopt but I couldn't find a compilable version of it in a standalone version, so I decided to make a simple version of it myself.

The result is something that is something that uses the getopt format specification but isn't getopt. It is basic but that is what I wanted - something quick to learn, use and debug. This is the result.


  • Accepts getopt command line specification
  • Simple
  • Platform independent
  • Case dependent or independent matching


  • It isn't getopt
  • All options on the command line must use flags. i.e. "program [-o] input_file" won't work, use "program [-o] -i input_file" instead


  • Uses exceptions
  • If using case-insensitive option matching, ensure that the string passed through as a_pszOptions is all lowercase

Example Usage

    CSimpleOptions cl( argc, argv, "Oa:" );
    while ( )
        switch ( cl.option() )
        case 'O':
            bOption = true;
        case 'a':
            strArgument = cl.arg();
catch ( const SIMPLE_OPTIONS_EXCEPTION & e )
    fprintf( stderr, "Invalid argument: %s\n", e.what() );
    return 1;


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


About the Author

Brodie Thiesfield
Web Developer
Japan Japan
Must eat, like to travel, thus programming, since 1995.

You may also be interested in...


Comments and Discussions

GeneralNice but does not work with plain char instead of wchar_t Pin
DEGT25-Nov-07 22:20
memberDEGT25-Nov-07 22:20 
GeneralCross-platform command-line parsing and file globbing Pin
Brodie Thiesfield23-Apr-06 0:49
memberBrodie Thiesfield23-Apr-06 0:49 
GeneralMore powerful, more simple (new version) Pin
Brodie Thiesfiel13-May-04 20:09
memberBrodie Thiesfiel13-May-04 20:09 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 15 Jan 2002
Article Copyright 2002 by Brodie Thiesfield
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid