Win32 API command line arguments parsing

前端 未结 9 2009
小蘑菇
小蘑菇 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.
    0 讨论(0)
  • 2020-12-16 14:40

    The only support that Win32 provides for command line arguments are the functions GetCommandLine and CommandLineToArgvW. This is exactly the same as the argv parameter that you have for a console application.

    You will have to do the parsing yourself. Regex would be a good option for this.

    0 讨论(0)
  • 2020-12-16 14:42

    There is no Win32 support for parsing command-line arguments.

    See related articles at MSDN:
    Parsing C++ Command-Line Arguments
    Argument Definitions
    Customizing C++ Command-Line Processing

    also look at similar questions:
    What parameter parser libraries are there for C++?
    Parsing parameters to main()
    Option Parsers for C/C++?
    What's an effective way to parse command line parameters in C++?
    ...

    0 讨论(0)
提交回复
热议问题