Skip to content

Commit

Permalink
Support command-line options with optional value
Browse files Browse the repository at this point in the history
  • Loading branch information
jkottiku committed Dec 4, 2024
1 parent c1820e9 commit 72c5f21
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
16 changes: 9 additions & 7 deletions rvs/include/rvscli.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion rvs/src/rvscli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void rvs::cli::init_grammar() {
grammar.insert(gpair("-i", sp));
grammar.insert(gpair("--indexes", sp));

sp = std::make_shared<optbase>("-j", command);
sp = std::make_shared<optbase>("-j", command, optionalvalue);
grammar.insert(gpair("-j", sp));
grammar.insert(gpair("--json", sp));

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

0 comments on commit 72c5f21

Please sign in to comment.