blob: dff7a3dab39cffa561f15df5011e191efaba5f3b [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001//
2// Copyright 2019 The Abseil Authors.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15#include "absl/flags/usage.h"
16
17#include <string>
18
19#include "absl/flags/internal/usage.h"
20#include "absl/synchronization/mutex.h"
21
22namespace absl {
23namespace flags_internal {
24namespace {
25ABSL_CONST_INIT absl::Mutex usage_message_guard(absl::kConstInit);
26ABSL_CONST_INIT std::string* program_usage_message
27 ABSL_GUARDED_BY(usage_message_guard) = nullptr;
28} // namespace
29} // namespace flags_internal
30
31// --------------------------------------------------------------------
32// Sets the "usage" message to be used by help reporting routines.
33void SetProgramUsageMessage(absl::string_view new_usage_message) {
34 absl::MutexLock l(&flags_internal::usage_message_guard);
35
36 if (flags_internal::program_usage_message != nullptr) {
37 ABSL_INTERNAL_LOG(FATAL, "SetProgramUsageMessage() called twice.");
38 std::exit(1);
39 }
40
41 flags_internal::program_usage_message = new std::string(new_usage_message);
42}
43
44// --------------------------------------------------------------------
45// Returns the usage message set by SetProgramUsageMessage().
46// Note: We able to return string_view here only because calling
47// SetProgramUsageMessage twice is prohibited.
48absl::string_view ProgramUsageMessage() {
49 absl::MutexLock l(&flags_internal::usage_message_guard);
50
51 return flags_internal::program_usage_message != nullptr
52 ? absl::string_view(*flags_internal::program_usage_message)
53 : "Warning: SetProgramUsageMessage() never called";
54}
55
56} // namespace absl