简体   繁体   English

使用 c++ stl 替代 unistd.h getopt

[英]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.我正在为我们将在各种平台上使用的用户编写一个 C++ 命令行应用程序。 For this reason, I am writing using only stl.出于这个原因,我只使用 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).我想知道 stl 是否有一种很好的方法来解析类似于unistd.h/getopt.h使用的 getopt 函数的输入参数(因为据我所知,这些不是独立于平台的)。 I've already thought of doing something like while/for loop and switch statements, but wondering if there something a bit more elegant.我已经想过做一些类似while/for loopswitch语句的事情,但想知道是否有更优雅的东西。 C++11 is ok, boost is not. C++11 可以,boost 不行。

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).确实,标准 C++ 中没有办法,但getopt是 POSIX 标准,它涵盖了很多平台(Linux、*NIX、*BSD、MacOS、iOS、Android、Cygwin)。

On native Win32 simply embed something like wingetopt into your project (BSD-licensed).在本机 Win32 上,只需将wingetopt 之类的东西嵌入到您的项目中(BSD 许可)。 Also FreeBSD getopt.c can be easily ported (though it doesn't support long option format). FreeBSD getopt.c也可以很容易地移植(尽管它不支持长选项格式)。

The solution to this is to write your own or use boost.解决这个问题的方法是自己编写或使用 boost。 As of C++11, there is no standard "getopt" functionality.从 C++11 开始,没有标准的“getopt”功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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