3

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?

6
  • 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. Commented Nov 22, 2018 at 5:08
  • 1
    getopt() is POSIX, getopt_long() is GNU, there's other libraries for it you can search for, or just do it yourself. Commented Nov 22, 2018 at 5:11
  • @Jean-FrançoisFabre Good point about embedded systems. Did not take it into account. Thank you. Commented Nov 22, 2018 at 5: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). Commented Nov 22, 2018 at 5:18
  • 2
    Yes there are standards. No there is not A C standard. Commented Nov 22, 2018 at 5:21

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.