Skip to content
Merged
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
54 changes: 39 additions & 15 deletions include/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1899,32 +1899,22 @@

~ArgumentBuilder() noexcept(false)
{
if (is_mutually_exclusive() && !is_allowed_in_mutually_exclusive_group())

Check failure on line 1902 in include/argparse.hpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not throw uncaught exceptions in a destructor.

See more on https://sonarcloud.io/project/issues?id=kkarbowiak_cpp-argparse&issues=AZrlo4MIq_RulXfOClaY&open=AZrlo4MIq_RulXfOClaY&pullRequest=190
{
throw option_error("mutually exclusive arguments must be optional");
}

if ((m_options.action == argparse::version) && m_options.help.empty())
{
m_options.help = "show program's version number and exit";
}

if (is_positional())
{
if (m_options.mutually_exclusive_group != nullptr
&& (!m_options.nargs.has_value()
|| (!std::holds_alternative<Nargs>(*m_options.nargs)
|| (std::get<Nargs>(*m_options.nargs) != zero_or_one
&& std::get<Nargs>(*m_options.nargs) != zero_or_more))))
{
throw option_error("mutually exclusive arguments must be optional");
}

m_arguments.emplace_back(PositionalArgument(std::move(m_options)));
}
else
{
if (m_options.mutually_exclusive_group != nullptr
&& m_options.required)
{
throw option_error("mutually exclusive arguments must be optional");
}

m_arguments.emplace_back(OptionalArgument(std::move(m_options)));
}
}
Expand Down Expand Up @@ -2025,6 +2015,40 @@
return !m_options.names.front().starts_with('-');
}

auto is_mutually_exclusive() const -> bool
{
return m_options.mutually_exclusive_group != nullptr;
}

auto is_allowed_in_mutually_exclusive_group() const -> bool
{
if (is_positional())
{
return is_optional_by_nargs_option();
}
else
{
return !m_options.required;
}
}

auto is_optional_by_nargs_option() const -> bool
{
if (!m_options.nargs.has_value())
{
return false;
}

if (!std::holds_alternative<Nargs>(*m_options.nargs))
{
return false;
}

auto const nargs = std::get<Nargs>(*m_options.nargs);

return nargs == zero_or_one || nargs == zero_or_more;
}

private:
Arguments & m_arguments;
OptString & m_version;
Expand Down