Many terminal programs like the well-known GCC have command options. For GCC, for example, -w, but there are others. How do executable files read these options? Just as a string, and then parsit? Or does the shell have an API for reading options like -option?
1 answer
Everything is very simple, at first the command shell takes the text entered by the user and runs it through its parser, for example, for glob() or de-escaping characters. It splits into tokens (by spaces, considering those specified) and executes the exec[lv][p[e]]() system call before doing fork() . Accordingly, one of the parameters exec[lv][p[e]]() passed to the array char *argv[] and its length int argc . Within the invoked command, this array is parsed either with the help of standard library calls of the type getopt() , getopt_long() , either independently or in a mixed version.
For all these calls, there are man help man in sections 2 and 3 for system calls and library functions, respectively.
- Thank! That is, the program language should have a function like getopt ()? And which man pages did you watch? glob and fork? - StrangeNode
- Not in the language, but in the text of the program should be a parser of these, either using library functions, or without. Manual pages at the time looked all mentioned, I do not remember the details. The answer was written from the head. - 0andriy