473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Command line parser

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
Jul 19 '05 #1
4 7531
In article <p9********************************@4ax.com>, 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
Jul 19 '05 #2
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

Jul 19 '05 #3

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

Jul 19 '05 #4
Boogie El Aceitoso <fr****@telefonica.net> wrote in message news:<p9********************************@4ax.com>. ..
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;
}
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jon Hewer | last post by:
hi i am writing a little script and currently implementing command line arguments following the guide by mark pilgrim from dive into python; ...
4
by: Greg B | last post by:
Well since getopt() doesn't seem to be compatible with Windows, and the free implementation of it for Windows that I found still had some annoying restrictions, I thought I'd whip up a simple...
17
by: News | last post by:
Hi everyone, My goal is to pull command switches/options from a file and then assign the values to select variables which would eventually be included in a class object. The data file looks...
2
by: PyPK | last post by:
Is there a Command line parser in python that can: 1. resolve conflicts 2. specify something like requires 3. and smart for ex: python test.py --a --b --c --d Case 1: If 'a' is an option...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
6
by: 31337one | last post by:
Hello all, I was wondering if there is a C/C++ command line parser that works for linux AND windows. . Anyways, the ideal parser im looking for should be able to support multiple arguments...
16
by: John Salerno | last post by:
Here's my new project: I want to write a little script that I can type at the terminal like this: $ scriptname package1 where scriptname is my module name and any subsequent arguments are the...
0
by: vincent90152900 | last post by:
I'm trying to get PowerShell script to run a program. Here is the command line I need to run: "\"C:\\Program Files\\PDF2allTest\\pdf2all\" -c pdf2jpg -s \"c:\\test\\test\\01.pdf\" -or 600" I...
4
by: cjt22 | last post by:
Hi there. I just wondered whether anyone could recommend the correct way I should be passing command line parameters into my program. I am currently using the following code: def main(argv =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.