2014-10-13 22:06:48 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
#ifndef APP_VALIDATE_FLAG_H_
|
|
|
|
#define APP_VALIDATE_FLAG_H_
|
|
|
|
|
2014-10-13 22:06:48 +00:00
|
|
|
#include <string>
|
|
|
|
|
2017-06-14 23:18:16 +00:00
|
|
|
#include "packager/base/strings/stringprintf.h"
|
|
|
|
|
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);
|
|
|
|
|
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) {
|
|
|
|
PrintError(
|
|
|
|
base::StringPrintf("--%s is required if %s.", flag_name, label));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!condition) {
|
|
|
|
PrintError(base::StringPrintf(
|
|
|
|
"--%s should be specified only if %s.", flag_name, label));
|
|
|
|
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
|
|
|
|
|
|
|
#endif // APP_VALIDATE_FLAG_H_
|