Is there a standardized way to parse command line arguments in C?












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?










share|improve this question




















  • 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 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.

    – Shawn
    Nov 22 '18 at 5:11











  • @Jean-FrançoisFabre Good point about embedded systems. Did not take it into account. Thank you.

    – Some Name
    Nov 22 '18 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).

    – Craig Estey
    Nov 22 '18 at 5:18






  • 1





    Yes there are standards. No there is not A C standard.

    – chux
    Nov 22 '18 at 5:21
















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?










share|improve this question




















  • 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 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.

    – Shawn
    Nov 22 '18 at 5:11











  • @Jean-FrançoisFabre Good point about embedded systems. Did not take it into account. Thank you.

    – Some Name
    Nov 22 '18 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).

    – Craig Estey
    Nov 22 '18 at 5:18






  • 1





    Yes there are standards. No there is not A C standard.

    – chux
    Nov 22 '18 at 5:21














2












2








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?










share|improve this question
















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?







c linux command-line posix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 5:13









Jean-François Fabre

104k955112




104k955112










asked Nov 22 '18 at 5:06









Some NameSome Name

1,389416




1,389416








  • 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 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.

    – Shawn
    Nov 22 '18 at 5:11











  • @Jean-FrançoisFabre Good point about embedded systems. Did not take it into account. Thank you.

    – Some Name
    Nov 22 '18 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).

    – Craig Estey
    Nov 22 '18 at 5:18






  • 1





    Yes there are standards. No there is not A C standard.

    – chux
    Nov 22 '18 at 5:21














  • 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 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.

    – Shawn
    Nov 22 '18 at 5:11











  • @Jean-FrançoisFabre Good point about embedded systems. Did not take it into account. Thank you.

    – Some Name
    Nov 22 '18 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).

    – Craig Estey
    Nov 22 '18 at 5:18






  • 1





    Yes there are standards. No there is not A C standard.

    – chux
    Nov 22 '18 at 5:21








3




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 5:08







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 5:08






1




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 5:11





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 5:11













@Jean-FrançoisFabre Good point about embedded systems. Did not take it into account. Thank you.

– Some Name
Nov 22 '18 at 5:12





@Jean-FrançoisFabre Good point about embedded systems. Did not take it into account. Thank you.

– Some Name
Nov 22 '18 at 5:12




1




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 5:18





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 5:18




1




1





Yes there are standards. No there is not A C standard.

– chux
Nov 22 '18 at 5:21





Yes there are standards. No there is not A C standard.

– chux
Nov 22 '18 at 5:21












1 Answer
1






active

oldest

votes


















2















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.






share|improve this answer



















  • 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 8:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424207%2fis-there-a-standardized-way-to-parse-command-line-arguments-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2















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.






share|improve this answer



















  • 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 8:28
















2















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.






share|improve this answer



















  • 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 8:28














2












2








2








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.






share|improve this answer














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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 8:47









Kamil CukKamil Cuk

11.4k1528




11.4k1528








  • 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 8:28














  • 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 8:28








1




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 8:28





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 8:28




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424207%2fis-there-a-standardized-way-to-parse-command-line-arguments-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith