Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to unistd.h getopt using c++ stl

Tags:

c++

c++11

stl

argv

I'm writing a C++ command line application for users that we will be used on various platforms. For this reason, I am writing using only stl. I'm wondering if stl has a nice way to parse input arguments similar to the getopt function used in unistd.h/getopt.h (as these are not platform independent to my knowledge). I've already thought of doing something like while/for loop and switch statements, but wondering if there something a bit more elegant. C++11 is ok, boost is not.

like image 707
zeus_masta_funk Avatar asked Feb 04 '26 20:02

zeus_masta_funk


2 Answers

The solution to this is to write your own or use boost. As of C++11, there is no standard "getopt" functionality.

like image 58
zeus_masta_funk Avatar answered Feb 06 '26 09:02

zeus_masta_funk


It's true that there isn't a way in standard C++ but getopt is a POSIX standard, which covers a lot of platforms (Linux, *NIX, *BSD, MacOS, iOS, Android, Cygwin).

On native Win32 simply embed something like wingetopt into your project (BSD-licensed). Also FreeBSD getopt.c can be easily ported (though it doesn't support long option format).

like image 24
rustyx Avatar answered Feb 06 '26 08:02

rustyx