Click here to Skip to main content
13,042,421 members (71,894 online)
Click here to Skip to main content
Add your own
alternative version

Stats

334.5K views
3.6K downloads
119 bookmarked
Posted 19 Jun 2001

CCmdLine - A command line parser

, 25 Aug 2011
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..

You may also be interested in...

Comments and Discussions

 
GeneralRe: Possible bug Pin
Chris Losinger31-Jan-02 6:40
memberChris Losinger31-Jan-02 6:40 
GeneralRe: Possible bug Pin
Garth J Lancaster3-Feb-02 12:52
memberGarth J Lancaster3-Feb-02 12:52 
GeneralRe: Possible bug Pin
Chris Losinger3-Feb-02 17:01
memberChris Losinger3-Feb-02 17:01 
GeneralCCmdLine + STLport 4.5 = memory leaks 1280 bytes long Pin
Odissey25-Sep-01 17:47
memberOdissey25-Sep-01 17:47 
GeneralRe: CCmdLine + STLport 4.5 = memory leaks 1280 bytes long Pin
Chris Losinger25-Sep-01 17:57
memberChris Losinger25-Sep-01 17:57 
GeneralRe: CCmdLine + STLport 4.5 = memory leaks 1280 bytes long Pin
Odissey2-Oct-01 1:29
memberOdissey2-Oct-01 1:29 
GeneralMinor article errata Pin
robin12-Sep-01 0:18
memberrobin12-Sep-01 0:18 
GeneralProblems with methods... Pin
eve*26-Jun-01 2:57
membereve*26-Jun-01 2:57 
GeneralRe: Problems with methods... Pin
Chris Losinger26-Jun-01 3:30
memberChris Losinger26-Jun-01 3:30 
GeneralRe: Problems with methods... Pin
eve*26-Jun-01 3:40
membereve*26-Jun-01 3:40 
GeneralRe: Problems with methods... Pin
eve*26-Jun-01 4:44
membereve*26-Jun-01 4:44 
GeneralRe: Problems with methods... Pin
Chris Losinger26-Jun-01 4:49
memberChris Losinger26-Jun-01 4:49 
GeneralRe: Problems with methods... Pin
eve*26-Jun-01 5:05
membereve*26-Jun-01 5:05 
GeneralSuggestion Pin
Arnaud Brejeon20-Jun-01 15:26
memberArnaud Brejeon20-Jun-01 15:26 
Hi,

I like it a lot, it's really nice and powerful.
Can I suggest something to improve it a little?
Instead of having GetSafeArgument returning a StringType, why don't you take advantage of templates to be able to get any kind of return depending on what the user wants : int, float, string, ...
That's what I did in http://www.codeproject.com/soap/paramio.asp
and it's very efficient and convenient to use.

It was just an idea... if I have time I may try to implement it.

Arnaud
GeneralAn alternative Command Line Parser Pin
Chris Sells20-Jun-01 7:04
memberChris Sells20-Jun-01 7:04 
General??? Pin
Chris Losinger20-Jun-01 7:26
memberChris Losinger20-Jun-01 7:26 
GeneralRe: ??? Pin
Philippe Lhoste25-Jun-01 22:33
memberPhilippe Lhoste25-Jun-01 22:33 
GeneralRe: ??? Pin
Chris Losinger26-Jun-01 3:38
memberChris Losinger26-Jun-01 3:38 

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
Web02 | 2.8.170713.1 | Last Updated 25 Aug 2011
Article Copyright 2001 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid