C/C++ cross platform command line parser
Thomas Fjellstrom

I'm looking for a good cross platform cli parser for a little project I'm working on. It absolutely has to work in C++ (using C++11 for the project), and at least support linux and windows (via mingw and potentially msvc, if I can't get the code to properly work in mingw, since I'm using std::thread, it may not work in mingw.. we'll see).

append: Boost is a last resort.

GullRaDriel

Write your own in perl / python or faster yacc / flex / bison ?

Thomas Fjellstrom

It's just for parsing command line arguments/options. writing it in a scripting language would be, odd to say the least. And I suppose I could write a full parser for it, but thats a bit overkill. I can whip something up with some basic string parsing, but I'd prefer to use something that's already proven.

Raidho36

What's so hard with parsing command line options manually? First off, this is done fine with default library. And they're all already fit in a bunch of strings for you in char ** argv, one per string, quotes already handled. Just scanf your strings for values. You may preselect strigns to process as keys if second letter would be dash and otherwise preselect strings to scanf with strncmp including trailing equals sign if scanf -ing every string against every possible key would take too long.

Thomas Fjellstrom

Yes, I could. But there are a few features that I'd like to have, that are less than trivial to support. Why code my own flexible option parser when one already exists?

Raidho36

Because that's a good practice and you'd spend more time looking for it than actually implementing it.

Thomas Fjellstrom

It's also incredibly tedious :P and reinventing the wheel for no good reason.

Matthew Leverton

Is getopt what you are looking for? The PHP version works great, which I assume is just a wrapper over this C function.

bamccaig

Yes, getopt is what you're looking for. Whether or not it's ported to MinGW or not, I don't know, but I don't see why not. It's not doing anything exceptional. The Wikipedia article that ML linked to seems to have a link to a Windows port for Visual C++ even (not necessarily GNU-compatible, but good enough, I imagine).

For the n00bs among us, I'm sure what TF is looking for is things like: -gnu to be the same as -g -n -u, and for -- -gnu to be received as no options and "-gnu" literally in argv. These are conventions that a UNIX user becomes accustomed to, and rightly so. Windows is so weak in this regard. I've rolled my own a few times, but it's still a lot of work to support all of these features gracefully (particularly in a low-level language like C or C++), and it's so much easier using getopt.

Protip: In Perl, there's Getopt::Long.

Thomas Fjellstrom

getopt isn't really what I was looking for. It doesn't do gnu style options.

bam is mostly right. I'd also like for --option to be a single option, rather than a group of options.

believe it or not I know about getopt :o

that said, the project that the wiki links to, actually seems to support getopt_long. It'll do.

append: well it might do. It's LGPL, not sure I like the license for this project.
append2: I may just end up doing my own due to the license, and requirements. I was hoping there were more options available.

Thread #612582. Printed from Allegro.cc