LLDB  mainline
OptionParser.cpp
Go to the documentation of this file.
1 //===-- source/Host/common/OptionParser.cpp ---------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "lldb/Host/HostGetOpt.h"
12 
13 #include <vector>
14 
15 using namespace lldb_private;
16 
17 void OptionParser::Prepare(std::unique_lock<std::mutex> &lock) {
18  static std::mutex g_mutex;
19  lock = std::unique_lock<std::mutex>(g_mutex);
20 #ifdef __GLIBC__
21  optind = 0;
22 #else
23  optreset = 1;
24  optind = 1;
25 #endif
26 }
27 
28 void OptionParser::EnableError(bool error) { opterr = error ? 1 : 0; }
29 
30 int OptionParser::Parse(int argc, char *const argv[], llvm::StringRef optstring,
31  const Option *longopts, int *longindex) {
32  std::vector<option> opts;
33  while (longopts->definition != nullptr) {
34  option opt;
35  opt.flag = longopts->flag;
36  opt.val = longopts->val;
37  opt.name = longopts->definition->long_option;
38  opt.has_arg = longopts->definition->option_has_arg;
39  opts.push_back(opt);
40  ++longopts;
41  }
42  opts.push_back(option());
43  std::string opt_cstr = optstring;
44  return getopt_long_only(argc, argv, opt_cstr.c_str(), &opts[0], longindex);
45 }
46 
47 char *OptionParser::GetOptionArgument() { return optarg; }
48 
49 int OptionParser::GetOptionIndex() { return optind; }
50 
51 int OptionParser::GetOptionErrorCause() { return optopt; }
52 
53 std::string OptionParser::GetShortOptionString(struct option *long_options) {
54  std::string s;
55  int i = 0;
56  bool done = false;
57  while (!done) {
58  if (long_options[i].name == 0 && long_options[i].has_arg == 0 &&
59  long_options[i].flag == 0 && long_options[i].val == 0) {
60  done = true;
61  } else {
62  if (long_options[i].flag == NULL && isalpha(long_options[i].val)) {
63  s.append(1, (char)long_options[i].val);
64  switch (long_options[i].has_arg) {
65  default:
66  case no_argument:
67  break;
68 
69  case optional_argument:
70  s.append(2, ':');
71  break;
72  case required_argument:
73  s.append(1, ':');
74  break;
75  }
76  }
77  ++i;
78  }
79  }
80  return s;
81 }
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
static int GetOptionErrorCause()
static void EnableError(bool error)
static void Prepare(std::unique_lock< std::mutex > &lock)
static char * GetOptionArgument()
static std::string GetShortOptionString(struct option *long_options)
const OptionDefinition * definition
Definition: OptionParser.h:25
static int Parse(int argc, char *const argv[], llvm::StringRef optstring, const Option *longopts, int *longindex)