Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 1 | // |
| 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 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 17 | #include <stdlib.h> |
| 18 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 21 | #include "absl/base/attributes.h" |
| 22 | #include "absl/base/config.h" |
| 23 | #include "absl/base/const_init.h" |
| 24 | #include "absl/base/thread_annotations.h" |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 25 | #include "absl/flags/internal/usage.h" |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 26 | #include "absl/strings/string_view.h" |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 27 | #include "absl/synchronization/mutex.h" |
| 28 | |
| 29 | namespace absl { |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 30 | ABSL_NAMESPACE_BEGIN |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 31 | namespace flags_internal { |
| 32 | namespace { |
| 33 | ABSL_CONST_INIT absl::Mutex usage_message_guard(absl::kConstInit); |
| 34 | ABSL_CONST_INIT std::string* program_usage_message |
| 35 | ABSL_GUARDED_BY(usage_message_guard) = nullptr; |
| 36 | } // namespace |
| 37 | } // namespace flags_internal |
| 38 | |
| 39 | // -------------------------------------------------------------------- |
| 40 | // Sets the "usage" message to be used by help reporting routines. |
| 41 | void SetProgramUsageMessage(absl::string_view new_usage_message) { |
| 42 | absl::MutexLock l(&flags_internal::usage_message_guard); |
| 43 | |
| 44 | if (flags_internal::program_usage_message != nullptr) { |
| 45 | ABSL_INTERNAL_LOG(FATAL, "SetProgramUsageMessage() called twice."); |
| 46 | std::exit(1); |
| 47 | } |
| 48 | |
| 49 | flags_internal::program_usage_message = new std::string(new_usage_message); |
| 50 | } |
| 51 | |
| 52 | // -------------------------------------------------------------------- |
| 53 | // Returns the usage message set by SetProgramUsageMessage(). |
| 54 | // Note: We able to return string_view here only because calling |
| 55 | // SetProgramUsageMessage twice is prohibited. |
| 56 | absl::string_view ProgramUsageMessage() { |
| 57 | absl::MutexLock l(&flags_internal::usage_message_guard); |
| 58 | |
| 59 | return flags_internal::program_usage_message != nullptr |
| 60 | ? absl::string_view(*flags_internal::program_usage_message) |
| 61 | : "Warning: SetProgramUsageMessage() never called"; |
| 62 | } |
| 63 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 64 | ABSL_NAMESPACE_END |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 65 | } // namespace absl |