Skip to content

Commit 6f8c5f0

Browse files
authored
Cli: fix & improve option expansion (#1124)
1 parent df25af2 commit 6f8c5f0

23 files changed

+616
-299
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ SRCS := $(shell find src -name '*.cc')
5050
OBJS := $(patsubst src/%,$(O)/%,$(SRCS:.cc=.o))
5151
DEPS := $(OBJS:.o=.d)
5252

53-
UNITTEST_SRCS := src/BuildConfig.cc src/Algos.cc src/Semver.cc src/VersionReq.cc src/Manifest.cc
53+
UNITTEST_SRCS := src/BuildConfig.cc src/Algos.cc src/Semver.cc src/VersionReq.cc src/Manifest.cc src/Cli.cc
5454
UNITTEST_OBJS := $(patsubst src/%,$(O)/tests/test_%,$(UNITTEST_SRCS:.cc=.o))
5555
UNITTEST_BINS := $(UNITTEST_OBJS:.o=)
5656
UNITTEST_DEPS := $(UNITTEST_OBJS:.o=.d)
@@ -88,6 +88,7 @@ test: $(UNITTEST_BINS)
8888
@$(O)/tests/test_Semver
8989
@$(O)/tests/test_VersionReq
9090
@$(O)/tests/test_Manifest
91+
@$(O)/tests/test_Cli
9192

9293
$(O)/tests/test_%.o: src/%.cc $(GIT_DEPS)
9394
$(MKDIR_P) $(@D)
@@ -118,6 +119,10 @@ $(O)/tests/test_Manifest: $(O)/tests/test_Manifest.o $(O)/TermColor.o \
118119
$(O)/Git2/Object.o $(O)/Command.o
119120
$(CXX) $(CXXFLAGS) $^ $(LIBS) $(LDFLAGS) -o $@
120121

122+
$(O)/tests/test_Cli: $(O)/tests/test_Cli.o $(O)/Algos.o $(O)/TermColor.o \
123+
$(O)/Command.o
124+
$(CXX) $(CXXFLAGS) $^ $(LIBS) $(LDFLAGS) -o $@
125+
121126

122127
tidy: $(TIDY_TARGETS)
123128

src/Cabin.cc

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@
77
#include "Rustify/Result.hpp"
88
#include "TermColor.hpp"
99

10-
#include <cstdlib>
11-
#include <exception>
12-
#include <fmt/core.h>
13-
#include <span>
1410
#include <string>
15-
#include <string_view>
1611
#include <utility>
17-
#include <vector>
1812

1913
namespace cabin {
2014

@@ -27,7 +21,10 @@ getCli() noexcept {
2721
.setShort("-v")
2822
.setDesc("Use verbose output (-vv very verbose output)")
2923
.setGlobal(true))
24+
// TODO: assuming -- for long options would be better, also empty
25+
// long options should be allowed?
3026
.addOpt(Opt{ "-vv" }
27+
.setShort("-vv")
3128
.setDesc("Use very verbose output")
3229
.setGlobal(true)
3330
.setHidden(true))
@@ -67,66 +64,20 @@ getCli() noexcept {
6764
return cli;
6865
}
6966

70-
static Result<void>
71-
parseArgs(const std::span<char* const> args) noexcept {
72-
// Parse arguments (options should appear before the subcommand, as the help
73-
// message shows intuitively)
74-
// cabin --verbose run --release help --color always --verbose
75-
// ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76-
// [global] [run] [help (under run)]
77-
for (auto itr = args.begin(); itr != args.end(); ++itr) {
78-
// Global options
79-
const auto control = Try(Cli::handleGlobalOpts(itr, args.end()));
80-
if (control == Cli::Return) {
81-
return Ok();
82-
} else if (control == Cli::Continue) {
83-
continue;
84-
}
85-
// else: Fallthrough: current argument wasn't handled
86-
87-
// Local options
88-
else if (*itr == "-V"sv || *itr == "--version"sv) {
89-
const std::vector<std::string_view> remArgs(itr + 1, args.end());
90-
return versionMain(remArgs);
91-
} else if (*itr == "--list"sv) {
92-
fmt::print("{}", getCli().formatAllSubcmds(true));
93-
return Ok();
94-
}
95-
96-
// Subcommands
97-
else if (getCli().hasSubcmd(*itr)) {
98-
const std::vector<std::string_view> remArgs(itr + 1, args.end());
99-
try {
100-
return getCli().exec(*itr, remArgs);
101-
} catch (const std::exception& e) {
102-
Bail(e.what());
103-
}
104-
}
105-
106-
// Unexpected argument
107-
else {
108-
return getCli().noSuchArg(*itr);
109-
}
110-
}
111-
112-
return getCli().printHelp({});
113-
}
114-
11567
static std::string
11668
colorizeAnyhowError(std::string s) {
117-
// `Caused by:` leaves a trailing newline
11869
if (s.find("Caused by:") != std::string::npos) {
11970
replaceAll(s, "Caused by:", Yellow("Caused by:").toErrStr());
71+
// `Caused by:` leaves a trailing newline, FIXME: upstream this
12072
replaceAll(s, "\n", "");
12173
}
12274
return s;
12375
}
12476

12577
Result<void, void>
126-
cliMain(int argc, char* argv[]) noexcept { // NOLINT(*-avoid-c-arrays)
127-
// Drop the first argument (program name)
128-
const std::span<char* const> args(argv + 1, argv + argc);
129-
return parseArgs(args)
78+
cabinMain(int argc, char* argv[]) noexcept { // NOLINT(*-avoid-c-arrays)
79+
return getCli()
80+
.parseArgs(argc, argv)
13081
.map_err([](const auto& e) { return colorizeAnyhowError(e->what()); })
13182
.map_err([](std::string e) { logger::error("{}", std::move(e)); });
13283
}

src/Cabin.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
namespace cabin {
66

77
// NOLINTNEXTLINE(*-avoid-c-arrays)
8-
Result<void, void> cliMain(int argc, char* argv[]) noexcept;
8+
Result<void, void> cabinMain(int argc, char* argv[]) noexcept;
99

1010
} // namespace cabin

0 commit comments

Comments
 (0)