Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C++

CCmdLine - A command line parser

Rate me:
Please Sign up or sign in to vote.
4.87/5 (23 votes)
25 Aug 2011CPOL1 min read 391.1K   3.9K   121   96
A simple command line parsing class.

Introduction

Parsing command lines with the standard C string functions (strlen, strcpy, etc.) or even with the CString operators is a nightmare. But, CCmdLine makes it effortless.

CCmdLine was written for use with console apps, but can be easily used in any application which requires command-line parsing.

CCmdLine uses STL for its collection classes, so it works in both MFC and non-MFC apps. If you are using this in an MFC app, the switches and arguments will be returned as 'CString's. If you are using this in a non-MFC app, they will be returned as STL 'string's.

CCmdLine is a simple way to parse a command line into switches and arguments. Example :

MyApp.exe -sw1 arg1 arg2 -sw2 arg3 -sw3 -sw4

When using CCmdLine, "-sw1", "-sw2", "-sw3" and "-sw4" are switches and "arg1", "arg2" and "arg3" are arguments.

Example:

Assume the application we're writing uses a command line that has two required switches and two optional switches. The app should abort if the required switches are not present and continue with default values if the optional switches are not present.

    Sample command line :

    MyApp.exe -p1 text1 text2 -p2 "this is a big argument" -opt1 -55 -opt2

    Switches -p1 and -p2 are required.

    p1 has two arguments and p2 has one.

    Switches -opt1 and -opt2 are optional.

    opt1 requires a numeric argument.

    opt2 has no arguments.

    Also, assume that the app displays a 'help' screen if the '-h' switch is present on the command line.

Example

Here's how you can use CCmdLine to handle the command line processing:

// if this is an MFC app, un-comment the next line
// #include "stdafx.h"

#include "CmdLine.h"

void main(int argc, char **argv)
{
  // our cmd line parser object
  CCmdLine cmdLine;

  // parse the command line.
  // in MFC apps, use __argc and __argv here
  if (cmdLine.SplitLine(argc, argv) < 1)
  {
     // no switches were given on the command line, abort
     ASSERT(0);
     exit(-1);
  }

  // test for the 'help' case
  if (cmdLine.HasSwitch("-h"))
  {
     show_help();
     exit(0);
  }

  // StringType is defined in CmdLine.h.
  // it is CString when using MFC, else STL's 'string'
  StringType p1_1, p1_2, p2_1;

  // get the required arguments
  try
  {
     // if any of these GetArgument calls fail,
     // we'll end up in the catch() block

     // get the first -p1 argument (arg number '0')
     p1_1 = cmdLine.GetArgument("-p1", 0);

     // get the second -p1 argument (arg number '1')
     p1_2 = cmdLine.GetArgument("-p1", 1);

     // get the first -p2 argument
     p2_1 = cmdLine.GetArgument("-p2", 0);

  }
  catch (...)
  {
     // one of the required arguments was missing, abort
     ASSERT(0);
     exit(-1);
  }

  // get the optional parameters

  // the GetSafeArgument member does not throw exceptions, and
  // allows for the use of a default value, if the switch is not found.

  // get the argument, convert it to an int.
  // default to '100', if the argument was not found
  int iOpt1Val =    atoi( cmdLine.GetSafeArgument( "-opt1", 0, 100 ) );

  // since opt2 has no arguments, just test for the presence of
  // the '-opt2' switch
  bool bOptSwitch2 =   cmdLine.HasSwitch("-opt2");

  .... and so on....

}

Pretty simple stuff...

History

  • 31 Jan 2002 - Updated source.
  • 25 Aug 2011 - Updated source.

License

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


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
GeneralRe: Program aborts... Pin
Chris Losinger13-Aug-03 11:03
professionalChris Losinger13-Aug-03 11:03 
GeneralRe: Program aborts... Pin
jocko7713-Aug-03 11:27
jocko7713-Aug-03 11:27 
GeneralRe: Program aborts... Pin
amatis19-Aug-03 2:52
amatis19-Aug-03 2:52 
GeneralUNICODE Pin
Valery A. Boronin1-Apr-03 8:10
Valery A. Boronin1-Apr-03 8:10 
GeneralRe: UNICODE Pin
Valery A. Boronin1-Apr-03 8:42
Valery A. Boronin1-Apr-03 8:42 
GeneralRe: UNICODE Pin
Chris Losinger1-Apr-03 8:51
professionalChris Losinger1-Apr-03 8:51 
GeneralExample doesn't work at all Pin
Jigit21-Feb-03 13:35
Jigit21-Feb-03 13:35 
GeneralRe: Example doesn't work at all Pin
Chris Losinger21-Feb-03 14:02
professionalChris Losinger21-Feb-03 14:02 
Generalmemory leak in CCmdLine Pin
mvyver16-Jan-03 14:54
mvyver16-Jan-03 14:54 
GeneralRe: memory leak in CCmdLine Pin
mvyver19-Jan-03 11:00
mvyver19-Jan-03 11:00 
QuestionCompile problem...? Pin
adb24-Oct-02 13:31
adb24-Oct-02 13:31 
AnswerRe: Compile problem...? Pin
Chris Losinger24-Oct-02 13:36
professionalChris Losinger24-Oct-02 13:36 
GeneralGetArgumentCount Problem Pin
Tim Hodgson18-Oct-02 8:35
Tim Hodgson18-Oct-02 8:35 
GeneralRe: GetArgumentCount Problem Pin
Chris Losinger18-Oct-02 9:33
professionalChris Losinger18-Oct-02 9:33 
GeneralRe: GetArgumentCount Problem Pin
Tim Hodgson18-Oct-02 9:55
Tim Hodgson18-Oct-02 9:55 
GeneralSplitLine(const char *line) morph Pin
helfrich24-Sep-02 14:44
helfrich24-Sep-02 14:44 
QuestionHow can I check a lpCmdLine? Pin
Ing. Lele29-Jul-02 8:39
sussIng. Lele29-Jul-02 8:39 
GeneralAlmost what I need... Pin
Nemanja Trifunovic18-Jul-02 10:27
Nemanja Trifunovic18-Jul-02 10:27 
GeneralRe: Almost what I need... Pin
Chris Losinger18-Jul-02 10:31
professionalChris Losinger18-Jul-02 10:31 
GeneralCode Counter Pin
28-Mar-02 6:05
suss28-Mar-02 6:05 
GeneralRe: Code Counter Pin
Chris Losinger28-Mar-02 6:05
professionalChris Losinger28-Mar-02 6:05 
Generalthank you.. and a little question.. Pin
Bernhard7-Mar-02 21:26
Bernhard7-Mar-02 21:26 
GeneralRe: thank you.. and a little question.. Pin
Chris Losinger8-Mar-02 1:46
professionalChris Losinger8-Mar-02 1:46 
GeneralRe: thank you.. and a little question.. Pin
Bernhard10-Mar-02 19:09
Bernhard10-Mar-02 19:09 
GeneralMinor note about stdafx.h Pin
ATL_Man_WA11-Feb-02 12:16
ATL_Man_WA11-Feb-02 12:16 

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.