jarro2783 / cxxopts

Lightweight C++ command line option parser
MIT License
4.25k stars 590 forks source link

Implicit value formatting #436

Open AbitTheGray opened 4 months ago

AbitTheGray commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 3 months ago

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.