可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm writing Win32 console application, which can be started with optional arguments like this:
app.exe /argName1:"argValue" /argName2:"argValue"
Do I have to parse it manually (to be able to determine, which arguments are present) from argc/argv variables, or does Win32 API contain some arguments parser?
回答1:
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++?
...
回答2:
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.
回答3:
You could mess around with various libraries and stuff... But sometimes all you require is something simple, practical and quick:
int i;
char *key, *value;
for( i = 1; i <= argc; i++ ) {
if( *argv[i] == '/' ) {
key = argv[i] + 1;
value = strchr(key, ':');
if( value != NULL ) *value++ = 0;
process_option( key, value );
} else {
process_value( argv[i] );
}
}
You get the idea...
This is assuming a normal Win32 console app as you have implied (which has a traditional main
function). For Win32 apps you come in at WinMain
instead, as another person has already commented.
回答4:
Just for the record, if you use MinGW's GCC, rather than Microsoft's MSVC, you get GNU getopt, (which also includes getopt_long and getopt_long_only variants), included within the standard runtime library.
回答5:
You can parse the arguments by using GetCommandLine, PathRemoveArgs, PathGetArgs
in a loop
https://msdn.microsoft.com/en-us/library/windows/desktop/bb773742(v=vs.85).aspx
回答6:
I have been developing and using libparamset that is written in plain C. It is really powerful and works well on Windows.
It provides:
- Is cross-platform.
- Wildcard support for file input on Windows!
- Powerful features. See libparamset.
回答7:
I don't believe that there is a Win32 API available. You can look for a Windows implementation of getopt or another library.
回答8:
Not sure about existence of such a win32 api function(s), but Boost.Program_Options library could help you.
回答9:
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.