jarro2783 / cxxopts

Lightweight C++ command line option parser
MIT License
4.22k stars 592 forks source link

Inconsistent defaults when none specified [Bug?] #382

Closed HumanGamer closed 1 year ago

HumanGamer commented 1 year ago

I'm making a simple game with raylib and decided to use the command line for various settings (at least for now). I was originally parsing it myself, but I've decided to go with cxxopts to make the implementation cleaner.

I've noticed if I remove the default value for integers (tested with width and height), the value of the variables stays the same as the initial value (1280x720).

However! if I remove the default value for booleans (tested with vSync), the value becomes false even though the initial value was true.

I would like it to keep the initial value so that I don't have to have a bunch of string variables for my defaults.

Is this a bug or is there something in the API that is making it behave this way than I can change?

The version I'm using is the latest commit as of today (December 12th 2022): e9d20c2c078076da928288fdbae2a39431b4f253

Here's the relevant portion of code:

const char* gameName = "Simple Game";
const char* gameDescription = "A simple game";
int screenWidth = 1280;
int screenHeight = 720;
bool fullscreen = false;
bool borderless = false;

bool msaa = true;
bool vSync = true;

// ...

int main(int argc, char* argv[])
{
    cxxopts::Options options(gameName, gameDescription);
    options.add_options()
        ("f,fullscreen", "Fullscreen", cxxopts::value<bool>(fullscreen)->default_value("false"))
        ("b,borderless", "Borderless", cxxopts::value<bool>(borderless)->default_value("false"))
        ("width", "Width", cxxopts::value<int>(screenWidth))//->default_value("1280"))
        ("height", "Height", cxxopts::value<int>(screenHeight))//->default_value("720"))
        ("m,msaa", "MSAA", cxxopts::value<bool>(msaa)->default_value("true"))
        ("v,vsync", "VSync", cxxopts::value<bool>(vSync))//->default_value("true"))
        ("h,help", "Print usage")
        ;

    auto result = options.parse(argc, argv);

    if (msaa)
        SetConfigFlags(FLAG_MSAA_4X_HINT);

   // ...

    InitWindow(screenWidth, screenHeight, gameName);
    if (vSync)
        SetWindowState(FLAG_VSYNC_HINT);

    /// ...
}

Thank you for this great library, and thank you in advance for helping me out!

jarro2783 commented 1 year ago

That seems wrong, I would expect it not to touch the value in that case.