2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-10-13 22:06:48 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
//
|
|
|
|
// Flag validation help functions.
|
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#ifndef PACKAGER_APP_VALIDATE_FLAG_H_
|
|
|
|
#define PACKAGER_APP_VALIDATE_FLAG_H_
|
2014-10-15 21:56:12 +00:00
|
|
|
|
2014-10-13 22:06:48 +00:00
|
|
|
#include <string>
|
|
|
|
|
2023-09-09 00:44:17 +00:00
|
|
|
#include <absl/strings/str_format.h>
|
2017-06-14 23:18:16 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-10-13 22:06:48 +00:00
|
|
|
|
2017-06-14 23:18:16 +00:00
|
|
|
/// Format and print error message.
|
|
|
|
/// @param error_message specifies the error message.
|
|
|
|
void PrintError(const std::string& error_message);
|
|
|
|
|
2017-09-20 22:49:00 +00:00
|
|
|
/// Format and print warning message.
|
|
|
|
/// @param warning_message specifies the warning message.
|
|
|
|
void PrintWarning(const std::string& warning_message);
|
|
|
|
|
2014-10-13 22:06:48 +00:00
|
|
|
/// Validate a flag against the given condition.
|
|
|
|
/// @param flag_name is the name of the flag.
|
|
|
|
/// @param flag_value is the value of the flag.
|
|
|
|
/// @param condition,optional determines how the flag should be validated. If
|
|
|
|
/// condition is true and optional is false, then this flag is required
|
|
|
|
// and cannot be empty; If condition is false, then this flag should
|
|
|
|
// not be set.
|
|
|
|
/// @param label specifies the label associated with the condition. It is used
|
|
|
|
/// to generate the error message on validation failure.
|
|
|
|
/// @return true on success, false otherwise.
|
2017-06-14 23:18:16 +00:00
|
|
|
template <class FlagType>
|
2014-10-13 22:06:48 +00:00
|
|
|
bool ValidateFlag(const char* flag_name,
|
2017-06-14 23:18:16 +00:00
|
|
|
const FlagType& flag_value,
|
2014-10-13 22:06:48 +00:00
|
|
|
bool condition,
|
|
|
|
bool optional,
|
2017-06-14 23:18:16 +00:00
|
|
|
const char* label) {
|
|
|
|
if (flag_value.empty()) {
|
|
|
|
if (!optional && condition) {
|
2023-09-09 00:44:17 +00:00
|
|
|
PrintError(absl::StrFormat("--%s is required if %s.", flag_name, label));
|
2017-06-14 23:18:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!condition) {
|
2023-09-09 00:44:17 +00:00
|
|
|
PrintError(absl::StrFormat("--%s should be specified only if %s.",
|
|
|
|
flag_name, label));
|
2017-06-14 23:18:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-13 22:06:48 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2014-10-15 21:56:12 +00:00
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#endif // PACKAGER_APP_VALIDATE_FLAG_H_
|