1 //===-- source/Host/common/OptionParser.cpp ---------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Host/OptionParser.h" 11 12 #if (!defined( _MSC_VER ) && defined( _WIN32 )) 13 #define _BSD_SOURCE // Required so that getopt.h defines optreset 14 #endif 15 #include "lldb/Host/HostGetOpt.h" 16 17 using namespace lldb_private; 18 19 void 20 OptionParser::Prepare() 21 { 22 #ifdef __GLIBC__ 23 optind = 0; 24 #else 25 optreset = 1; 26 optind = 1; 27 #endif 28 } 29 30 void 31 OptionParser::EnableError(bool error) 32 { 33 opterr = error ? 1 : 0; 34 } 35 36 int 37 OptionParser::Parse (int argc, 38 char * const argv [], 39 const char *optstring, 40 const Option *longopts, 41 int *longindex) 42 { 43 return getopt_long_only(argc, argv, optstring, (const option*)longopts, longindex); 44 } 45 46 char* 47 OptionParser::GetOptionArgument() 48 { 49 return optarg; 50 } 51 52 int 53 OptionParser::GetOptionIndex() 54 { 55 return optind; 56 } 57 58 int 59 OptionParser::GetOptionErrorCause() 60 { 61 return optopt; 62 } 63 64 std::string 65 OptionParser::GetShortOptionString(struct option *long_options) 66 { 67 std::string s; 68 int i=0; 69 bool done = false; 70 while (!done) 71 { 72 if (long_options[i].name == 0 && 73 long_options[i].has_arg == 0 && 74 long_options[i].flag == 0 && 75 long_options[i].val == 0) 76 { 77 done = true; 78 } 79 else 80 { 81 if (long_options[i].flag == NULL && 82 isalpha(long_options[i].val)) 83 { 84 s.append(1, (char)long_options[i].val); 85 switch (long_options[i].has_arg) 86 { 87 default: 88 case no_argument: 89 break; 90 91 case optional_argument: 92 s.append(2, ':'); 93 break; 94 case required_argument: 95 s.append(1, ':'); 96 break; 97 } 98 } 99 ++i; 100 } 101 } 102 return s; 103 } 104