Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Command line parser

Reply
Thread Tools

Command line parser

 
 
Boogie El Aceitoso
Guest
Posts: n/a
 
      10-24-2003
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
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      10-24-2003
In article <(E-Mail Removed)>, Boogie El Aceitoso wrote:
> 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...


--
Andreas Kähäri
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      10-24-2003
Boogie El Aceitoso wrote:

> 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

 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      10-25-2003

On Fri, 24 Oct 2003, Boogie El Aceitoso wrote:
>
> 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

 
Reply With Quote
 
J. Campbell
Guest
Posts: n/a
 
      10-25-2003
Boogie El Aceitoso <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> 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:arseit(){
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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java command line space & escape character parser for Runtime.exec jimmy_please@yahoo.com Java 4 10-31-2006 11:07 PM
Powerful Command line parser PyPK Python 0 04-21-2006 05:55 PM
Yet Another Command Line Parser Manlio Perillo Python 9 10-27-2004 08:34 AM
simple command line parser Greg B C Programming 4 09-02-2003 06:53 PM
Parser for command line arguments? Ahmed Moustafa Java 0 08-21-2003 05:21 AM



Advertisments