Skip to content

Conversation

tnixeu
Copy link
Contributor

@tnixeu tnixeu commented Jun 27, 2025

I am starting to use CLI11 in a project and I came across an inconvenience in code like this

const std::string description(std::format("description"));
app.add_flag("--flag", description);

As far as I can tell. I cannot directly use the description here. The reason seems to be to distinguish between assignment and description. So only const std::string or const char* and so on can be used here. But a const std::string && can not be used.

With the changes in the PR the code can also be written like this:

app.add_flag("--flag", std::format("description"));

The add_flag function can then be used with a const string or an rvalue reference to a string.

A bit OT: I was a bit confused by this part of the existing comment if a variable string is passed that variable will be assigned the results from the flag I think this might be wrong, but I am not sure.

tnixeu and others added 2 commits June 27, 2025 14:11
Allow rvalue references to be used with `add_flag` without assignment.
Copy link

codecov bot commented Jun 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e4ee3af) to head (9c341f0).
Report is 105 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##              main     #1173     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files           17        17             
  Lines         4546      5166    +620     
  Branches         0      1061   +1061     
===========================================
+ Hits          4546      5166    +620     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@phlptp
Copy link
Collaborator

phlptp commented Jun 27, 2025

I was a bit confused by this part of the existing comment if a variable string is passed that variable will be assigned the results from the flag I think this might be wrong, but I am not sure.

This is basically saying that if you pass a non const string here, that is an assignment variable, so the results of parsing that flag would get passed into that variable. add_flag("--flag", variable, "description") so if the flag were used in the command line then the output would get put into variable.

@phlptp
Copy link
Collaborator

phlptp commented Jun 27, 2025

I like it, may add a few more tests later on, but this looks good, I am going to merge it

@phlptp phlptp merged commit 7a7ba4b into CLIUtils:main Jun 27, 2025
57 checks passed
@tnixeu
Copy link
Contributor Author

tnixeu commented Jun 27, 2025

I was a bit confused by this part of the existing comment if a variable string is passed that variable will be assigned the results from the flag I think this might be wrong, but I am not sure.

This is basically saying that if you pass a non const string here, that is an assignment variable, so the results of parsing that flag would get passed into that variable. add_flag("--flag", variable, "description") so if the flag were used in the command line then the output would get put into variable.

I see, but in this case the std::enable_if would block it. It seems as if the comment is meant for another function.

@phlptp
Copy link
Collaborator

phlptp commented Jun 27, 2025

Yeah you are probably right, that comment is probably copied from the other overloads. I will clean that up in another PR soon.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants