Skip to content
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

Add const& when needed to avoid unnecessary copies #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ As a result libargparse also supports custom conversions, allowing user-defined
If we wanted to modify the above example so the '--bar' argument accepted the strings 'on' and 'off' (instead of the default 'true' and 'false') we would define a custom class as follows:
```cpp
struct OnOff {
ConvertedValue<bool> from_str(std::string str) {
ConvertedValue<bool> from_str(const std::string& str) {
ConvertedValue<bool> converted_value;

if (str == "on") converted_value.set_value(true);
Expand Down
4 changes: 2 additions & 2 deletions argparse_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Args {
};

struct OnOff {
ConvertedValue<bool> from_str(std::string str) {
ConvertedValue<bool> from_str(const std::string& str) {
ConvertedValue<bool> converted_value;

if (str == "on") converted_value.set_value(true);
Expand All @@ -37,7 +37,7 @@ struct OnOff {
};

struct ZeroOneRange {
ConvertedValue<float> from_str(std::string str) {
ConvertedValue<float> from_str(const std::string& str) {
float value;
std::stringstream ss(str);
ss >> value;
Expand Down
10 changes: 5 additions & 5 deletions argparse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ struct Args {
ArgValue<std::vector<float>> zero_or_more;
};

bool expect_pass(argparse::ArgumentParser& parser, std::vector<std::string> cmd_line);
bool expect_fail(argparse::ArgumentParser& parser, std::vector<std::string> cmd_line);
bool expect_pass(argparse::ArgumentParser& parser, const std::vector<std::string>& cmd_line);
bool expect_fail(argparse::ArgumentParser& parser, const std::vector<std::string>& cmd_line);

struct OnOff {
ConvertedValue<bool> from_str(std::string str) {
ConvertedValue<bool> from_str(const std::string& str) {
ConvertedValue<bool> converted_value;

if (str == "on") converted_value.set_value(true);
Expand Down Expand Up @@ -549,7 +549,7 @@ int main(
#endif
}

bool expect_pass(argparse::ArgumentParser& parser, std::vector<std::string> cmd_line) {
bool expect_pass(argparse::ArgumentParser& parser, const std::vector<std::string>& cmd_line) {
try {
parser.parse_args_throw(cmd_line);
} catch(const argparse::ArgParseHelp&) {
Expand All @@ -566,7 +566,7 @@ bool expect_pass(argparse::ArgumentParser& parser, std::vector<std::string> cmd_
return true;
}

bool expect_fail(argparse::ArgumentParser& parser, std::vector<std::string> cmd_line) {
bool expect_fail(argparse::ArgumentParser& parser, const std::vector<std::string>& cmd_line) {
try {
parser.parse_args_throw(cmd_line);
} catch(const argparse::ArgParseError& err) {
Expand Down
28 changes: 14 additions & 14 deletions src/argparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace argparse {
* ArgumentParser
*/

ArgumentParser::ArgumentParser(std::string prog_name, std::string description_str, std::ostream& os)
ArgumentParser::ArgumentParser(const std::string& prog_name, const std::string& description_str, std::ostream& os)
: description_(description_str)
, formatter_(new DefaultFormatter())
, os_(os)
Expand All @@ -25,7 +25,7 @@ namespace argparse {
argument_groups_.push_back(ArgumentGroup("arguments"));
}

ArgumentParser& ArgumentParser::prog(std::string prog_name, bool basename_only) {
ArgumentParser& ArgumentParser::prog(const std::string& prog_name, bool basename_only) {
if (basename_only) {
prog_ = basename(prog_name);
} else {
Expand All @@ -34,17 +34,17 @@ namespace argparse {
return *this;
}

ArgumentParser& ArgumentParser::version(std::string version_str) {
ArgumentParser& ArgumentParser::version(const std::string& version_str) {
version_ = version_str;
return *this;
}

ArgumentParser& ArgumentParser::epilog(std::string epilog_str) {
ArgumentParser& ArgumentParser::epilog(const std::string& epilog_str) {
epilog_ = epilog_str;
return *this;
}

ArgumentGroup& ArgumentParser::add_argument_group(std::string description_str) {
ArgumentGroup& ArgumentParser::add_argument_group(const std::string& description_str) {
argument_groups_.push_back(ArgumentGroup(description_str));
return argument_groups_[argument_groups_.size() - 1];
}
Expand Down Expand Up @@ -78,7 +78,7 @@ namespace argparse {
parse_args_throw(arg_strs);
}

void ArgumentParser::parse_args_throw(std::vector<std::string> arg_strs) {
void ArgumentParser::parse_args_throw(const std::vector<std::string>& arg_strs) {
add_help_option_if_unspecified();

//Reset all the defaults
Expand Down Expand Up @@ -373,7 +373,7 @@ namespace argparse {
}
}

ArgumentParser::ShortArgInfo ArgumentParser::no_space_short_arg(std::string str, const std::map<std::string, std::shared_ptr<Argument>>& str_to_option_arg) const {
ArgumentParser::ShortArgInfo ArgumentParser::no_space_short_arg(const std::string& str, const std::map<std::string, std::shared_ptr<Argument>>& str_to_option_arg) const {

ShortArgInfo short_arg_info;
for(const auto& kv : str_to_option_arg) {
Expand Down Expand Up @@ -409,11 +409,11 @@ namespace argparse {
/*
* ArgumentGroup
*/
ArgumentGroup::ArgumentGroup(std::string name_str)
ArgumentGroup::ArgumentGroup(const std::string& name_str)
: name_(name_str)
{}

ArgumentGroup& ArgumentGroup::epilog(std::string str) {
ArgumentGroup& ArgumentGroup::epilog(const std::string& str) {
epilog_ = str;
return *this;
}
Expand All @@ -424,7 +424,7 @@ namespace argparse {
/*
* Argument
*/
Argument::Argument(std::string long_opt, std::string short_opt)
Argument::Argument(const std::string& long_opt, const std::string& short_opt)
: long_opt_(long_opt)
, short_opt_(short_opt) {

Expand All @@ -444,7 +444,7 @@ namespace argparse {
metavar_ = toupper(dashes_name[1]);
}

Argument& Argument::help(std::string help_str) {
Argument& Argument::help(const std::string& help_str) {
help_ = help_str;
return *this;
}
Expand Down Expand Up @@ -475,12 +475,12 @@ namespace argparse {
return *this;
}

Argument& Argument::metavar(std::string metavar_str) {
Argument& Argument::metavar(const std::string& metavar_str) {
metavar_ = metavar_str;
return *this;
}

Argument& Argument::choices(std::vector<std::string> choice_values) {
Argument& Argument::choices(const std::vector<std::string>& choice_values) {
choices_ = choice_values;
return *this;
}
Expand Down Expand Up @@ -535,7 +535,7 @@ namespace argparse {
return default_value(std::vector<std::string>(values.begin(), values.end()));
}

Argument& Argument::group_name(std::string grp) {
Argument& Argument::group_name(const std::string& grp) {
group_name_ = grp;
return *this;
}
Expand Down
Loading