Parsing Command Line Arguments in C++?
Boost.Program_options should do the trick
Is there a standardized way to parse command line arguments in C?
Is there a standardized way to parse command line arguments in C?
Yes there is. Not standardized by the C committee, but by others. The most commonly widespread is POSIX with it's Utility Conventions and getopt utility. Using GNU Argument Syntax with argp is just fun and cool. There is also commonly used GNUs getopt_long for supporting long arguments with getopt.
The only standardized and portable way to parse command line args is to do it yourself?
There is none standardized in C11 way to parse command line. The C11 only specifies, that the main arguments are strings, but they're value is just implementation-defined:
If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment.
The C standard doesn't introduce an abstraction of a "command line", so an abstraction consisting of "command line arguments" and furthermore "command line arguments parsing" is just not defined. I think introducing a standardized way to parse command line arguments is out of scope for a C standard.
The most portable way is to write and use the most portable code. Indeed the most portable code would not use any non-standard C libraries and "do it yourself" in a most portable manner.There is little sense to target all possible architectures and environments. If you are going only for GNU/Linux, I would go with getopt
if you want to stay portable to some crazy environment and for argp
if you want just to target GNU/Linux specific systems. getopt
is really widespread, even a library for embedded systems like newlib has getopt
and getopt_long
implemented. For others, you can just copy/include the code for getopt from other sources into your program, thus protecting against environments that doesn't have it.
Related Topics
What Open Source C++ Static Analysis Tools Are Available
Is There a Limit on Number of Open Files in Windows
Where Exactly Does C++ Standard Say Dereferencing an Uninitialized Pointer Is Undefined Behavior
Getting Std :: Ifstream to Handle Lf, Cr, and Crlf
Why Must the Copy Assignment Operator Return a Reference/Const Reference
How to Sort Two Vectors in the Same Way, With Criteria That Uses Only One of the Vectors
Why Does Integer Overflow on X86 With Gcc Cause an Infinite Loop
How Does C++ Linking Work in Practice
What Is the Partial Ordering Procedure in Template Deduction
Tellg() Function Give Wrong Size of File
Capturing Stdout from a System() Command Optimally
When Should I Use C++ Private Inheritance
How to Call the Base Class Constructor
Generate Random Numbers Following a Normal Distribution in C/C++
Programmatically Create Static Arrays At Compile Time in C++