BioBoost / course-object-oriented-programming-with-cpp

C++ Programming Course for VIVES University of Applied Sciences (Bachelor Degree)
https://evolved-cpp.netlify.app/
8 stars 10 forks source link

Useful libraries - getopts #60

Open BioBoost opened 1 year ago

BioBoost commented 1 year ago

https://github.com/jarro2783/cxxopts

BioBoost commented 1 year ago

Alternative is basic getopt which is less powerful:

#include <getopt.h>
#include <iostream>

int main(int argc, char* argv[]) {
    option longopts[] = {
        {"prefix", optional_argument, nullptr, 'p'}, 
        {"binary", optional_argument, nullptr, 'b'}, 
        {"hex", optional_argument, nullptr, 'h'}, {0}
    };

    while (1) {
        const int opt = getopt_long(argc, argv, "pbh::", longopts, 0);

        if (opt == -1) {
            break;
        }

        switch (opt) {
            case 'p':
                // ...
                std::cout << "Remove prefix" << std::endl;
                break;
            case 'b':
                std::cout << "Binary" << std::endl;
                // ...
                break;
            case 'h':
                std::cout << "Hex" << std::endl;
                // ...
                break;
        }
    }

    //...
}