Command Line Flags in C++

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <getopt.h>

int main(int argc, char* argv[]) {
    // Basic flag declarations are available for string,
    // integer, and boolean options. Here we declare a
    // string flag "word" with a default value "foo"
    // and a short description.
    std::string word = "foo";
    int numb = 42;
    bool fork = false;
    std::string svar = "bar";

    // Declare the supported options.
    static struct option long_options[] = {
        {"word", required_argument, 0, 'w'},
        {"numb", required_argument, 0, 'n'},
        {"fork", no_argument, 0, 'f'},
        {"svar", required_argument, 0, 's'},
        {0, 0, 0, 0}
    };

    int option_index = 0;
    int c;
    while ((c = getopt_long(argc, argv, "w:n:fs:", long_options, &option_index)) != -1) {
        switch (c) {
            case 'w':
                word = optarg;
                break;
            case 'n':
                numb = std::atoi(optarg);
                break;
            case 'f':
                fork = true;
                break;
            case 's':
                svar = optarg;
                break;
            case '?':
                // getopt_long already printed an error message.
                break;
            default:
                abort();
        }
    }

    // Here we'll just dump out the parsed options and
    // any trailing positional arguments.
    std::cout << "word: " << word << std::endl;
    std::cout << "numb: " << numb << std::endl;
    std::cout << "fork: " << std::boolalpha << fork << std::endl;
    std::cout << "svar: " << svar << std::endl;

    std::cout << "tail:";
    for (int i = optind; i < argc; i++) {
        std::cout << " " << argv[i];
    }
    std::cout << std::endl;

    return 0;
}

This C++ program demonstrates how to use command-line flags, which are a common way to specify options for command-line programs. For example, in wc -l the -l is a command-line flag.

In C++, we use the getopt_long function from the <getopt.h> library to parse command-line arguments. This is similar to the flag package in the original example.

To experiment with the command-line flags program, first compile it and then run the resulting binary directly.

$ g++ -o command-line-flags command-line-flags.cpp

Try out the built program by first giving it values for all flags.

$ ./command-line-flags --word=opt --numb=7 --fork --svar=flag
word: opt
numb: 7
fork: true
svar: flag
tail:

Note that if you omit flags they automatically take their default values.

$ ./command-line-flags --word=opt
word: opt
numb: 42
fork: false
svar: bar
tail:

Trailing positional arguments can be provided after any flags.

$ ./command-line-flags --word=opt a1 a2 a3
word: opt
numb: 42
fork: false
svar: bar
tail: a1 a2 a3

Unlike the flag package in the original example, getopt_long allows flags to appear after positional arguments. However, it’s generally a good practice to place all flags before positional arguments for consistency.

To get help text for the command-line program, you would typically add a --help option in your program and print usage information when it’s used. This isn’t automatically generated like in the original example, but you can implement it manually.

If you provide a flag that wasn’t specified, the program will print an error message.

Remember that C++ doesn’t have built-in garbage collection, so you need to be mindful of memory management, especially when dealing with dynamically allocated resources.