Click here to Skip to main content
11,430,829 members (76,174 online)
Click here to Skip to main content

CCmdLine - A command line parser

, 25 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Chris Losinger
Software Developer
United States United States
Chris Losinger is the president of Smaller Animals Software, Inc..

Comments and Discussions

 
Bugbug in GetArgumentCount Pin
xpk74-May-12 10:45
memberxpk74-May-12 10:45 
GeneralRe: bug in GetArgumentCount Pin
Chris Losinger4-May-12 11:08
memberChris Losinger4-May-12 11:08 
GeneralRe: bug in GetArgumentCount Pin
xpk74-May-12 11:16
memberxpk74-May-12 11:16 
GeneralRe: bug in GetArgumentCount Pin
Chris Losinger4-May-12 11:19
memberChris Losinger4-May-12 11:19 
xpk7 wrote:
GetArgumentCount uses HasSwitch fn, which appends this character to the input string.


ah. that's right.
good catch.

SuggestionAdd support for legacy command line Pin
Parsley7222-Oct-11 18:56
memberParsley7222-Oct-11 18:56 
SuggestionSmall class to parse command line without STL or MFC Pin
ggtours31-Aug-11 11:25
memberggtours31-Aug-11 11:25 
GeneralRe: Small class to parse command line without STL or MFC Pin
Member 41764165-Sep-11 19:59
memberMember 41764165-Sep-11 19:59 
QuestionWhat exception type gets thrown? (Avoid using catch (...) ) Pin
Mike Diack29-Aug-11 23:57
memberMike Diack29-Aug-11 23:57 
AnswerRe: What exception type gets thrown? (Avoid using catch (...) ) Pin
Chris Losinger30-Aug-11 1:53
memberChris Losinger30-Aug-11 1:53 
QuestionUsing _CCmdLine as a member variable Pin
Member 121682129-Aug-11 0:12
memberMember 121682129-Aug-11 0:12 
AnswerRe: Using _CCmdLine as a member variable Pin
Chris Losinger29-Aug-11 2:17
memberChris Losinger29-Aug-11 2:17 
GeneralLots of error C2664 Pin
ozatomic24-Feb-11 13:46
memberozatomic24-Feb-11 13:46 
GeneralRe: Lots of error C2664 Pin
Chris Losinger24-Feb-11 16:59
memberChris Losinger24-Feb-11 16:59 
GeneralRe: Lots of error C2664 [modified] Pin
vontom24-Aug-11 20:39
membervontom24-Aug-11 20:39 
GeneralRe: Lots of error C2664 Pin
Chris Losinger25-Aug-11 2:36
memberChris Losinger25-Aug-11 2:36 
GeneralRe: Lots of error C2664 Pin
vontom25-Aug-11 3:41
membervontom25-Aug-11 3:41 
GeneralRe: Lots of error C2664 Pin
Chris Losinger25-Aug-11 4:02
memberChris Losinger25-Aug-11 4:02 
GeneralRe: Lots of error C2664 [OT] Pin
Garth J Lancaster25-Aug-11 13:28
memberGarth J Lancaster25-Aug-11 13:28 
Generallooks great, but can't get it to work Pin
thready1-Mar-10 9:59
memberthready1-Mar-10 9:59 
GeneralRe: looks great, but can't get it to work Pin
Chris Losinger1-Mar-10 10:26
memberChris Losinger1-Mar-10 10:26 
GeneralRe: looks great, but can't get it to work Pin
thready1-Mar-10 11:11
memberthready1-Mar-10 11:11 
GeneralRe: looks great, but can't get it to work Pin
thready1-Mar-10 11:13
memberthready1-Mar-10 11:13 
GeneralRe: looks great, but can't get it to work Pin
Chris Losinger1-Mar-10 11:57
memberChris Losinger1-Mar-10 11:57 
GeneralRe: looks great, but can't get it to work Pin
thready1-Mar-10 12:02
memberthready1-Mar-10 12:02 
GeneralRe: looks great, but can't get it to work Pin
Chris Losinger1-Mar-10 13:00
memberChris Losinger1-Mar-10 13:00 
GeneralRe: looks great, but can't get it to work Pin
thready1-Mar-10 12:49
memberthready1-Mar-10 12:49 
GeneralRe: looks great, but can't get it to work Pin
Chris Losinger1-Mar-10 13:03
memberChris Losinger1-Mar-10 13:03 
GeneralRe: looks great, but can't get it to work Pin
thready1-Mar-10 13:41
memberthready1-Mar-10 13:41 
GeneralRe: looks great, but can't get it to work Pin
Chris Losinger1-Mar-10 13:46
memberChris Losinger1-Mar-10 13:46 
GeneralRe: looks great, but can't get it to work Pin
thready1-Mar-10 13:52
memberthready1-Mar-10 13:52 
GeneralRe: looks great, but can't get it to work Pin
thready1-Mar-10 14:05
memberthready1-Mar-10 14:05 
Questionhow can i use it Pin
xfuajiao11-Dec-08 0:00
memberxfuajiao11-Dec-08 0:00 
AnswerRe: how can i use it Pin
Chris Losinger11-Dec-08 2:35
memberChris Losinger11-Dec-08 2:35 
GeneralRe: how can i use it Pin
xfuajiao11-Dec-08 15:45
memberxfuajiao11-Dec-08 15:45 
GeneralRe: how can i use it Pin
xfuajiao11-Dec-08 16:37
memberxfuajiao11-Dec-08 16:37 
GeneralRe: how can i use it Pin
Chris Losinger11-Dec-08 16:42
memberChris Losinger11-Dec-08 16:42 
GeneralRe: how can i use it Pin
xfuajiao11-Dec-08 17:17
memberxfuajiao11-Dec-08 17:17 
GeneralRe: how can i use it Pin
Chris Losinger11-Dec-08 17:19
memberChris Losinger11-Dec-08 17:19 
GeneralRe: how can i use it Pin
xfuajiao11-Dec-08 17:39
memberxfuajiao11-Dec-08 17:39 
GeneralVisual BNF parser generator Pin
AdamSlosarski13-Mar-07 19:03
memberAdamSlosarski13-Mar-07 19:03 
GeneralPocket PC Pin
Jonathan Schmidt (foz)5-Jun-04 9:07
memberJonathan Schmidt (foz)5-Jun-04 9:07 
Generalfatal error C1010: unexpected end of file while looking for precompiled header directive Pin
nico2000024-Nov-03 23:36
membernico2000024-Nov-03 23:36 
GeneralRe: fatal error C1010: unexpected end of file while looking for precompiled header directive Pin
Chris Losinger25-Nov-03 2:31
memberChris Losinger25-Nov-03 2:31 
GeneralRe: fatal error C1010: unexpected end of file while looking for precompiled header directive Pin
Anonymous10-May-05 11:42
sussAnonymous10-May-05 11:42 
GeneralRe: fatal error C1010: unexpected end of file while looking for precompiled header directive Pin
Chris Losinger10-May-05 11:50
memberChris Losinger10-May-05 11:50 
GeneralProgram aborts... Pin
jocko7713-Aug-03 10:59
memberjocko7713-Aug-03 10:59 
General? Pin
Chris Losinger13-Aug-03 11:10
memberChris Losinger13-Aug-03 11:10 
GeneralRe: Program aborts... Pin
jocko7713-Aug-03 11:35
memberjocko7713-Aug-03 11:35 
GeneralRe: Program aborts... Pin
Chris Losinger13-Aug-03 11:49
memberChris Losinger13-Aug-03 11:49 
GeneralRe: Program aborts... Pin
jocko7713-Aug-03 11:54
memberjocko7713-Aug-03 11:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 25 Aug 2011
Article Copyright 2001 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid