LLDB  mainline
OptionParser.cpp
Go to the documentation of this file.
1 //===-- source/Host/common/OptionParser.cpp -------------------------------===//
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"
13 
14 #include <vector>
15 
16 using namespace lldb_private;
17 
18 void OptionParser::Prepare(std::unique_lock<std::mutex> &lock) {
19  static std::mutex g_mutex;
20  lock = std::unique_lock<std::mutex>(g_mutex);
21 #ifdef __GLIBC__
22  optind = 0;
23 #else
24  optreset = 1;
25  optind = 1;
26 #endif
27 }
28 
29 void OptionParser::EnableError(bool error) { opterr = error ? 1 : 0; }
30 
31 int OptionParser::Parse(llvm::MutableArrayRef<char *> argv,
32  llvm::StringRef optstring, const Option *longopts,
33  int *longindex) {
34  std::vector<option> opts;
35  while (longopts->definition != nullptr) {
36  option opt;
37  opt.flag = longopts->flag;
38  opt.val = longopts->val;
39  opt.name = longopts->definition->long_option;
40  opt.has_arg = longopts->definition->option_has_arg;
41  opts.push_back(opt);
42  ++longopts;
43  }
44  opts.push_back(option());
45  std::string opt_cstr = std::string(optstring);
46  return getopt_long_only(argv.size() - 1, argv.data(), opt_cstr.c_str(),
47  &opts[0], longindex);
48 }
49 
50 char *OptionParser::GetOptionArgument() { return optarg; }
51 
52 int OptionParser::GetOptionIndex() { return optind; }
53 
54 int OptionParser::GetOptionErrorCause() { return optopt; }
55 
56 std::string OptionParser::GetShortOptionString(struct option *long_options) {
57  std::string s;
58  int i = 0;
59  bool done = false;
60  while (!done) {
61  if (long_options[i].name == nullptr && long_options[i].has_arg == 0 &&
62  long_options[i].flag == nullptr && long_options[i].val == 0) {
63  done = true;
64  } else {
65  if (long_options[i].flag == nullptr && isalpha(long_options[i].val)) {
66  s.append(1, (char)long_options[i].val);
67  switch (long_options[i].has_arg) {
68  default:
69  case no_argument:
70  break;
71 
72  case optional_argument:
73  s.append(2, ':');
74  break;
75  case required_argument:
76  s.append(1, ':');
77  break;
78  }
79  }
80  ++i;
81  }
82  }
83  return s;
84 }
lldb-private-types.h
lldb_private::OptionParser::Parse
static int Parse(llvm::MutableArrayRef< char * > argv, llvm::StringRef optstring, const Option *longopts, int *longindex)
Argv must be an argument vector "as passed to main", i.e.
Definition: OptionParser.cpp:31
lldb_private::OptionParser::EnableError
static void EnableError(bool error)
Definition: OptionParser.cpp:29
OptionDefinition.h
lldb_private::Option
Definition: OptionParser.h:24
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
HostGetOpt.h
lldb_private::OptionParser::GetOptionIndex
static int GetOptionIndex()
Definition: OptionParser.cpp:52
lldb_private::OptionDefinition::long_option
const char * long_option
Full name for this option.
Definition: OptionDefinition.h:27
lldb_private::OptionDefinition::option_has_arg
int option_has_arg
no_argument, required_argument or optional_argument
Definition: OptionDefinition.h:34
lldb_private::OptionParser::GetOptionArgument
static char * GetOptionArgument()
Definition: OptionParser.cpp:50
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::Option::val
int val
Definition: OptionParser.h:30
OptionParser.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Option::definition
const OptionDefinition * definition
Definition: OptionParser.h:26
lldb_private::OptionParser::GetShortOptionString
static std::string GetShortOptionString(struct option *long_options)
Definition: OptionParser.cpp:56
lldb_private::Option::flag
int * flag
Definition: OptionParser.h:28
lldb_private::OptionParser::GetOptionErrorCause
static int GetOptionErrorCause()
Definition: OptionParser.cpp:54
lldb_private::OptionParser::Prepare
static void Prepare(std::unique_lock< std::mutex > &lock)
Definition: OptionParser.cpp:18