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
13
14#include <vector>
15
16using namespace lldb_private;
17
18void 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
29void OptionParser::EnableError(bool error) { opterr = error ? 1 : 0; }
30
31int 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
50char *OptionParser::GetOptionArgument() { return optarg; }
51
52int OptionParser::GetOptionIndex() { return optind; }
53
54int OptionParser::GetOptionErrorCause() { return optopt; }
55
56std::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}
static llvm::raw_ostream & error(Stream &strm)
static void Prepare(std::unique_lock< std::mutex > &lock)
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.
static std::string GetShortOptionString(struct option *long_options)
static char * GetOptionArgument()
static void EnableError(bool error)
static int GetOptionErrorCause()
A class that represents a running process on the host machine.
const char * long_option
Full name for this option.
int option_has_arg
no_argument, required_argument or optional_argument
const OptionDefinition * definition
Definition: OptionParser.h:26