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

Implicit value formatting #436

Open
AbitTheGray opened this issue Jul 10, 2024 · 3 comments
Open

Implicit value formatting #436

AbitTheGray opened this issue Jul 10, 2024 · 3 comments

Comments

@AbitTheGray
Copy link

Hi, I was configuring cxxopts for new project and used some implicit values. They look a little too complex for me

--fullscreen [=monitor_name(=PRIMARY)]

with a little tweak (see below) you can have one of those

--fullscreen [monitor_name]=PRIMARY
--fullscreen [monitor_name=PRIMARY]

but I know it may not fit everyone.

Would you consider using std::format (since C++20) to allow more customization? Either per-option or at higher level.


Here is the little tweak on cxxopts.hpp around LN2150.

At the end of format_option is for options with implicit value formatting like

result += " [=" + arg + "(=" + toLocalString(o.implicit_value) + ")]";

but I would like it to look like one of those

result += " " + arg + "=" + toLocalString(o.implicit_value);
result += " [" + arg + "=" + toLocalString(o.implicit_value) + "]";

to be more in line with the one without implicit value

result += " " + arg;

My personal goal is to have <argument> for options with required values and [argument=DEFAULT] or [argument]=DEFAULT.

I like this library, if you like I will prepare the change as Pull Request. I just need to know whenever you like the idea and how to approach it.

@jarro2783
Copy link
Owner

I think your suggested syntax does look better. I'm happy to look at a PR with the changes. I don't really want to use std::format because I want to keep supporting older versions of c++.

@just-ero
Copy link

just-ero commented Jul 31, 2024

Our team would prefer syntax like the following:

--foo [=BAR]

Handling of empty strings is also sub-par, showing up like this right now:

--foo [=FOO-ARG(=)]
(default: )

We would prefer the empty implicit value to be replaced with <empty> or something similar.

@AbitTheGray
Copy link
Author

Sorry for late reply due to lack of time.

I hastily put together the small formatting tweak (--output [BIN=b.def]) in MR to my fork.

If you wish, there is another MR with ability to specify format for each option (not a good idea, more of PoC). It can be behind CMake option() so you need to explicitly enable it (for those who don't have C++20 support or just don't want the functionality).
I would like to tweak it to be configured on cxxopts::Options instead of Value/HelpOptionDetails but I will need to find time to look into it.

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

No branches or pull requests

3 participants