|
Command Line Parser
Submitted by |
Hi, I'm Albert Sandberg (thec) and been visiting this site for quite some
time and thought it was time for me to contribute to the community.
I've been writing an Command Line Parser which takes a line such as:
demo.exe /fullscreen /x:320 /y:240 /time:52.32
And parses the line that you simply can pick out which parameters are passed
to the program. It also allowes wierd lines such as:
demo.exe / fullscreen /x: 523
To be parsed even if that's not to good syntax :-)
I'm only using stl and windows.h so you shouldn't have any problems with
it...
All sourcecode may be downloaded, modified and used until the fingers
burned. I know for instance that I don't support "demo.exe -a -x" but that
you should be able to fix under a minute, I'll let it be up to you to fix
:-) Note that the sourcecode may state a different licence agreement, but
this piece of description override whatever it says in the sourcecode!
Happy programming!
Albert "thec" Sandberg
|
Currently browsing [commandline.zip] (1,819 bytes) - [commandlineparser.h] - (1,343 bytes)
#ifndef COMMAND_LINE_PARSER
#define COMMAND_LINE_PARSER
/*
=============================================================================
= Helper Library. (c) Outbreak 2001
=============================================================================
@ Responsible : Thec
@ Brief : Command Line Parser, parses lines like:
"demo.exe /fullscreen /x:320 /y:240 /time:50.00 /bpp:32"
=============================================================================
*/
#include <string>
#include <vector>
namespace Helper {
// Parses a command line like "demo.exe /fullscreen /x:320 /y:240 /time:52.00
class CommandLineParser {
private:
// Entity data
struct EntityData {
std::string name;
std::string value;
};
// Entity list.
std::vector<EntityData> entity;
// Trim and sets entity.
void addEntity(const std::string& input);
// Trimmer
std::string trim(const std::string& input);
public:
// Creates the register of strings
CommandLineParser();
// Check if command is entered, good for detecting "demo.exe /fullscreen"
const bool isEntity(const std::string& name);
// Get entity with entered name, for instance, "demo.exe /x:320 /y:240 /bpp:32"
const std::string getEntity(const std::string& name);
};
}
#endif |
|
Currently browsing [commandline.zip] (1,819 bytes) - [commandlineparser.cpp] - (3,408 bytes)
#include "commandlineparser.h"
#include <windows.h>
#include <helper/core/debug/debug.h>
//············································································
Helper::CommandLineParser::CommandLineParser() {
// Get command line arguments
std::string args = GetCommandLine();
Debug::log("CommandLineParser::CommandLineParser", "Command line found: %s", args.c_str());
int start=-1;
// Loop through argument line and search for entities.
for (int i=0; i<args.length(); i++) {
// Found new entity.
if (args[i] == '/') {
// Only if not first time.
if ((start != -1) && (start != (i-1))) {
// Adds entity to the list and trims the value.
addEntity(args.substr(start+1, i-start-1));
}
// Store position for next '/'
start = i;
}
}
// Add remaining string
if ((start!=-1) && (start < i-1)) {
addEntity(args.substr(start+1, i-start-1));
}
}
//············································································
void Helper::CommandLineParser::addEntity(const std::string& input) {
// Set defaults
std::string name=input;
std::string value="";
// Loop through and look for ":" to make a value.
for (int i=1; i<input.length(); i++) {
// Found seperator
if ((input[i] == ':') || (input[i] == '=')) {
// Set name
name = input.substr(0,i);
// Set value, if any
if (i+1 < input.length()) {
value = input.substr(i+1, input.length()-i-1);
}
break;
}
}
// Trim values
name = trim(name);
value = trim(value);
// Only add if name is valid!
if (name.length() != 0) {
// Debug
Debug::log("CommandLineParser::addEntity()", "Entity added, name: %s, value: %s", name.c_str(), value.c_str());
// Set up entity structure
EntityData ed;
ed.name = name;
ed.value = value;
// Add entity
entity.push_back(ed);
}
}
//············································································
std::string Helper::CommandLineParser::trim(const std::string& input) {
// If string is empty, there is nothing to look for.
if (input.length()==0) return "";
// Set up temporary
std::string final=input;
// Remove spaces at beginning
int i=0;
while ((i<input.length()) && (input[i]==' ')) {
i++;
}
// String full of spaces, return nothing.
if (i >= input.length()) return "";
if (i>0) {
final = input.substr(i, input.length()-i);
}
// Remove spaces at end
i=final.length()-1;
while ((i>=0) && (final[i]==' ')) {
i--;
}
final = final.substr(0, i+1);
// Return the new string
return final;
}
//············································································
const bool Helper::CommandLineParser::isEntity(const std::string& name) {
// Loop through entities
for (int i=0; i<entity.size(); i++) {
// If match, return true
if (entity[i].name == name) return true;
}
return false;
}
//············································································
const std::string Helper::CommandLineParser::getEntity(const std::string& name) {
// Loop through entities
for (int i=0; i<entity.size(); i++) {
// If match, return true
if (entity[i].name == name) return entity[i].value;
}
return "";
}
//············································································
|
|
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.
|