2

I'm learning C programming language and trying to do some basic stuff with it. The problem I faced is how to parse command line arguments. I read this answer and tried to find the library summary for unistd.h functions int the standard N1570. But unfortunately it is not defined there (Work only for POSIX compliant OS as far as I can understand).

AFAIK Windows is not really POSIX compliant so something like in the answer I referred to above

#include <unistd.h>

int main(int argc, char *argv[]){
    int opt;
    while((opt = getopt(argc, argv, "ilv") != -1){
        //do some with it
    }
}

is not really portable.

QUESTION: The only standardized and portable way to parse command line args is to do it yourself?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Some Name
  • 8,555
  • 5
  • 27
  • 77
  • 3
    like pretty much everything else in C... but `getopt` is widely spread. Windows has good versions of gcc where getopt works very well. Embedded systems, well they don't need arguments. – Jean-François Fabre Nov 22 '18 at 05:08
  • 1
    `getopt()` is POSIX, `getopt_long()` is GNU, there's other libraries for it you can search for, or just do it yourself. – Shawn Nov 22 '18 at 05:11
  • @Jean-FrançoisFabre Good point about embedded systems. Did not take it into account. Thank you. – Some Name Nov 22 '18 at 05:12
  • 1
    Although the ISO/C spec and POSIX define some libc functions, POSIX is a superset, so follow it in lieu of ISO/C for functions. For example, POSIX defines things related to syscalls, network sockets, etc. that ISO/C doesn't. The linux kernel's support for libc functions (i.e. syscalls) follows POSIX and _not_ ISO/C. On embedded systems, you'd have to consult the embedded OS's documentation on this (as Jean-Francois mentioned). – Craig Estey Nov 22 '18 at 05:18
  • 2
    Yes there are standards. No there is not A C standard. – chux - Reinstate Monica Nov 22 '18 at 05:21
  • Well, actually `` is an unrelated header that has nothing to do with the use of **getopt(3)**. To use `getopt()` you need to `#include ` which is also nonstandard, (well, POSIX is also a portability standard between UNIX systems) Have you read about `argc` and `argv` parameters to `main()`? This is actually the portable interface. What do you want to do exactly? – Luis Colorado Nov 27 '18 at 08:26

1 Answers1

4

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Just to say, `getopt()` is not a GNU invention, but predates from v7 of the AT&T unix version, i thing. Just my two cents. – Luis Colorado Nov 27 '18 at 08:28