-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat (facade): simple argument parser #1747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3d521c4
feat: simple argument parser
dranikpg c688e4b
chore: experiment v2
dranikpg 7d65623
chore: experiment v3
dranikpg 87075df
fix: rename arg_parser
dranikpg 71dff73
fix: fixes v2
dranikpg e4d8581
chore: add cmd_arg_parser_test
dranikpg 1a1a80c
fix: fix rebase
dranikpg 0a25ec3
fix: small fixes
dranikpg 6aa22a2
fix: split into h/cc
dranikpg 82468d1
fix: foxes
dranikpg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023, DragonflyDB authors. All rights reserved. | ||
// See LICENSE for licensing terms. | ||
// | ||
|
||
#include "facade/cmd_arg_parser.h" | ||
|
||
#include <absl/strings/ascii.h> | ||
#include <absl/strings/match.h> | ||
#include <absl/strings/numbers.h> | ||
|
||
#include "base/logging.h" | ||
#include "facade/error.h" | ||
|
||
namespace facade { | ||
|
||
CmdArgParser::CheckProxy::operator bool() const { | ||
if (idx_ >= parser_->args_.size()) | ||
return false; | ||
|
||
std::string_view arg = parser_->SafeSV(idx_); | ||
if ((!ignore_case_ && arg != tag_) || (ignore_case_ && !absl::EqualsIgnoreCase(arg, tag_))) | ||
kostasrim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
|
||
if (idx_ + expect_tail_ >= parser_->args_.size()) { | ||
parser_->Report(SHORT_OPT_TAIL, idx_); | ||
return false; | ||
} | ||
|
||
parser_->cur_i_++; | ||
|
||
if (size_t uidx = idx_ + expect_tail_ + 1; next_upper_ && uidx < parser_->args_.size()) | ||
parser_->ToUpper(uidx); | ||
|
||
return true; | ||
} | ||
|
||
template <typename T> T CmdArgParser::NextProxy::Int() { | ||
T out; | ||
if (absl::SimpleAtoi(operator std::string_view(), &out)) | ||
return out; | ||
parser_->Report(INVALID_INT, idx_); | ||
return T{0}; | ||
} | ||
|
||
template uint64_t CmdArgParser::NextProxy::Int<uint64_t>(); | ||
kostasrim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
template int64_t CmdArgParser::NextProxy::Int<int64_t>(); | ||
|
||
ErrorReply CmdArgParser::ErrorInfo::MakeReply() const { | ||
switch (type) { | ||
case INVALID_INT: | ||
return ErrorReply{kInvalidIntErr}; | ||
default: | ||
return ErrorReply{kSyntaxErr}; | ||
}; | ||
return ErrorReply{kSyntaxErr}; | ||
} | ||
|
||
CmdArgParser::~CmdArgParser() { | ||
DCHECK(!error_.has_value()) << "Parsing error occured but not checked"; | ||
} | ||
|
||
void CmdArgParser::ToUpper(size_t i) { | ||
kostasrim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (auto& c : args_[i]) | ||
c = absl::ascii_toupper(c); | ||
} | ||
|
||
} // namespace facade |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// Copyright 2023, DragonflyDB authors. All rights reserved. | ||
// See LICENSE for licensing terms. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <optional> | ||
#include <string_view> | ||
|
||
#include "facade/facade_types.h" | ||
|
||
namespace facade { | ||
|
||
// Utility class for easily parsing command options from argument lists. | ||
struct CmdArgParser { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it actually ok to keep this header only? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, probably not (noticing 2s after giving the approval) |
||
enum ErrorType { | ||
OUT_OF_BOUNDS, | ||
SHORT_OPT_TAIL, | ||
INVALID_INT, | ||
INVALID_CASES, | ||
}; | ||
|
||
struct NextProxy; | ||
|
||
template <typename T> struct CaseProxy { | ||
operator T(); | ||
|
||
CaseProxy Case(std::string_view tag, T value); | ||
|
||
private: | ||
friend struct NextProxy; | ||
|
||
CaseProxy(CmdArgParser* parser, size_t idx) : parser_{parser}, idx_{idx} { | ||
} | ||
|
||
CmdArgParser* parser_; | ||
size_t idx_; | ||
std::optional<T> value_; | ||
}; | ||
|
||
struct NextProxy { | ||
operator std::string_view() { | ||
return parser_->SafeSV(idx_); | ||
} | ||
|
||
operator std::string() { | ||
return std::string{operator std::string_view()}; | ||
} | ||
|
||
template <typename T> T Int(); | ||
|
||
// Detect value based on cases. | ||
// Returns default if the argument is not present among the cases list, | ||
// and reports an error. | ||
template <typename T> auto Case(std::string_view tag, T value) { | ||
return CaseProxy<T>{parser_, idx_}.Case(tag, value); | ||
} | ||
|
||
private: | ||
friend struct CmdArgParser; | ||
|
||
NextProxy(CmdArgParser* parser, size_t idx) : parser_{parser}, idx_{idx} { | ||
} | ||
|
||
CmdArgParser* parser_; | ||
size_t idx_; | ||
}; | ||
|
||
struct CheckProxy { | ||
explicit operator bool() const; | ||
|
||
// Expect the tag to be followed by a number of arguments. | ||
// Reports an error if the tag is matched but the condition is not met. | ||
CheckProxy& ExpectTail(size_t tail) { | ||
expect_tail_ = tail; | ||
return *this; | ||
} | ||
|
||
// Call ToUpper on the next value after the flag and its expected tail. | ||
CheckProxy& NextUpper() { | ||
next_upper_ = true; | ||
return *this; | ||
} | ||
|
||
CheckProxy& IgnoreCase() { | ||
ignore_case_ = true; | ||
return *this; | ||
} | ||
|
||
private: | ||
friend struct CmdArgParser; | ||
|
||
CheckProxy(CmdArgParser* parser, std::string_view tag, size_t idx) | ||
: parser_{parser}, tag_{tag}, idx_{idx} { | ||
} | ||
|
||
CmdArgParser* parser_; | ||
std::string_view tag_; | ||
size_t idx_; | ||
size_t expect_tail_ = 0; | ||
bool next_upper_ = false; | ||
bool ignore_case_ = false; | ||
}; | ||
|
||
struct ErrorInfo { | ||
ErrorType type; | ||
size_t index; | ||
|
||
ErrorReply MakeReply() const; | ||
}; | ||
|
||
public: | ||
CmdArgParser(CmdArgList args) : args_{args} { | ||
} | ||
|
||
// Debug asserts sure error was consumed | ||
~CmdArgParser(); | ||
|
||
// Get next value without consuming it | ||
NextProxy Peek() { | ||
return NextProxy(this, cur_i_); | ||
} | ||
|
||
// Consume next value | ||
NextProxy Next() { | ||
if (cur_i_ >= args_.size()) | ||
Report(OUT_OF_BOUNDS, cur_i_); | ||
return NextProxy{this, cur_i_++}; | ||
} | ||
kostasrim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Check if the next value if equal to a specific tag. If equal, its consumed. | ||
CheckProxy Check(std::string_view tag) { | ||
return CheckProxy(this, tag, cur_i_); | ||
} | ||
|
||
// Skip specified number of arguments | ||
CmdArgParser& Skip(size_t n) { | ||
cur_i_ += n; | ||
return *this; | ||
} | ||
|
||
// In-place convert the next argument to uppercase | ||
CmdArgParser& ToUpper() { | ||
if (cur_i_ < args_.size()) | ||
ToUpper(cur_i_); | ||
return *this; | ||
} | ||
|
||
// Return remaining arguments | ||
CmdArgList Tail() const { | ||
return args_.subspan(cur_i_); | ||
} | ||
|
||
// Return true if arguments are left and no errors occured | ||
bool HasNext() { | ||
return cur_i_ < args_.size() && !error_; | ||
} | ||
|
||
// Get optional error if occured | ||
std::optional<ErrorInfo> Error() { | ||
auto out = std::move(error_); | ||
error_.reset(); | ||
return out; | ||
dranikpg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
dranikpg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
private: | ||
std::string_view SafeSV(size_t i) const { | ||
if (i >= args_.size()) | ||
return ""; | ||
return ToSV(args_[i]); | ||
} | ||
|
||
void Report(ErrorType type, size_t idx) { | ||
if (!error_) | ||
error_ = {type, idx}; | ||
} | ||
|
||
void ToUpper(size_t i); | ||
|
||
private: | ||
size_t cur_i_ = 0; | ||
CmdArgList args_; | ||
|
||
std::optional<ErrorInfo> error_; | ||
}; | ||
|
||
template <typename T> CmdArgParser::CaseProxy<T>::operator T() { | ||
if (!value_) | ||
parser_->Report(INVALID_CASES, idx_); | ||
return value_.value_or(T{}); | ||
} | ||
|
||
template <typename T> | ||
CmdArgParser::CaseProxy<T> CmdArgParser::CaseProxy<T>::Case(std::string_view tag, T value) { | ||
std::string_view arg = parser_->SafeSV(idx_); | ||
if (arg == tag) | ||
value_ = std::move(value); | ||
return *this; | ||
} | ||
|
||
} // namespace facade |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved all function longer than 3 lines and all functions that have some sependencies (absl and our stuff)