Skip to content

Commit 0ae46b7

Browse files
committed
expand multiple letters starting with '-' into individual single letter arguments
1 parent 7a8c7ee commit 0ae46b7

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

src/cpp/fs/ArgumentParser.cpp

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void register_index(T& index, string v, string help, bool required)
7373
string ArgumentParser::get_args()
7474
{
7575
std::string args{arguments_.at(0)};
76-
for (auto i = 1; i < arguments_.size(); ++i)
76+
for (size_t i = 1; i < arguments_.size(); ++i)
7777
{
7878
args.append(" ");
7979
args.append(arguments_.at(i));
@@ -136,9 +136,11 @@ string ArgumentParser::get_arg() noexcept
136136
{
137137
// check if we don't have any more arguments
138138
fs::logging::check_fatal(
139-
cur_arg_ + 1 >= arguments_.size(), "Missing argument to --%s", arguments_.at(cur_arg_).c_str()
139+
cur_arg_ + 1 >= args_expanded().size(),
140+
"Missing argument to --%s",
141+
args_expanded().at(cur_arg_).c_str()
140142
);
141-
return arguments_.at(++cur_arg_);
143+
return args_expanded().at(++cur_arg_);
142144
}
143145
size_t parse_size_t()
144146
{
@@ -206,9 +208,10 @@ ArgumentParser::ArgumentParser(
206208
}
207209
void ArgumentParser::parse_args()
208210
{
209-
while (cur_arg_ < arguments_.size())
211+
auto& args = args_expanded();
212+
while (cur_arg_ < args.size())
210213
{
211-
const string arg = arguments_.at(cur_arg_);
214+
const string arg = args.at(cur_arg_);
212215
bool is_positional = !arg.starts_with("-");
213216
if (!is_positional)
214217
{
@@ -225,7 +228,7 @@ void ArgumentParser::parse_args()
225228
// cur_arg_ would be incremented while trying to parse at this point, so -1 is 'arg'
226229
printf(
227230
"\n'%s' is not a valid value for argument %s\n\n",
228-
arguments_.at(cur_arg_).c_str(),
231+
args.at(cur_arg_).c_str(),
229232
arg.c_str()
230233
);
231234
show_usage_and_exit();
@@ -493,9 +496,55 @@ FwiWeather MainArgumentParser::get_yesterday_weather() const
493496
dc
494497
};
495498
}
496-
string ArgumentParser::cur_arg() { return arguments_.at(cur_arg_); };
499+
string ArgumentParser::cur_arg() { return args_expanded().at(cur_arg_); };
497500
bool parse_flag(bool not_inverse)
498501
{
499502
return parse_once<bool>([not_inverse] { return not_inverse; });
500503
}
504+
vector<string>& ArgumentParser::args_expanded()
505+
{
506+
// if empty then not parsed, or parsing is fast since no arguments
507+
if (arguments_expanded_.empty())
508+
{
509+
arguments_expanded_ = [&]() {
510+
vector<std::string> args{};
511+
for (const auto& s : arguments_)
512+
{
513+
if (s.starts_with("-") && !s.starts_with("--"))
514+
{
515+
// break anything starting with just one - into individual letters
516+
for (size_t i = 1; i < s.length(); ++i)
517+
{
518+
const string arg = string("-") + s.at(i);
519+
// if this isn't a flag then don't expand it
520+
if (PARSE_FCT.find(arg) != PARSE_FCT.end())
521+
{
522+
args.emplace_back(arg);
523+
}
524+
else if (1 == i)
525+
{
526+
// if this is just the start of a non-flag then leave it alone
527+
args.emplace_back(s);
528+
break;
529+
}
530+
else
531+
{
532+
return logging::fatal<vector<string>>(
533+
"Invalid argument %s found as part of combined flag argument %s",
534+
arg.c_str(),
535+
s.c_str()
536+
);
537+
}
538+
}
539+
}
540+
else
541+
{
542+
args.emplace_back(s);
543+
}
544+
}
545+
return args;
546+
}();
547+
}
548+
return arguments_expanded_;
549+
}
501550
}

src/cpp/fs/ArgumentParser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ class ArgumentParser
7979
size_t cur_arg_{0};
8080
size_t skipped_args_{0};
8181
vector<string> arguments_{};
82+
vector<string> arguments_expanded_{};
8283
string binary_directory_{};
8384
string binary_name_{};
85+
vector<string>& args_expanded();
8486
};
8587
enum MODE
8688
{

0 commit comments

Comments
 (0)