Command line parser

Discussion in 'C++' started by Boogie El Aceitoso, Oct 24, 2003.

  1. 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
     
    Boogie El Aceitoso, Oct 24, 2003
    #1
    1. Advertising

  2. In article <>, 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
     
    Andreas Kahari, Oct 24, 2003
    #2
    1. Advertising

  3. 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
     
    Thomas Matthews, Oct 24, 2003
    #3
  4. 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
     
    Arthur J. O'Dwyer, Oct 25, 2003
    #4
  5. Boogie El Aceitoso

    J. Campbell Guest

    Boogie El Aceitoso <> wrote in message news:<>...
    > 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;
    }
     
    J. Campbell, Oct 25, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa

    Parser for command line arguments?

    Ahmed Moustafa, Aug 21, 2003, in forum: Java
    Replies:
    0
    Views:
    396
    Ahmed Moustafa
    Aug 21, 2003
  2. Greg B

    simple command line parser

    Greg B, Sep 2, 2003, in forum: C Programming
    Replies:
    4
    Views:
    530
  3. Manlio Perillo

    Yet Another Command Line Parser

    Manlio Perillo, Oct 26, 2004, in forum: Python
    Replies:
    9
    Views:
    374
    Manlio Perillo
    Oct 27, 2004
  4. PyPK

    Powerful Command line parser

    PyPK, Apr 21, 2006, in forum: Python
    Replies:
    0
    Views:
    394
  5. Replies:
    4
    Views:
    3,033
    Robert Mark Bram
    Oct 31, 2006
Loading...

Share This Page