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

enable command line argument transformation #963

Merged
merged 3 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
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
46 changes: 45 additions & 1 deletion src/Cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,51 @@ Cli::noSuchArg(std::string_view arg) const {
Cli::exec(
const std::string_view subcmd, const std::span<const std::string_view> args
) const {
return subcmds.at(subcmd).mainFn(args);
return subcmds.at(subcmd).mainFn(transformOptions(subcmd, args));
}

std::vector<std::string_view>
Cli::transformOptions(
std::string_view subcmd, std::span<const std::string_view> args
) const {
const Subcmd& cmd = subcmds.at(subcmd);
std::vector<std::string_view> transformed;
transformed.reserve(args.size());
for (std::string_view arg : args) {
if (arg.starts_with("--")) {
if (auto pos = arg.find_first_of('='); pos != std::string_view::npos) {
transformed.push_back(arg.substr(0, pos));
transformed.push_back(arg.substr(pos + 1));
continue;
}
} else if (arg.starts_with("-")) {
std::string_view multioption = arg.substr(1);
bool handled = false;
for (std::size_t i = 0; i < multioption.size(); ++i) {
const auto handle = [&](const std::span<const Opt> opts) {
for (const Opt& opt : opts) {
if (opt.shortName.empty())
continue;
if (opt.shortName.substr(1) != multioption.substr(i, 1))
continue;
transformed.push_back(opt.shortName);
if (!opt.placeholder.empty()) {
transformed.push_back(multioption.substr(i + 1));
}
handled = true;
}
};
if (cmd.globalOpts)
handle(*cmd.globalOpts);
handle(cmd.localOpts);
}
if (handled)
continue;
}

transformed.push_back(arg);
}
return transformed;
}

void
Expand Down
4 changes: 4 additions & 0 deletions src/Cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ class Cli : public CliBase<Cli> {
}

private:
std::vector<std::string_view> transformOptions(
std::string_view subcmd, std::span<const std::string_view> args
) const;

usize calcMaxShortSize() const noexcept;

/// Print help message for poac itself.
Expand Down
4 changes: 0 additions & 4 deletions src/Cmd/Build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ buildMain(const std::span<const std::string_view> args) {
return Subcmd::missingArgumentForOpt(*itr);
}
setParallelism(std::stoul((++itr)->data()));
} else if (itr->starts_with("-j")) {
setParallelism(std::stoul(itr->substr(2).data()));
} else if (itr->starts_with("--jobs=")) {
setParallelism(std::stoul(itr->substr(7).data()));
} else {
return BUILD_CMD.noSuchArg(*itr);
}
Expand Down
1 change: 1 addition & 0 deletions src/Cmd/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ inline const Opt OPT_JOBS =
Opt{ "--jobs" }
.setShort("-j")
.setDesc("Set the number of jobs to run in parallel")
.setPlaceholder("<NUM>")
.setDefault(NUM_DEFAULT_THREADS);
Loading