diff --git a/rvs/include/rvscli.h b/rvs/include/rvscli.h index d29e3f35..b06f9766 100644 --- a/rvs/include/rvscli.h +++ b/rvs/include/rvscli.h @@ -1,6 +1,6 @@ /******************************************************************************** * - * Copyright (c) 2018-2022 Advanced Micro Devices, Inc. All rights reserved. + * Copyright (c) 2018-2024 Advanced Micro Devices, Inc. All rights reserved. * * MIT LICENSE: * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -60,14 +60,15 @@ class cli { * @brief Possible continuation types * * Defines possible type for the next token: - * |value |meaning - * |---------|------------------------- - * |eof |end of input - * |value |value expected (must be different then command strings defined throuth grammar) - * |command |command string as defined in grammar + * |value |meaning + * |---------------|------------------------- + * |eof |end of input + * |value |value always expected (must be different then command strings defined through grammar) + * |optionalvalue |value not always expected (must be different then command strings defined through grammar) + * |command |command string as defined in grammar * */ - typedef enum {eof, value, command} econtext; + typedef enum {eof, value, optionalvalue, command} econtext; /** * @class optbase @@ -100,6 +101,7 @@ class cli { const char* get_token(); bool try_command(const std::string& token); bool try_value(const std::string& token); + bool try_optionalvalue(const std::string& token); bool emit_option(void); void init_grammar(void); void extract_path(void); diff --git a/rvs/src/rvscli.cpp b/rvs/src/rvscli.cpp index e2bea0b0..c76cd41d 100644 --- a/rvs/src/rvscli.cpp +++ b/rvs/src/rvscli.cpp @@ -151,7 +151,7 @@ void rvs::cli::init_grammar() { grammar.insert(gpair("-i", sp)); grammar.insert(gpair("--indexes", sp)); - sp = std::make_shared("-j", command); + sp = std::make_shared("-j", command, optionalvalue); grammar.insert(gpair("-j", sp)); grammar.insert(gpair("--json", sp)); @@ -224,6 +224,10 @@ int rvs::cli::parse(int Argc, char** Argv) { } break; + case econtext::optionalvalue: + token_done = try_optionalvalue(token); + break; + case econtext::eof: if (token == "") { emit_option(); @@ -344,3 +348,30 @@ bool rvs::cli::try_value(const std::string& token) { return true; } +/** + * @brief Try interpreting given token as a value if present following previous command line option. + * + * If successful, stores current token in a buffer as value + * + * @param token token being processed + * @return true if successful, false otherwise + * + */ +bool rvs::cli::try_optionalvalue(const std::string& token) { + + if (token == "") + return true; + + // should not be one of command line options + auto it = grammar.find(token); + if (it != grammar.end()) + return false; + + // token is value for previous command + current_value = token; + + // emit previous option-value pair: + emit_option(); + + return true; +}