Preface:
ForOptional ParameterYou must use either of the following methods to indicate the value-wvalue or -- Who = value instead of -- who value,
ForRequired Parameter-Lvalue or -- love value can be used.Or -- love = value,
This is not a bug.
// ================================================ ============================================== // Name: testopt. cpp // Author: yangyh // Version: // Copyright: Your copyright notice // Description: Hello world in C ++, ANSI-style // ================================================ ============================================== # Include <iostream> # include <getopt. h> using namespace STD; Int Version; Struct Option longopts [] = {{" Version ", No_argument, & version, 'V '},{" Name ", No_argument, null, 'n '},{" Love ", Required_argument, null, 'L '},{" Who ", Optional_argument, null, 'w'}, {0, 0, 0 }}; Int Main (Int Argc, Char * Argv []) { Int C; While (C = getopt_long (argc, argv ," Vl: W :: ", Longopts, null ))! =-1 ){ Switch (C ){ Case 'L ': Printf (" Love = % s! \ N ", Optarg ); Break ; Case 0: // Printf (" Getopt_long () variable: version = % C \ n ", Version ); Break ; Case 'V ': Printf (" Version .. \ n "); Break ;
Case'W ':Printf("WHO = % s \ n", Optarg );Break;}}Return0 ;}