Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C/C++ cross platform command line parser

This thread is locked; no one can reply to it. rss feed Print
C/C++ cross platform command line parser
Thomas Fjellstrom
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

GullRaDriel
Member #3,861
September 2003
avatar

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

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Thomas Fjellstrom
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Raidho36
Member #14,628
October 2012
avatar

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
Member #476
June 2000
avatar

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?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Raidho36
Member #14,628
October 2012
avatar

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

Thomas Fjellstrom
Member #476
June 2000
avatar

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

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Matthew Leverton
Supreme Loser
January 1999
avatar

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

bamccaig
Member #7,536
July 2006
avatar

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
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: