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

Use cxxopts for command line arguments #86

Merged
merged 8 commits into from
Apr 15, 2024
Merged

Use cxxopts for command line arguments #86

merged 8 commits into from
Apr 15, 2024

Conversation

lisitsyn
Copy link
Owner

@lisitsyn lisitsyn commented Apr 14, 2024

cxxopts seems to be a go-to solution for command line options parsing nowadays. Using something well-supported seems better than freezing parser in the code. Also, this cleans up the licensing part.

src/cli/main.cpp Outdated
return cxxopts::value<double>()->default_value(defs);
}

auto int_with_default(const char* defs) -> decltype(cxxopts::value<int>()->default_value(""))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iglesias any idea how to avoid code duplication here? It seems template aliases are only supported for types but I wanted to avoid introducing classes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's in #87. I was doubting if the return type deduction would be problematic with compiler version but locally with gcc 13.2 it worked just fine without tweaking any options. I don't know in the CI yet. Can you take a look and try if it works well on your end too?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense trying to simplify more passing int, double or std::string (instead of the literals)? Deducing the type and then no need to pass it, e.g. toward with_default(2) instead of int_with_default("2") or with_default<int>("2").

src/cli/main.cpp Outdated
{
processed.push_back(argv[i]);
#if defined(USE_SLASH_CLI_WINDOWS) && (defined(_WIN32) || defined(_WIN64))
// rpplace -- and - with /
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a complicated story here.

We can basically replace / with -- and pass it to the parsing library. But this doesn't help with shortened keywords because they have to be replaced with -. The library doesn't support anything about that with '-' being hardcoded (sic!).

I am in favour of just dropping the slash support here.

@iglesias curious what you think.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other options might be to drop support of short keywords. The verbose style of WIndows commands would match that, too. We can also replace - style with CamelCase.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will get back :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... I think I see.

Among those two options, I would definitely choose dropping the slash support. It's subjective, though. When going through cxxopts README, I got the idea that one its main features is supporting long and short keywords.

Something involving some more work, but I think would help to replace / with -- or -:
At the moment the long options are defined as static const char*s and then, each associated with the short version via calls to either.
Instead of the const char*s, I think we could use some kind of map from empty structs to string, or a set of strings if the tags aren't essential. At the moment of replacing, if it is in the map's values or in the set, then replace with --, detected as a long option, otherwise with -. Would that make sense, or would it clash with the error handling for unknown options? If the latter, then the mapping should associate to short options too and make two lookups.
In case it sounds good, I could work on this during this week. Or I could also let you to it and review, that's also fine of course. Any option is good :)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropping it is then! Let me just commit it right away.

I think it is way too complicated to be worth spending your time.

@lisitsyn
Copy link
Owner Author

@iglesias looks like it is ready to go

@lisitsyn lisitsyn changed the title Use cxxopts for command line arguments [WIP] Use cxxopts for command line arguments Apr 15, 2024
@lisitsyn lisitsyn merged commit 4efe427 into main Apr 15, 2024
4 of 6 checks passed
@lisitsyn lisitsyn deleted the refactor/cxxopts branch May 22, 2024 12:10
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