Skip to content
Snippets Groups Projects
Commit 87d44b94 authored by Michael Davis's avatar Michael Davis
Browse files

[cta_admin] Validates command line options

parent ae0d3a80
Branches
Tags
No related merge requests found
......@@ -79,7 +79,12 @@ CtaAdminCmd::CtaAdminCmd(int argc, const char *const *const argv) :
void CtaAdminCmd::send() const
{
// Validate the Protocol Buffer
validateOptions(m_request.admincmd());
try {
validateCommand(m_request.admincmd());
} catch(std::runtime_error &ex) {
throwUsage(ex.what());
}
// Send the Protocol Buffer
XrdSsiPb::OutputJsonString(std::cout, &m_request.admincmd());
......
......@@ -79,5 +79,38 @@ std::string CmdHelp::help() const {
return help;
}
void validateCommand(const cta::admin::AdminCmd &admincmd)
{
// Get the option list for this command
auto option_list_it = cmdOptions.find(cmd_key_t{ admincmd.cmd(), admincmd.subcmd() });
if(option_list_it == cmdOptions.end()) {
throw std::runtime_error("Invalid command/subcommand");
}
// Verify that the compulsory options exist
for(auto option_it = option_list_it->second.begin(); option_it != option_list_it->second.end(); ++option_it)
{
if(!option_it->is_optional())
{
switch(option_it->get_type())
{
case Option::OPT_CMD:
case Option::OPT_STR:
admincmd.option_str()
case Option::OPT_FLAG:
case Option::OPT_BOOL:
admincmd.option_bool()
case Option::OPT_UINT:
admincmd.option_uint64()
}
}
}
}
}} // namespace cta::admin
......@@ -70,15 +70,20 @@ public:
*/
option_t get_type() const { return m_type; }
/*!
* Return the number of parameters expected after this option
*/
int num_params() const { return m_type == OPT_CMD || m_type == OPT_FLAG ? 0 : 1; }
/*!
* Return the key for this option
*/
const std::string &get_key() const { return m_lookup_key; }
/*!
* Return the number of parameters expected after this option
* Return whether the option is optional
*/
int num_params() const { return m_type == OPT_CMD || m_type == OPT_FLAG ? 0 : 1; }
bool is_optional() const { return m_is_optional; }
/*!
* Return per-option help string
......@@ -497,7 +502,9 @@ const std::map<cmd_key_t, cmd_val_t> cmdOptions = {
/*!
* Validate that all required command line options are present
*
* Throws a std::runtime_error if the command is invalid
*/
void validateOptions(const cta::admin::AdminCmd &admin_cmd);
void validateCommand(const cta::admin::AdminCmd &admincmd);
}} // namespace cta::admin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment