Bo2SS

Bo2SS

0 Course Introduction and Command Line Parsing Functions

  • Image

[How to implement command options? You can think of the input parameters argc and argv in the main function]

Course Content#

getopt Function#

[Command Line Parsing Function]

  • <unistd.h> → Unix Standard
  • ⭐Function Prototype
    • int getopt(int argc, char * const argv[], const char *optstring);
    • Guess
      • Return value int
        • ① 0: success; -1: failure
        • ② ≥0: success; <0: failure
      • Parameters argc, argv: similar to the parameters in the main function
      • Parameter optstring: option string
    • Check the man page: man 3 getopt
      • The return value is a character [option] or -1
        • The external variable optind and the static variable nextchar will be updated upon return
        • If there are no more options, -1 is returned and optind moves to the first element of argv [not an option]
      • argc, argv
        • Passed directly from the main function arguments
      • optstring
        • Meaning: a string composed of all valid optional characters
        • Single character represents an option
        • If a single character is followed by a colon
          • One colon means that the option must be followed by an argument value
            • The argument is immediately after the option or separated by a space
            • [Similar to the existence of option arguments in commands]
          • Two colons mean that the argument after the option is optional
            • If there is an argument, it must follow the option and cannot be separated by a space
          • The pointer to the argument is assigned to optarg [For two colons, it defaults to 0←null]
        • Example
          • Image
          • x, y, z represent three options
      • Repeatedly calling getopt() can read multiple options, such as -al -h
      • Some global variables
        • Image
        • optarg: When there is a colon, the option argument can be extracted
        • optind
          • Meaning: the index of the next element [character] in the argv array
          • When the caller resets it to 1, argv can be rescanned
          • [PS] External variable; like a pointer
        • Can be used directly [no need to declare], because these variables have been defined in the [referenced header file]
          • Concept of external variables

Code Demonstration#

getopt#

  • Image
  • Learn to use flags and switches
  • Meaning of the colon: required/optional option argument
  • Pay attention to the global variables related to getopt, they can be used directly
  • exit() can use a non-zero value to indicate abnormal exit
  • Program result
    • Image
    • For reference

Additional Knowledge#

Points for Consideration#

Tips#

  • Homework: Implement the ls -al command in C language
    • Learn to use flowcharts to clarify your thoughts, recommended ProcessOn - an online flowchart drawing website
      • Image
      • A simple flowchart from the ship
  • When learning commands, don't focus too much on variations
  • In vim, enter to view the structured view of the source code
  • Common operations of ctags: ctrl + ] to jump to the definition, ctrl + o to return

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.