Click here to Skip to main content
12,696,092 members (32,033 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

CommandLine parameters parser

, 14 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Get command line params in any order, any number, any type
<!-- Add the rest of your HTML here -->
CmdLineParser /command1:argument1 /command2 /command3:longer argument /commannd4:1234


Many applications can work with command line parameters. Usually it is a file name, for example you can call MS word with a document file name, and it will open it.

It is a useful feature. Also if you want to give more parameters it could be difficult deciding which parameter means what, which is necessary, and so on.

In this solution every parameter starts with a '/' character, the command name and the argument is separated by ':'.

For example:

CmdLineParser /file:file_name /print /option1:text with space /option2:1234

As it shows, you can use

  • command with argument
  • just command without argument like /PRINT
  • argument can contain space, any character except '/'
  • argument can be any type like text, number, BOOL, I mean any text that will be converted to your type!

Using the code

For testing, type some parameters in the project setting dialog (Alt-F7), dialog window, program arguments.

Project setting dialog (Alt-F7), dialog window

If you type the previous parameters, it reads the file name parameter, and gives an error message cause of unknown parameter:

if (!_strnicmp(command, "command", 10))
   strcpy(m_file, arg);
   if (!_strnicmp(command, "print", 10))

To convert your parameter use _strncmp which is not case sensitive, and then make your code to handle this command. To found unknown commands, if else pair work fine. You cannot use switch for strings. After you get all parameters your code can work with them!


It's working! :)


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

Web Developer Visionsoft, Best Film
Hungary Hungary
MCP ASP.NET Web Client Developer

You may also be interested in...

Comments and Discussions

QuestionWhat I was looking for. Pin
Onineko24-Apr-06 7:28
memberOnineko24-Apr-06 7:28 
This was one of the only samples I found that had a parser coming off of the lparam that gets passed into a Windows app's exe as opposed to all of the MFC samples that assumed you were using a Cstring. Great starter sample for writing a command line parser in a non-MFC/CString windows environemnt. This one was lightweight and didn't try to solve every command line combinatorial in one code snippet, which was also something I was looking for.

To the poster that flamed this, just because it wasn't helpful for you doesn't mean it wasn't helpful for everyone Smile | :)

Thanks Zoltan.;)
JokeRe: What I was looking for. Pin
Member 327217025-May-11 12:33
memberMember 327217025-May-11 12:33 
QuestionHow can I avoid contradict constant Pin
wuwenguo21-Apr-03 3:13
memberwuwenguo21-Apr-03 3:13 
Question2.33? Pin
Larry Antram18-Mar-03 10:10
memberLarry Antram18-Mar-03 10:10 
AnswerRe: 2.33? Pin
Zoltan19-Mar-03 0:26
memberZoltan19-Mar-03 0:26 
GeneralPoor Pin
dog_spawn15-Mar-03 13:18
memberdog_spawn15-Mar-03 13:18 
GeneralRe: Poor Pin
Zoltan16-Mar-03 0:28
memberZoltan16-Mar-03 0:28 
GeneralRe: Poor Pin
dog_spawn16-Mar-03 11:11
memberdog_spawn16-Mar-03 11:11 
GeneralRe: Poor? If works, is good ;) Pin
[ducted]19-Mar-03 23:10
member[ducted]19-Mar-03 23:10 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 Mar 2003
Article Copyright 2003 by Zoltan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid