Command line parser

B

Boogie El Aceitoso

Hi,

I need a command line parser that understands filename swith spaces.
Since I'm absolutelly sure I'm not the first developer to need a command line
parser, I was wondering is there's a decent open source one available.

I really don't want to reinvent the wheel.... O:)

TIA
 
A

Andreas Kahari

Hi,

I need a command line parser that understands filename swith spaces.
Since I'm absolutelly sure I'm not the first developer to need a command line
parser, I was wondering is there's a decent open source one available.

This is a bit off topic for the groups you chose to send it to.

Having said that, most systems I've worked with allows arguments
to be enclosed in single or double quotes...
 
T

Thomas Matthews

Boogie said:
Hi,

I need a command line parser that understands filename swith spaces.
Since I'm absolutelly sure I'm not the first developer to need a command line
parser, I was wondering is there's a decent open source one available.

I really don't want to reinvent the wheel.... O:)

TIA

Since there is no standard layout for a command line, I suggest that you
write your own. These parsers are not that big nor difficult.

Some OSes use the "-" as a token separator (i.e. hyphenated words get
broken into two tokens), others don't. Same with '/'.

Sometimes, the wheel may have to be altered for differing road
conditions (or applications of said wheel, like tanks and race cars).

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
A

Arthur J. O'Dwyer

I need a command line parser that understands filename swith spaces.
Since I'm absolutelly sure I'm not the first developer to need a command
line parser, I was wondering is there's a decent open source one available.

What do you need, exactly? You see, all operating systems I know
of do all the difficult parsing for you ahead-of-time, especially
the kind dealing with "filenames with spaces." E.g., if you call
a program like this:

prompt> myprog eenie meenie "My Documents" > foobar

what your C++ program will actually see internally is something
like this:

argv[0]: something like "myprog"
argv[1]: "eenie"
argv[2]: "meenie"
argv[3]: "My Documents"
argv[4]: NULL

You see, the space in "My Documents" has been preserved because
the user typed in quotes at the command line. No special
parsing necessary.

On the other hand, if you want a parser that can handle simple
Unix-style command-line tags like

prompt> myprog -abcd -o output.txt --foo --whozit

then you might be interested in some of the source code here:

http://www.contrib.andrew.cmu.edu/~ajo/free-software/

for instance, 'detab.c', 'rot13.c', or 'quine.c'. These
programs' main() functions contain some simple code for this
kind of parsing that I've found to be very useful and extensible;
I'd be glad to explain it in more detail if you think it's what
you're looking for.

Many systems have functions called 'getopt' or similar that
can do fancy argument parsing, but those packages aren't
standard C or C++, and if you want help with them you would
do better to ask in a group related to the particular package.

HTH,
-Arthur
 
J

J. Campbell

Boogie El Aceitoso said:
Hi,

I need a command line parser that understands filename swith spaces.
Since I'm absolutelly sure I'm not the first developer to need a command line
parser, I was wondering is there's a decent open source one available.

I really don't want to reinvent the wheel.... O:)

TIA

if using [dos]...this may help. you can use, eg ifstream in(Cmd.fs.c_str());

#include<string>
#include<iostream>
#include<fstream>

using namespace std;

class Filespec{
private:
void parseit();
public:
Filespec(char a[]);
~Filespec();

string fs;
string name;
string path;
string root;
string ext;
};

Filespec::Filespec(char a[]):fs(a) {
parseit();
name = root + ext;
}

Filespec::~Filespec(){}

void Filespec::parseit(){
string temp = fs;
int bs = temp.rfind('\\');
if (temp.rfind('.') != -1){
ext = temp.substr(temp.rfind('.'), (temp.size() - temp.rfind('.')));
temp = temp.substr(0, temp.size() - ext.size());
}
if (bs != -1){
root = temp.substr((bs+1), (temp.size()-bs));
path = temp.substr(0, temp.size() - root.size());
}
else {root = temp;
path = "";
}
}

int main(int argc, char* argv[]){
if (argc < 2){
cout << "no command line argument supplied\n";
return 0;
}
Filespec Cmd(argv[1]);
cout << "The filespec is\n" << Cmd.path + Cmd.name
<< "\npath is\n" << Cmd.path
<< "\nroot is\n" << Cmd.root
<< "\nextension is\n" << Cmd.ext
<< "\nfilename is\n" << Cmd.name << endl;
string t; getline(cin, t);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top