Click here to Skip to main content
Click here to Skip to main content

CommandLine parameters parser

By , 14 Mar 2003
 
CmdLineParser /command1:argument1 /command2 /command3:longer argument /commannd4:1234

Introduction

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);
   }
else
   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!

History

It's working! :)

License

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

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhat I was looking for.memberOnineko24 Apr '06 - 6:28 
JokeRe: What I was looking for.memberMember 327217025 May '11 - 11:33 
Excellent sentiments! I have gone through so-called sophisticated coding samples and, when you pull away all of the persiflage, it's absolute crap. By the same token, sometimes the most pedestrian examples have some tidbits and techniques that have saved me hours of research through those massive C++ tomes.
 
..besides, I'd think twice about flaming a guy from a former Iron Curtain country named Zoltan!
QuestionHow can I avoid contradict constantmemberwuwenguo21 Apr '03 - 2:13 
Question2.33?memberLarry Antram18 Mar '03 - 9:10 
AnswerRe: 2.33?memberZoltan18 Mar '03 - 23:26 
GeneralPoormemberdog_spawn15 Mar '03 - 12:18 
GeneralRe: PoormemberZoltan15 Mar '03 - 23:28 
GeneralRe: Poormemberdog_spawn16 Mar '03 - 10:11 
GeneralRe: Poor? If works, is good ;)member[ducted]19 Mar '03 - 22:10 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 15 Mar 2003
Article Copyright 2003 by Zoltan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid