|
|
| C/C++ cross platform command line parser |
|
Thomas Fjellstrom
Member #476
June 2000
|
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
Member #3,861
September 2003
|
Write your own in perl / python or faster yacc / flex / bison ? "Code is like shit - it only smells if it is not yours" |
|
Thomas Fjellstrom
Member #476
June 2000
|
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
Member #14,628
October 2012
|
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
|
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
Member #14,628
October 2012
|
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
|
It's also incredibly tedious -- |
|
Matthew Leverton
Supreme Loser
January 1999
|
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
|
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. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
Thomas Fjellstrom
Member #476
June 2000
|
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 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. -- |
|
|