简体   繁体   中英

Alternative to unistd.h getopt using c++ stl

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.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM