Win32 API command line arguments parsing

前端 未结 9 2496
小蘑菇
小蘑菇 2020-12-16 13:52

I\'m writing Win32 console application, which can be started with optional arguments like this:

app.exe /argName1:\"argValue\" /argName2:\"argValue\"
         


        
9条回答
  •  渐次进展
    2020-12-16 14:36

    If your needs are simple, you might want to take a look at Argh!.
    It is single header and super easy to use:

    int main(int, char* argv[])
    {
        argh::parser cmdl(argv);          // declare
    
        if (cmdl[{ "-v", "--verbose" }])  // use immediately
            std::cout << "Verbose, I am.\n";
    
        return EXIT_SUCCESS;
    }
    

    By being unintrusive, it doesn't take over you main() function.

    From the Readme:

    Philosophy

    Contrary to many alternatives, argh takes a minimalist laissez-faire approach, very suitable for fuss-less prototyping with the following rules:

    The API is:

    • Minimalistic but expressive:
      • No getters nor binders
      • Just the [] and () operators.
      • Easy iteration (range-for too).
    • You don't pay for what you don't use;
    • Conversion to typed variables happens (via std::istream >>) on the user side after the parsing phase;
    • No exceptions thrown for failures.
    • Liberal BSD license;
    • Single header file;
    • No non-std dependencies.

    argh does not care about:

    • How many '-' preceded your option;
    • Which flags and options you support - that is your responsibility;
    • Syntax validation: any command line is a valid (not necessarily unique) combination of positional parameters, flags and options;
    • Automatically producing a usage message.

提交回复
热议问题