Skip to content

Commit

Permalink
-fixed usage info width calculation, by taking commands width into ac…
Browse files Browse the repository at this point in the history
…count;

-set version to 2.7.0;
  • Loading branch information
kamchatka-volcano committed Jan 3, 2025
1 parent e0d9e65 commit 1a46d66
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.18)
project(cmdlime VERSION 2.6.0 DESCRIPTION "C++17 command line parsing library")
project(cmdlime VERSION 2.7.0 DESCRIPTION "C++17 command line parsing library")
include(external/seal_lake)

option(CMDLIME_USE_NAMEOF "Enable automatic registration of struct field names using the nameof library" OFF)
Expand Down
9 changes: 9 additions & 0 deletions include/cmdlime/detail/gnuformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,15 @@ class GNUOutputFormatter {
stream << "<" << argList.info().name() << "> (" << argList.info().valueName() << ")";
return stream.str();
}

static std::string commandDescriptionName(const ICommand& command, int indent = 0)
{
auto stream = std::stringstream{};
if (indent)
stream << std::setw(indent) << " ";
stream << command.info().name() << " [options]";
return stream.str();
}
};

template<>
Expand Down
9 changes: 9 additions & 0 deletions include/cmdlime/detail/posixformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ class PosixOutputFormatter {
stream << "<" << argList.info().name() << "> (" << argList.info().valueName() << ")";
return stream.str();
}

static std::string commandDescriptionName(const ICommand& command, int indent = 0)
{
auto stream = std::stringstream{};
if (indent)
stream << std::setw(indent) << " ";
stream << command.info().name() << " [options]";
return stream.str();
}
};

template<>
Expand Down
9 changes: 9 additions & 0 deletions include/cmdlime/detail/simpleformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ class DefaultOutputFormatter {
stream << "<" << argList.info().name() << "> (" << argList.info().valueName() << ")";
return stream.str();
}

static std::string commandDescriptionName(const ICommand& command, int indent = 0)
{
auto stream = std::stringstream{};
if (indent)
stream << std::setw(indent) << " ";
stream << command.info().name() << " [options]";
return stream.str();
}
};

template<>
Expand Down
10 changes: 6 additions & 4 deletions include/cmdlime/detail/usageinfocreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ class UsageInfoCreator {

std::string paramsInfo()
{
auto result = std::string{"Parameters:\n"};
if (params_.empty())
return result;
return {};
auto result = std::string{"Parameters:\n"};
for (const IParam& param : params_) {
const auto name = OutputFormatter::paramDescriptionName(param, outputSettings_.nameIndentation) + "\n";
result += makeConfigFieldInfo(name, getDescription(param));
Expand All @@ -205,9 +205,9 @@ class UsageInfoCreator {

std::string paramListsInfo()
{
auto result = std::string{};
if (paramLists_.empty())
return result;
return {};
auto result = std::string{};
for (const IParamList& paramList : paramLists_) {
const auto name =
OutputFormatter::paramListDescriptionName(paramList, outputSettings_.nameIndentation) + "\n";
Expand Down Expand Up @@ -364,6 +364,8 @@ class UsageInfoCreator {
updateLength(OutputFormatter::flagDescriptionName(*flag, outputSettings_.nameIndentation));
for (auto& arg : options_.args())
updateLength(OutputFormatter::argDescriptionName(*arg, outputSettings_.nameIndentation));
for (auto& command : options_.commands())
updateLength(OutputFormatter::commandDescriptionName(*command, outputSettings_.nameIndentation));
if (options_.argList())
updateLength(OutputFormatter::argListDescriptionName(*options_.argList(), outputSettings_.nameIndentation));
return length;
Expand Down
9 changes: 9 additions & 0 deletions include/cmdlime/detail/x11format.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ class X11OutputFormatter {
stream << "<" << argList.info().name() << "> (" << argList.info().valueName() << ")";
return stream.str();
}

static std::string commandDescriptionName(const ICommand& command, int indent = 0)
{
auto stream = std::stringstream{};
if (indent)
stream << std::setw(indent) << " ";
stream << command.info().name() << " [options]";
return stream.str();
}
};

template<>
Expand Down
18 changes: 18 additions & 0 deletions tests/test_gnu_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ struct FullConfig : public Config {
CMDLIME_SUBCOMMAND(subcommand, SubcommandConfig);
};

struct CommandsConfig : public Config {
CMDLIME_COMMAND(cmd, SubcommandConfig);
CMDLIME_SUBCOMMAND(subcommand, SubcommandConfig);
};

#ifdef CMDLIME_NAMEOF_AVAILABLE
struct FullConfigWithoutMacro : public Config {
std::string requiredParam = param<&T::requiredParam>();
Expand Down Expand Up @@ -1387,6 +1392,19 @@ TEST(GNUConfig, DetailedUsageInfo)
EXPECT_EQ(reader.usageInfoDetailed<FullConfig>(), expectedDetailedInfo);
}

TEST(GNUConfig, DetailedUsageInfoCommandsOnly)
{
auto reader = cmdlime::CommandLineReader<cmdlime::Format::GNU>{};
reader.setProgramName("testproc");
auto expectedDetailedInfo =
std::string{"Usage: testproc [commands] \n"
"Commands:\n"
" cmd [options] \n"
" subcommand [options] \n"};
EXPECT_EQ(reader.usageInfoDetailed<CommandsConfig>(), expectedDetailedInfo);
}


TEST(GNUConfig, DetailedUsageInfoWithoutMacro)
{
auto reader = cmdlime::CommandLineReader<cmdlime::Format::GNU>{};
Expand Down

0 comments on commit 1a46d66

Please sign in to comment.