From 2c3d0b0d1f87cf68c960393909beb1bd34e1174b Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Mon, 2 Nov 2026 19:25:48 +0100 Subject: [PATCH] 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. --- implementations/Crafter.Build-Clang.cpp | 69 +++++++++++++------------ 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index 24d365d..eb805be 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -2078,11 +2078,12 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) { std::vector projectArgs; projectArgs.reserve(argc); - bool runTests = false; - bool runLint = false; - bool runFormat = false; - bool runClean = false; - bool runScript = false; + // What this invocation does: the first verb on the command line, a + // build without one. A verb word after it is an operand of that one + // (`test lint` runs the tests that match "lint", `run test` runs the + // script called test) or a project arg, never a second verb. + enum class Verb { Build, Test, Lint, Format, Clean, Run }; + Verb verb = Verb::Build; bool runAfterBuild = false; RunTestsOptions testOpts; 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. if (scriptOpts.named) { 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 // elsewhere. Options before it keep their usual meaning. scriptOpts.name = arg; scriptOpts.named = true; } else if (operandsOnly) { - if (runTests) { + if (verb == Verb::Test) { testOpts.globs.emplace_back(arg); - } else if (runLint || runFormat) { + } else if (verb == Verb::Lint || verb == Verb::Format) { lintOpts.globs.emplace_back(arg); } else { 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. PrintVersion(); 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); return 0; - } else if (!runLint && !runFormat && !runClean && arg == "test") { - runTests = true; - } else if (!runTests && !runFormat && !runClean && arg == "lint") { - runLint = true; - } else if (!runTests && !runLint && !runFormat && arg == "clean") { - runClean = true; - } else if (!runTests && !runLint && !runClean && arg == "format") { - runFormat = true; + } else if (verb == Verb::Build && arg == "test") { + verb = Verb::Test; + } else if (verb == Verb::Build && arg == "lint") { + verb = Verb::Lint; + } else if (verb == Verb::Build && arg == "clean") { + verb = Verb::Clean; + } else if (verb == Verb::Build && arg == "format") { + verb = Verb::Format; lintOpts.mode = LintMode::Apply; - } else if (!runTests && !runLint && !runFormat && !runClean && arg == "run") { - runScript = true; - } else if (runScript && arg == "--list") { + } else if (verb == Verb::Build && arg == "run") { + verb = Verb::Run; + } else if (verb == Verb::Run && arg == "--list") { scriptOpts.listOnly = true; - } else if (runFormat && arg == "--check") { + } else if (verb == Verb::Format && arg == "--check") { lintOpts.mode = LintMode::Check; } else if (arg == "-r") { runAfterBuild = true; @@ -2153,21 +2154,21 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) { verbosity = Progress::Verbosity::Quiet; } else if (arg.starts_with("--project=")) { 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()))); - } 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())))); - } else if (runTests && arg == "--list") { + } else if (verb == Verb::Test && arg == "--list") { 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())); - } else if (runTests && !arg.starts_with("-")) { + } else if (verb == Verb::Test && !arg.starts_with("-")) { testOpts.globs.emplace_back(arg); - } else if ((runLint || runFormat) && arg == "--list") { + } else if ((verb == Verb::Lint || verb == Verb::Format) && arg == "--list") { lintOpts.listOnly = true; - } else if ((runLint || runFormat) && arg == "--no-ast") { + } else if ((verb == Verb::Lint || verb == Verb::Format) && arg == "--no-ast") { 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); } else { 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. - if (runClean) { + if (verb == Verb::Clean) { std::vector removed = CleanProject(projectFile); if (removed.empty()) { 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); SetParentProject(&config); - if (runScript) { + if (verb == Verb::Run) { scriptOpts.projectFile = fs::absolute(projectFile).lexically_normal(); return RunScript(config, scriptOpts); } - if (runLint || runFormat) { + if (verb == Verb::Lint || verb == Verb::Format) { // lexically_normal: fs::absolute keeps a "./" component, which // would leak into displayed paths and the project-root derivation. lintOpts.projectFile = fs::absolute(projectFile).lexically_normal(); 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; // Apply: formatting files is success (gofmt convention). // 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. if (!testOpts.listOnly) UpdateAgentsMd(config, projectFile); - if (runTests) { + if (verb == Verb::Test) { TestSummary summary = RunTests(config, testOpts, projectArgs); return summary.AllPassed() ? 0 : 1; }