refactor: one verb per invocation, as an enum

Run() kept a flag per verb: test, lint, clean and format were each
recognised only when the others' flags were clear, so a verb added among
them meant another condition in each of their branches, while run took
the words after it in a branch ahead of them all. One Verb now records
the first verb on the command line. A verb word after it is taken as
that verb takes any other word: a glob after test, lint and format, the
script's name after run, a project arg after clean, as it already was
for two different verbs. Only a repeated word changes: `test test`,
`lint lint` and `format format` now take the second word as a glob
instead of reading it as the verb again. That fixes
`format --check format`: the second `format` set the mode back to
applying and recorded no glob, so the run applied every rule and rewrote
the project's sources, though --check writes nothing.
This commit is contained in:
Jorijn van der Graaf 2026-11-02 19:25:48 +01:00
commit 2c3d0b0d1f
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB

View file

@ -2078,11 +2078,12 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
std::vector<std::string_view> projectArgs; std::vector<std::string_view> projectArgs;
projectArgs.reserve(argc); projectArgs.reserve(argc);
bool runTests = false; // What this invocation does: the first verb on the command line, a
bool runLint = false; // build without one. A verb word after it is an operand of that one
bool runFormat = false; // (`test lint` runs the tests that match "lint", `run test` runs the
bool runClean = false; // script called test) or a project arg, never a second verb.
bool runScript = false; enum class Verb { Build, Test, Lint, Format, Clean, Run };
Verb verb = Verb::Build;
bool runAfterBuild = false; bool runAfterBuild = false;
RunTestsOptions testOpts; RunTestsOptions testOpts;
RunLintOptions lintOpts; RunLintOptions lintOpts;
@ -2102,15 +2103,15 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
// variant hash and so the directory the project builds into. // variant hash and so the directory the project builds into.
if (scriptOpts.named) { if (scriptOpts.named) {
scriptOpts.args.emplace_back(arg); scriptOpts.args.emplace_back(arg);
} else if (runScript && (operandsOnly || !arg.starts_with("-"))) { } else if (verb == Verb::Run && (operandsOnly || !arg.starts_with("-"))) {
// The first word after `run`, even one that is a verb // The first word after `run`, even one that is a verb
// elsewhere. Options before it keep their usual meaning. // elsewhere. Options before it keep their usual meaning.
scriptOpts.name = arg; scriptOpts.name = arg;
scriptOpts.named = true; scriptOpts.named = true;
} else if (operandsOnly) { } else if (operandsOnly) {
if (runTests) { if (verb == Verb::Test) {
testOpts.globs.emplace_back(arg); testOpts.globs.emplace_back(arg);
} else if (runLint || runFormat) { } else if (verb == Verb::Lint || verb == Verb::Format) {
lintOpts.globs.emplace_back(arg); lintOpts.globs.emplace_back(arg);
} else { } else {
projectArgs.push_back(arg); projectArgs.push_back(arg);
@ -2127,23 +2128,23 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
// project.cpp as an unknown argument. // project.cpp as an unknown argument.
PrintVersion(); PrintVersion();
return 0; return 0;
} else if (arg == "-h" || arg == "--help" || (!runTests && !runLint && !runFormat && arg == "help")) { } else if (arg == "-h" || arg == "--help" || (verb != Verb::Test && verb != Verb::Lint && verb != Verb::Format && arg == "help")) {
PrintHelp(argv0); PrintHelp(argv0);
return 0; return 0;
} else if (!runLint && !runFormat && !runClean && arg == "test") { } else if (verb == Verb::Build && arg == "test") {
runTests = true; verb = Verb::Test;
} else if (!runTests && !runFormat && !runClean && arg == "lint") { } else if (verb == Verb::Build && arg == "lint") {
runLint = true; verb = Verb::Lint;
} else if (!runTests && !runLint && !runFormat && arg == "clean") { } else if (verb == Verb::Build && arg == "clean") {
runClean = true; verb = Verb::Clean;
} else if (!runTests && !runLint && !runClean && arg == "format") { } else if (verb == Verb::Build && arg == "format") {
runFormat = true; verb = Verb::Format;
lintOpts.mode = LintMode::Apply; lintOpts.mode = LintMode::Apply;
} else if (!runTests && !runLint && !runFormat && !runClean && arg == "run") { } else if (verb == Verb::Build && arg == "run") {
runScript = true; verb = Verb::Run;
} else if (runScript && arg == "--list") { } else if (verb == Verb::Run && arg == "--list") {
scriptOpts.listOnly = true; scriptOpts.listOnly = true;
} else if (runFormat && arg == "--check") { } else if (verb == Verb::Format && arg == "--check") {
lintOpts.mode = LintMode::Check; lintOpts.mode = LintMode::Check;
} else if (arg == "-r") { } else if (arg == "-r") {
runAfterBuild = true; runAfterBuild = true;
@ -2153,21 +2154,21 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
verbosity = Progress::Verbosity::Quiet; verbosity = Progress::Verbosity::Quiet;
} else if (arg.starts_with("--project=")) { } else if (arg.starts_with("--project=")) {
projectFile = arg.substr(std::string_view("--project=").size()); projectFile = arg.substr(std::string_view("--project=").size());
} else if (runTests && arg.starts_with("--jobs=")) { } else if (verb == Verb::Test && arg.starts_with("--jobs=")) {
testOpts.jobs = std::stoi(std::string(arg.substr(std::string_view("--jobs=").size()))); testOpts.jobs = std::stoi(std::string(arg.substr(std::string_view("--jobs=").size())));
} else if (runTests && arg.starts_with("--timeout=")) { } else if (verb == Verb::Test && arg.starts_with("--timeout=")) {
testOpts.timeoutOverride = std::chrono::seconds(std::stoi(std::string(arg.substr(std::string_view("--timeout=").size())))); testOpts.timeoutOverride = std::chrono::seconds(std::stoi(std::string(arg.substr(std::string_view("--timeout=").size()))));
} else if (runTests && arg == "--list") { } else if (verb == Verb::Test && arg == "--list") {
testOpts.listOnly = true; testOpts.listOnly = true;
} else if (runTests && arg.starts_with("--runner=")) { } else if (verb == Verb::Test && arg.starts_with("--runner=")) {
testOpts.runnerOverride = std::string(arg.substr(std::string_view("--runner=").size())); testOpts.runnerOverride = std::string(arg.substr(std::string_view("--runner=").size()));
} else if (runTests && !arg.starts_with("-")) { } else if (verb == Verb::Test && !arg.starts_with("-")) {
testOpts.globs.emplace_back(arg); testOpts.globs.emplace_back(arg);
} else if ((runLint || runFormat) && arg == "--list") { } else if ((verb == Verb::Lint || verb == Verb::Format) && arg == "--list") {
lintOpts.listOnly = true; lintOpts.listOnly = true;
} else if ((runLint || runFormat) && arg == "--no-ast") { } else if ((verb == Verb::Lint || verb == Verb::Format) && arg == "--no-ast") {
lintOpts.noAst = true; lintOpts.noAst = true;
} else if ((runLint || runFormat) && !arg.starts_with("-")) { } else if ((verb == Verb::Lint || verb == Verb::Format) && !arg.starts_with("-")) {
lintOpts.globs.emplace_back(arg); lintOpts.globs.emplace_back(arg);
} else { } else {
projectArgs.push_back(arg); projectArgs.push_back(arg);
@ -2191,7 +2192,7 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
} }
// Ahead of LoadProject on purpose — see CleanProject. // Ahead of LoadProject on purpose — see CleanProject.
if (runClean) { if (verb == Verb::Clean) {
std::vector<fs::path> removed = CleanProject(projectFile); std::vector<fs::path> removed = CleanProject(projectFile);
if (removed.empty()) { if (removed.empty()) {
std::println("Nothing to clean"); std::println("Nothing to clean");
@ -2204,17 +2205,17 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
Configuration config = LoadProject(projectFile, projectArgs); Configuration config = LoadProject(projectFile, projectArgs);
SetParentProject(&config); SetParentProject(&config);
if (runScript) { if (verb == Verb::Run) {
scriptOpts.projectFile = fs::absolute(projectFile).lexically_normal(); scriptOpts.projectFile = fs::absolute(projectFile).lexically_normal();
return RunScript(config, scriptOpts); return RunScript(config, scriptOpts);
} }
if (runLint || runFormat) { if (verb == Verb::Lint || verb == Verb::Format) {
// lexically_normal: fs::absolute keeps a "./" component, which // lexically_normal: fs::absolute keeps a "./" component, which
// would leak into displayed paths and the project-root derivation. // would leak into displayed paths and the project-root derivation.
lintOpts.projectFile = fs::absolute(projectFile).lexically_normal(); lintOpts.projectFile = fs::absolute(projectFile).lexically_normal();
LintSummary lintSummary = RunLint(config, lintOpts); LintSummary lintSummary = RunLint(config, lintOpts);
if (!runFormat) return lintSummary.Clean() ? 0 : 1; if (verb == Verb::Lint) return lintSummary.Clean() ? 0 : 1;
if (lintSummary.noRulesDefined || lintSummary.errors > 0) return 1; if (lintSummary.noRulesDefined || lintSummary.errors > 0) return 1;
// Apply: formatting files is success (gofmt convention). // Apply: formatting files is success (gofmt convention).
// Check: a would-change file is the CI failure signal. // Check: a would-change file is the CI failure signal.
@ -2229,7 +2230,7 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
// that fails needs them. // that fails needs them.
if (!testOpts.listOnly) UpdateAgentsMd(config, projectFile); if (!testOpts.listOnly) UpdateAgentsMd(config, projectFile);
if (runTests) { if (verb == Verb::Test) {
TestSummary summary = RunTests(config, testOpts, projectArgs); TestSummary summary = RunTests(config, testOpts, projectArgs);
return summary.AllPassed() ? 0 : 1; return summary.AllPassed() ? 0 : 1;
} }