tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
Split monolithic CommandLine.cs into CommandTypes, CommandLine, and CommandMakers
- Commit
0934dd359916eb1517fc3c87dfcca2104175d889- Author
- Leitwolf11 ·
- Parents
3cec2b7f63da
Changes
src/Progmasoft.Tazel/Cli/CommandLine.cs | 710 ++++-------------------------- src/Progmasoft.Tazel/Cli/CommandMakers.cs | 404 +++++++++++++++++ src/Progmasoft.Tazel/Cli/CommandTypes.cs | 185 ++++++++ 3 files changed, 663 insertions(+), 636 deletions(-) diff --git a/src/Progmasoft.Tazel/Cli/CommandLine.cs b/src/Progmasoft.Tazel/Cli/CommandLine.cs index 4cfcddf..25644d5 100644 --- a/src/Progmasoft.Tazel/Cli/CommandLine.cs +++ b/src/Progmasoft.Tazel/Cli/CommandLine.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Text; using Progmasoft.Tazel.Dsl.Common; @@ -11,156 +10,6 @@ using Progmasoft.Tazel.Output; namespace Progmasoft.Tazel.Cli; -/// <summary>A command line that cannot be taken, with a hint when there is one.</summary> -public sealed class CommandLineException : Exception -{ - /// <summary>What cannot be taken, and how to put it right.</summary> - public CommandLineException(string message) - : base(message) - { - } - - /// <summary>Nothing to say.</summary> - public CommandLineException() - { - } - - /// <summary>What cannot be taken, and why.</summary> - public CommandLineException(string message, Exception inner) - : base(message, inner) - { - } -} - -/// <summary>How Tazel prints, for every command.</summary> -internal sealed record OutputOptions(When Color, When Progress, bool Quiet, MessageFormat Format = MessageFormat.RichStdio); - -/// <summary>What <c>build</c>, <c>run</c> and <c>test</c> take beyond their labels.</summary> -internal sealed record BuildFlags -{ - public BuildMode? Mode { get; init; } - - public Platform? Platform { get; init; } - - public int? Jobs { get; init; } - - public bool KeepGoing { get; init; } - - /// <summary><c>--recompile</c>: every compilation runs.</summary> - public bool Recompile { get; init; } - - /// <summary><c>--no-cache</c>: every action and test runs, and no cache is read or written.</summary> - public bool NoCache { get; init; } - - public List<string> FeaturesOn { get; } = []; - - public List<string> FeaturesOff { get; } = []; - - public List<string> CompilerOptions { get; } = []; - - public List<string> CCompilerOptions { get; } = []; - - public List<string> CppCompilerOptions { get; } = []; - - public List<string> LinkerOptions { get; } = []; - - public List<string> ZigCompilerOptions { get; } = []; -} - -/// <summary>A command line, read into what it asks for; later stages do not look at its text again.</summary> -internal abstract record Command(OutputOptions Output) -{ - /// <summary>The settings of --dotnet-settings, Name=Value each, checked; for this Tazel process alone.</summary> - public IReadOnlyList<Runtime.DotnetSetting> DotnetSettings { get; init; } = []; -} - -internal sealed record BuildCommand(OutputOptions Output, IReadOnlyList<string> Labels, BuildFlags Flags) : Command(Output); - -internal sealed record TestCommand(OutputOptions Output, IReadOnlyList<string> Labels, BuildFlags Flags) : Command(Output); - -internal sealed record RunCommand(OutputOptions Output, string? Label, BuildFlags Flags, IReadOnlyList<string> Arguments) - : Command(Output); - -internal sealed record CleanCommand(OutputOptions Output, bool Expunge, bool DryRun) : Command(Output); - -internal sealed record PackageCommand(OutputOptions Output, bool List, bool AllowDirty, string? OutputDir) : Command(Output); - -internal sealed record CustomCommandLine(OutputOptions Output, IReadOnlyList<string> Words) : Command(Output); - -internal sealed record VersionCommand(OutputOptions Output, bool Verbose, bool Json) : Command(Output); - -/// <summary>How much <c>tazel help</c> prints: <c>--short</c>, medium (default), or <c>--long</c>.</summary> -internal enum HelpLength -{ - Short, - Medium, - Long, -} - -/// <summary>Output format of <c>tazel license</c>.</summary> -internal enum LicenseFormat -{ - Text, - Markdown, - RestructuredText, -} - -internal sealed record LicenseCommand(OutputOptions Output, LicenseFormat Format) : Command(Output); - -internal sealed record HelpCommand(OutputOptions Output, string? Topic, HelpLength Length = HelpLength.Medium) : Command(Output) -{ - public bool Long => Length == HelpLength.Long; -} - -internal sealed record CoverageCommand(OutputOptions Output, IReadOnlyList<string> Labels, BuildFlags Flags) : Command(Output); - -/// <summary>Which query: of the target graph, of the configured targets, or of the actions.</summary> -internal enum QueryKind -{ - Query, - CQuery, - AQuery, -} - -internal sealed record QueryCommand(OutputOptions Output, QueryKind Kind, string Expression, BuildFlags Flags, string Format) - : Command(Output); - -internal sealed record FetchCommand(OutputOptions Output) : Command(Output); - -internal sealed record ExplainCommand(OutputOptions Output, string Code, HelpLength Length = HelpLength.Medium) : Command(Output) -{ - public bool Long => Length == HelpLength.Long; -} - -/// <summary><c>tazel shell-completion bash|zsh|fish|powershell</c>: emits a shell completion script to stdout.</summary> -internal sealed record ShellCompletionCommand(OutputOptions Output, ShellKind Shell) : Command(Output); - -/// <summary><c>tazel dotnet get <setting></c>: what this process has for a .NET setting.</summary> -internal sealed record DotnetGetCommand(OutputOptions Output, string Setting) : Command(Output); - -/// <summary><c>tazel dotnet what-is <term></c>: what a term of .NET means.</summary> -internal sealed record DotnetWhatIsCommand(OutputOptions Output, string Term) : Command(Output); - -/// <summary><c>tazel limited-time C#</c>: the migration to BUILD.csx and labels in Depends, until Tazel 1.0.0.</summary> -internal sealed record MigrateCommand(OutputOptions Output) : Command(Output); - -internal sealed record VendorCommand(OutputOptions Output) : Command(Output); - -/// <summary>Searching Tazel Portal Modules, or the versions of one module.</summary> -internal enum PortalAction -{ - Search, - Info, -} - -internal sealed record PortalModulesCommand(OutputOptions Output, PortalAction Action, string Query, bool Exact) - : Command(Output); - -internal sealed record DaemonCommand(OutputOptions Output, string Action) : Command(Output); - -/// <summary><c>tazel cache keygen</c>: a key pair for signing the results of a remote cache.</summary> -internal sealed record CacheKeygenCommand(OutputOptions Output, Cache.SigningAlgorithm Algorithm) : Command(Output); - /// <summary> /// Reads command lines by hand, into typed values. Long options take their value after whitespace, <c>=</c> or /// <c>:</c>; short options also joined (<c>-hbuild</c> is not an option, as <c>-h</c> takes none). A list is @@ -174,57 +23,45 @@ internal static class CommandLine ["build", "run", "test", "coverage", "query", "cquery", "aquery", "fetch", "vendor", "clean", "package", "custom", "portal-modules", "cache", "explain", "version", "help", "license", "shell-completion", "dotnet", "limited-time", "options", "custom-cmds", "envs", "tool", "ignore"]; - private enum Kind - { - Flag, - Value, - List, - - // A list after whitespace too: --dotnet-settings A=1,B=2. - ListAlways, - } - - private sealed record Option(string Long, Kind Kind, IReadOnlyList<string>? Values, params string[] Scope); - private static readonly string[] Building = ["build", "run", "test", "coverage", "cquery", "aquery"]; - private static readonly Option[] Options = + private static readonly OptionSpec[] Options = [ - new("color", Kind.Value, ["always", "auto", "never"]), - new("progress", Kind.Value, ["always", "auto", "never"]), - new("quiet", Kind.Flag, null), - new("dotnet-settings", Kind.ListAlways, null), - new("user", Kind.Flag, null, "options", "custom-cmds", "envs"), - new("project", Kind.Flag, null, "options", "custom-cmds", "envs"), - new("local", Kind.Flag, null, "options", "custom-cmds", "envs"), - new("create", Kind.Flag, null, "options"), - new("output-dir", Kind.Value, null, "tool", "package"), - new("remote", Kind.Flag, null, "cache"), - new("dry-run", Kind.Flag, null, "clean"), - new("list", Kind.Flag, null, "package"), - new("allow-dirty", Kind.Flag, null, "package"), - new("verbose", Kind.Flag, null, "version"), - new("message-format", Kind.Value, ["rich-stdio", "script-friendly-stdio", "jsonl", "xml"]), - new("help", Kind.Flag, null), - new("platform", Kind.Value, Platforms.Names, Building), - new("mode", Kind.Value, ["debug", "release"], Building), - new("jobs", Kind.Value, null, Building), - new("keep-going", Kind.Flag, null, Building), - new("recompile", Kind.Flag, null, "build", "run", "test", "coverage"), - new("no-cache", Kind.Flag, null, "build", "run", "test", "coverage"), - new("feature", Kind.List, null, Building), - new("no-feature", Kind.List, null, Building), - new("compiler-options", Kind.List, null, Building), - new("c-compiler-options", Kind.List, null, Building), - new("cpp-compiler-options", Kind.List, null, Building), - new("linker-options", Kind.List, null, Building), - new("zig-compiler-options", Kind.List, null, Building), - new("expunge", Kind.Flag, null, "clean"), - new("short", Kind.Flag, null, "help", "explain"), - new("long", Kind.Flag, null, "help", "explain"), - new("exact", Kind.Flag, null, "portal-modules"), - new("algorithm", Kind.Value, ["ed25519", "rsa-4096", "ml-dsa"], "cache"), - new("output", Kind.Value, null, "query", "cquery", "aquery", "license", "version"), + new("color", OptionKind.Value, ["always", "auto", "never"]), + new("progress", OptionKind.Value, ["always", "auto", "never"]), + new("quiet", OptionKind.Flag, null), + new("dotnet-settings", OptionKind.ListAlways, null), + new("user", OptionKind.Flag, null, "options", "custom-cmds", "envs"), + new("project", OptionKind.Flag, null, "options", "custom-cmds", "envs"), + new("local", OptionKind.Flag, null, "options", "custom-cmds", "envs"), + new("create", OptionKind.Flag, null, "options"), + new("output-dir", OptionKind.Value, null, "tool", "package"), + new("remote", OptionKind.Flag, null, "cache"), + new("dry-run", OptionKind.Flag, null, "clean"), + new("list", OptionKind.Flag, null, "package"), + new("allow-dirty", OptionKind.Flag, null, "package"), + new("verbose", OptionKind.Flag, null, "version"), + new("message-format", OptionKind.Value, ["rich-stdio", "script-friendly-stdio", "jsonl", "xml"]), + new("help", OptionKind.Flag, null), + new("platform", OptionKind.Value, Platforms.Names, Building), + new("mode", OptionKind.Value, ["debug", "release"], Building), + new("jobs", OptionKind.Value, null, Building), + new("keep-going", OptionKind.Flag, null, Building), + new("recompile", OptionKind.Flag, null, "build", "run", "test", "coverage"), + new("no-cache", OptionKind.Flag, null, "build", "run", "test", "coverage"), + new("feature", OptionKind.List, null, Building), + new("no-feature", OptionKind.List, null, Building), + new("compiler-options", OptionKind.List, null, Building), + new("c-compiler-options", OptionKind.List, null, Building), + new("cpp-compiler-options", OptionKind.List, null, Building), + new("linker-options", OptionKind.List, null, Building), + new("zig-compiler-options", OptionKind.List, null, Building), + new("expunge", OptionKind.Flag, null, "clean"), + new("short", OptionKind.Flag, null, "help", "explain"), + new("long", OptionKind.Flag, null, "help", "explain"), + new("exact", OptionKind.Flag, null, "portal-modules"), + new("algorithm", OptionKind.Value, ["ed25519", "rsa-4096", "ml-dsa"], "cache"), + new("output", OptionKind.Value, null, "query", "cquery", "aquery", "license", "version"), ]; /// <summary>Reads <paramref name="arguments"/>.</summary> @@ -250,7 +87,7 @@ internal static class CommandLine return Make(scanned) with { DotnetSettings = settings }; } - private static Command Make(Scanned scanned) + private static Command Make(ScannedCommand scanned) { var command = scanned.Command; // -o means --output for tazel license/version and --output-dir for tazel tool/package. @@ -291,24 +128,17 @@ internal static class CommandLine throw new CommandLineException($"tazel {command} takes nothing after --; only tazel run passes arguments on"); } - return Makers.TryGetValue(command, out var make) ? make(scanned, output) : MakeHelp(scanned, output); + return CommandMakers.Build(scanned, output); } - private static OutputOptions OutputOf(Scanned scanned) => new( - ParseWhen(Last(scanned.Seen, "color")) ?? When.Auto, - ParseWhen(Last(scanned.Seen, "progress")) ?? When.Auto, + private static OutputOptions OutputOf(ScannedCommand scanned) => new( + ParseWhen(scanned.Last("color")) ?? When.Auto, + ParseWhen(scanned.Last("progress")) ?? When.Auto, scanned.Has("quiet"), - ParseFormat(Last(scanned.Seen, "message-format")) ?? MessageFormat.RichStdio); - - /// <summary>The words of a command line, read: the command, the words that are not options, and the options.</summary> - private sealed record Scanned( - string Command, List<string> Positional, List<string> AfterDashes, List<(Option Option, List<string> Values, string Written)> Seen) - { - public bool Has(string option) => Seen.Any(entry => entry.Option.Long == option); - } + ParseFormat(scanned.Last("message-format")) ?? MessageFormat.RichStdio); private static void ReadOption( - string argument, IReadOnlyList<string> arguments, ref int index, List<(Option, List<string>, string)> seen) + string argument, IReadOnlyList<string> arguments, ref int index, List<(OptionSpec, List<string>, string)> seen) { if (argument.StartsWith("--", StringComparison.Ordinal)) { @@ -320,12 +150,12 @@ internal static class CommandLine } } - private static Scanned Scan(IReadOnlyList<string> arguments) + private static ScannedCommand Scan(IReadOnlyList<string> arguments) { string? command = null; var positional = new List<string>(); var afterDashes = new List<string>(); - var seen = new List<(Option Option, List<string> Values, string Written)>(); + var seen = new List<(OptionSpec Option, List<string> Values, string Written)>(); var index = 0; while (index < arguments.Count) { @@ -355,265 +185,7 @@ internal static class CommandLine } } - return new Scanned(command ?? "help", positional, afterDashes, seen); - } - - /// <summary>How each command is made from what was scanned; tazel help is the rest.</summary> - private static readonly Dictionary<string, Func<Scanned, OutputOptions, Command>> Makers = new(StringComparer.Ordinal) - { - ["build"] = (scanned, output) => new BuildCommand(output, scanned.Positional, Flags(scanned.Seen)), - ["test"] = (scanned, output) => new TestCommand(output, scanned.Positional, Flags(scanned.Seen)), - ["coverage"] = (scanned, output) => new CoverageCommand(output, scanned.Positional, Flags(scanned.Seen)), - ["run"] = MakeRun, - ["query"] = (scanned, output) => MakeQuery(scanned, output, QueryKind.Query), - ["cquery"] = (scanned, output) => MakeQuery(scanned, output, QueryKind.CQuery), - ["aquery"] = (scanned, output) => MakeQuery(scanned, output, QueryKind.AQuery), - ["explain"] = (scanned, output) => Explain(output, scanned.Positional, scanned.Has("short"), scanned.Has("long")), - ["fetch"] = (scanned, output) => Bare(scanned, new FetchCommand(output)), - ["vendor"] = (scanned, output) => Bare(scanned, new VendorCommand(output)), - ["portal-modules"] = (scanned, output) => PortalModules(output, scanned.Positional, scanned.Has("exact")), - ["clean"] = (scanned, output) => Bare(scanned, new CleanCommand(output, scanned.Has("expunge"), scanned.Has("dry-run"))), - ["package"] = (scanned, output) => Bare(scanned, new PackageCommand(output, scanned.Has("list"), scanned.Has("allow-dirty"), Last(scanned.Seen, "output-dir"))), - ["custom"] = (scanned, output) => scanned.Positional.Count == 0 - ? throw new CommandLineException("tazel custom needs the name of a command of tazel_custom.txt") - : new CustomCommandLine(output, scanned.Positional), - ["version"] = MakeVersion, - ["license"] = MakeLicense, - ["shell-completion"] = MakeShellCompletion, - // The one word after it is case-sensitive: C#, as the language is written. - ["limited-time"] = (scanned, output) => scanned.Positional is ["C#"] - ? new MigrateCommand(output) - : throw new CommandLineException("tazel limited-time takes one word, C#: tazel limited-time C#"), - ["dotnet"] = MakeDaemon, - ["options"] = MakeOptions, - ["tool"] = MakeTool, - ["ignore"] = MakeIgnore, - ["custom-cmds"] = (scanned, output) => MakeValidate(scanned, output, ConfigurationKind.Custom), - ["envs"] = (scanned, output) => MakeValidate(scanned, output, ConfigurationKind.Envs), - ["cache"] = MakeCache, - }; - - private static Command Bare(Scanned scanned, Command command) - { - NoPositional(scanned.Command, scanned.Positional); - return command; - } - - private static Command MakeRun(Scanned scanned, OutputOptions output) - { - var positional = scanned.Positional; - return positional.Count > 1 - ? throw new CommandLineException( - $"tazel run takes one target; the program's own arguments go after --: tazel run {positional[0]} -- {string.Join(' ', positional.Skip(1))}") - : new RunCommand(output, positional.FirstOrDefault(), Flags(scanned.Seen), scanned.AfterDashes); - } - - private static Command MakeQuery(Scanned scanned, OutputOptions output, QueryKind kind) - { - if (scanned.Positional.Count == 0) - { - throw new CommandLineException($"tazel {scanned.Command} needs an expression, such as deps(//app:greeter)"); - } - - var format = Last(scanned.Seen, "output") ?? "label"; - var allowed = new[] { "label", "label_kind" }; - if (!allowed.Contains(format, StringComparer.Ordinal)) - { - var near = Suggestions.Nearest(format, allowed); - throw new CommandLineException( - $"--output takes {string.Join(", ", allowed)}, not '{format}'" + - (near is null ? "" : $"; did you mean --output {near}?")); - } - - return new QueryCommand(output, kind, string.Join(' ', scanned.Positional), Flags(scanned.Seen), format); - } - - private static Command MakeVersion(Scanned scanned, OutputOptions output) - { - NoPositional(scanned.Command, scanned.Positional); - var raw = Last(scanned.Seen, "output"); - if (raw is null || raw == "text") - { - return new VersionCommand(output, scanned.Has("verbose"), false); - } - - if (raw == "json") - { - return new VersionCommand(output, scanned.Has("verbose"), true); - } - - var allowed = new[] { "text", "json" }; - var near = Suggestions.Nearest(raw, allowed); - throw new CommandLineException( - $"--output takes {string.Join(", ", allowed)}, not '{raw}'" + - (near is null ? "" : $"; did you mean --output {near}?")); - } - - private static Command MakeLicense(Scanned scanned, OutputOptions output) - { - NoPositional(scanned.Command, scanned.Positional); - var raw = Last(scanned.Seen, "output"); - if (raw is null) - { - return new LicenseCommand(output, LicenseFormat.Text); - } - - var allowed = new[] { "text", "md", "markdown", "rst" }; - return raw switch - { - "text" => new LicenseCommand(output, LicenseFormat.Text), - "md" or "markdown" => new LicenseCommand(output, LicenseFormat.Markdown), - "rst" => new LicenseCommand(output, LicenseFormat.RestructuredText), - _ => throw new CommandLineException( - $"--output takes {string.Join(", ", allowed)}, not '{raw}'" + - (Suggestions.Nearest(raw, allowed) is { } near ? $"; did you mean --output {near}?" : "")), - }; - } - - // --user, --project or --local: one at most; null for none. - private static Dsl.Configuration.LayerKind? Scope(Scanned scanned) - { - var given = new[] { ("user", Dsl.Configuration.LayerKind.User), ("project", Dsl.Configuration.LayerKind.Project), ("local", Dsl.Configuration.LayerKind.Local) } - .Where(flag => scanned.Has(flag.Item1)).ToList(); - return given.Count switch - { - 0 => null, - 1 => given[0].Item2, - _ => throw new CommandLineException($"--{given[0].Item1} and --{given[1].Item1} name two layers; give one"), - }; - } - - private static Command MakeOptions(Scanned scanned, OutputOptions output) - { - var positional = scanned.Positional; - var scope = Scope(scanned); - if (scanned.Has("create") && positional is not ["update", ..]) - { - throw new CommandLineException("--create is an option of tazel options update"); - } - - return positional switch - { - ["validate"] => new OptionsCommand(output, "validate", null, null, scope, false), - ["get", var name] => new OptionsCommand(output, "get", name, null, scope, false), - ["what-is", var name] => scope is null ? new OptionsCommand(output, "what-is", name, null, null, false) - : throw new CommandLineException("tazel options what-is reads no file, so it takes no layer"), - ["update", var name, var value] => scope == Dsl.Configuration.LayerKind.Local - ? throw new CommandLineException("tazel options update writes ./tazel_options.txt without a flag; --local is for reading") - : new OptionsCommand(output, "update", name, value, scope, scanned.Has("create")), - _ => throw new CommandLineException( - "tazel options takes validate, get <setting>, what-is <setting> or update <setting> <value>" + - (positional.Count > 0 && Suggestions.Nearest(positional[0], ["validate", "get", "what-is", "update"]) is { } near ? $"; did you mean {near}?" : "")), - }; - } - - private static Command MakeIgnore(Scanned scanned, OutputOptions output) => scanned.Positional switch - { - ["validate"] => new IgnoreCommand(output, "validate", null), - ["create-from" or "add", var argument] => new IgnoreCommand(output, scanned.Positional[0], argument), - ["delete" or "del", var argument] => new IgnoreCommand(output, "delete", argument), - [var action, ..] when action is "create-from" or "add" or "delete" or "del" => - throw new CommandLineException($"tazel ignore {action} takes one {(action == "create-from" ? "path" : "pattern")}; quote a pattern with spaces"), - _ => throw new CommandLineException("tazel ignore takes create-from <path>, validate, add <pattern>, delete <pattern> or del <pattern>" + - (scanned.Positional.Count > 0 && Suggestions.Nearest(scanned.Positional[0], ["create-from", "validate", "add", "delete"]) is { } near - ? $"; did you mean {near}?" : "")), - }; - - private static Command MakeTool(Scanned scanned, OutputOptions output) - { - var positional = scanned.Positional; - var directory = Last(scanned.Seen, "output-dir"); - if (directory is not null && positional is not ["create-wrapper", ..]) - { - throw new CommandLineException("-o and --output-dir are options of tazel tool create-wrapper"); - } - - return positional switch - { - ["launch", var name, .. var rest] => new ToolLaunchCommand(output, name, rest), - ["create-wrapper", var name] => new ToolWrapperCommand(output, name, directory), - ["launch"] or ["create-wrapper"] => throw new CommandLineException($"tazel tool {positional[0]} needs the name of a tool"), - ["create-wrapper", ..] => throw new CommandLineException("tazel tool create-wrapper takes one name"), - _ => throw new CommandLineException("tazel tool takes launch <name> [arguments] or create-wrapper <name> [-o dir]" + - (positional.Count > 0 && Suggestions.Nearest(positional[0], ["launch", "create-wrapper"]) is { } near ? $"; did you mean {near}?" : "")), - }; - } - - private static Command MakeValidate(Scanned scanned, OutputOptions output, ConfigurationKind kind) => scanned.Positional is ["validate"] - ? new ValidateCommand(output, kind, Scope(scanned)) - : throw new CommandLineException($"tazel {scanned.Command} takes validate"); - - private static Command MakeDaemon(Scanned scanned, OutputOptions output) - { - var actions = new[] { "start-daemon", "restart-daemon", "stop-daemon", "get", "what-is" }; - var positional = scanned.Positional; - if (positional is ["get", var setting]) - { - return new DotnetGetCommand(output, setting); - } - - if (positional is ["what-is", var term]) - { - return new DotnetWhatIsCommand(output, term); - } - - if (positional is ["get" or "what-is", ..]) - { - throw new CommandLineException($"tazel dotnet {positional[0]} takes one {(positional[0] == "get" ? "setting" : "term")}"); - } - - if (positional.Count == 1 && actions.Contains(positional[0], StringComparer.Ordinal)) - { - return new DaemonCommand(output, positional[0]); - } - - var near = positional.Count == 1 ? Suggestions.Nearest(positional[0], actions) : null; - throw new CommandLineException( - $"tazel dotnet takes get, what-is, start-daemon, restart-daemon or stop-daemon{(near is null ? "" : $"; did you mean {near}?")}"); - } - - private static Command MakeCache(Scanned scanned, OutputOptions output) - { - if (scanned.Positional is ["validate" or "repair"]) - { - return Last(scanned.Seen, "algorithm") is not null - ? throw new CommandLineException("--algorithm is an option of tazel cache keygen") - : new CacheCheckCommand(output, scanned.Positional[0] == "repair", scanned.Has("remote")); - } - - if (scanned.Positional is not ["keygen"]) - { - throw new CommandLineException("tazel cache takes keygen [--algorithm ed25519|rsa-4096|ml-dsa], validate [--remote] or repair [--remote]"); - } - - if (scanned.Has("remote")) - { - throw new CommandLineException("--remote is an option of tazel cache validate and repair"); - } - - var algorithm = Last(scanned.Seen, "algorithm") switch - { - "rsa-4096" => Cache.SigningAlgorithm.Rsa4096, - "ml-dsa" => Cache.SigningAlgorithm.MlDsa65, - _ => Cache.SigningAlgorithm.Ed25519, - }; - return new CacheKeygenCommand(output, algorithm); - } - - private static Command MakeHelp(Scanned scanned, OutputOptions output) - { - if (scanned.Positional.Count > 1) - { - throw new CommandLineException("tazel help takes one command"); - } - - if (scanned.Has("short") && scanned.Has("long")) - { - throw new CommandLineException("--short and --long cannot be used together"); - } - - var length = scanned.Has("short") ? HelpLength.Short : scanned.Has("long") ? HelpLength.Long : HelpLength.Medium; - return new HelpCommand(output, scanned.Positional.FirstOrDefault(), length); + return new ScannedCommand(command ?? "help", positional, afterDashes, seen); } /// <summary>Splits a list at commas; <c>\,</c> is a comma of a value.</summary> @@ -649,6 +221,28 @@ internal static class CommandLine return items; } + /// <summary> + /// The message format a command line asks for, read on its own for a command line that cannot be taken: the last + /// <c>--message-format</c> before <c>--</c> with a value Tazel knows, else rich-stdio. + /// </summary> + public static MessageFormat FormatOf(IReadOnlyList<string> arguments) + { + ArgumentNullException.ThrowIfNull(arguments); + var format = MessageFormat.RichStdio; + for (var i = 0; i < arguments.Count && arguments[i] != "--"; i++) + { + const string Name = "--message-format"; + var argument = arguments[i]; + string? value = argument == Name && i + 1 < arguments.Count ? arguments[i + 1] + : argument.StartsWith(Name, StringComparison.Ordinal) && argument.Length > Name.Length && + argument[Name.Length] is '=' or ':' ? argument[(Name.Length + 1)..] + : null; + format = ParseFormat(value) ?? format; + } + + return format; + } + private static CommandLineException UnknownOption(string name, string rest) { var near = Suggestions.Nearest(name, Options.Select(o => o.Long)); @@ -656,28 +250,28 @@ internal static class CommandLine } private static void ReadLong( - string argument, IReadOnlyList<string> arguments, ref int index, List<(Option, List<string>, string)> seen) + string argument, IReadOnlyList<string> arguments, ref int index, List<(OptionSpec, List<string>, string)> seen) { var body = argument[2..]; var separator = body.IndexOfAny(['=', ':']); var name = separator < 0 ? body : body[..separator]; var option = Options.FirstOrDefault(o => o.Long == name) ?? throw UnknownOption(name, separator < 0 ? "" : body[separator..]); List<string> values; - if (option.Kind == Kind.Flag) + if (option.Kind == OptionKind.Flag) { values = separator < 0 ? [] : throw new CommandLineException($"--{name} takes no value"); } else if (separator >= 0) { var text = body[(separator + 1)..]; - values = option.Kind is Kind.List or Kind.ListAlways ? new List<string>(SplitList(text)) : new List<string> { text }; + values = option.Kind is OptionKind.List or OptionKind.ListAlways ? new List<string>(SplitList(text)) : new List<string> { text }; Check(option, values, $"--{name}{body[separator]}"); } else { // Whitespace gives one value, whatever it holds. values = index >= arguments.Count ? throw new CommandLineException($"--{name} needs a value") - : option.Kind == Kind.ListAlways ? [.. SplitList(arguments[index++])] : [arguments[index++]]; + : option.Kind == OptionKind.ListAlways ? [.. SplitList(arguments[index++])] : [arguments[index++]]; Check(option, values, $"--{name} "); } @@ -690,7 +284,7 @@ internal static class CommandLine } private static void ReadShort( - string argument, IReadOnlyList<string> arguments, ref int index, List<(Option, List<string>, string)> seen) + string argument, IReadOnlyList<string> arguments, ref int index, List<(OptionSpec, List<string>, string)> seen) { if (argument == "-o" || argument.StartsWith("-o", StringComparison.Ordinal)) { @@ -738,7 +332,7 @@ internal static class CommandLine throw new CommandLineException($"{argument} is not an option; tazel help lists them"); } - private static void Check(Option option, List<string> values, string written) + private static void Check(OptionSpec option, List<string> values, string written) { if (option.Values is null) { @@ -757,34 +351,6 @@ internal static class CommandLine } } - private static string? Last(List<(Option Option, List<string> Values, string)> seen, string name) => - seen.LastOrDefault(entry => entry.Option.Long == name).Values?.LastOrDefault(); - - private static IEnumerable<string> All(List<(Option Option, List<string> Values, string)> seen, string name) => - seen.Where(entry => entry.Option.Long == name).SelectMany(entry => entry.Values); - - /// <summary> - /// The message format a command line asks for, read on its own for a command line that cannot be taken: the last - /// <c>--message-format</c> before <c>--</c> with a value Tazel knows, else rich-stdio. - /// </summary> - public static MessageFormat FormatOf(IReadOnlyList<string> arguments) - { - ArgumentNullException.ThrowIfNull(arguments); - var format = MessageFormat.RichStdio; - for (var i = 0; i < arguments.Count && arguments[i] != "--"; i++) - { - const string Name = "--message-format"; - var argument = arguments[i]; - string? value = argument == Name && i + 1 < arguments.Count ? arguments[i + 1] - : argument.StartsWith(Name, StringComparison.Ordinal) && argument.Length > Name.Length && - argument[Name.Length] is '=' or ':' ? argument[(Name.Length + 1)..] - : null; - format = ParseFormat(value) ?? format; - } - - return format; - } - private static MessageFormat? ParseFormat(string? text) => text switch { "rich-stdio" => MessageFormat.RichStdio, @@ -802,134 +368,6 @@ internal static class CommandLine _ => null, }; - private static BuildFlags Flags(List<(Option Option, List<string> Values, string)> seen) - { - int? jobs = null; - if (Last(seen, "jobs") is { } jobsText) - { - jobs = int.TryParse(jobsText, NumberStyles.None, CultureInfo.InvariantCulture, out var parsed) && parsed is > 0 and <= 4096 - ? parsed - : throw new CommandLineException($"--jobs takes a number from 1 to 4096, not '{jobsText}'"); - } - - Platform? platform = Last(seen, "platform") is { } name && Platforms.TryParse(name, out var found) ? found : null; - var flags = new BuildFlags - { - Mode = Last(seen, "mode") switch - { - "debug" => BuildMode.Debug, - "release" => BuildMode.Release, - _ => null, - }, - Platform = platform, - Jobs = jobs, - KeepGoing = seen.Any(entry => entry.Option.Long == "keep-going"), - Recompile = seen.Any(entry => entry.Option.Long == "recompile"), - NoCache = seen.Any(entry => entry.Option.Long == "no-cache"), - }; - flags.FeaturesOn.AddRange(All(seen, "feature")); - flags.FeaturesOff.AddRange(All(seen, "no-feature")); - flags.CompilerOptions.AddRange(All(seen, "compiler-options")); - flags.CCompilerOptions.AddRange(All(seen, "c-compiler-options")); - flags.CppCompilerOptions.AddRange(All(seen, "cpp-compiler-options")); - flags.LinkerOptions.AddRange(All(seen, "linker-options")); - flags.ZigCompilerOptions.AddRange(All(seen, "zig-compiler-options")); - return flags; - } - - /// <summary>A query is module or group:module, and group:module with --exact.</summary> - private static void CheckModuleQuery(string action, string query, bool exact) - { - var parts = query.Split(':'); - if (exact && (parts.Length != 2 || parts[0].Length == 0)) - { - throw new CommandLineException( - $"with -e or --exact, tazel portal-modules {action} needs the group too: group:{query.TrimStart(':')}"); - } - - if (parts.Length > 2 || parts.Any(part => part.Length == 0)) - { - throw new CommandLineException($"'{query}' is not module or group:module"); - } - } - - private static PortalModulesCommand PortalModules(OutputOptions output, List<string> positional, bool exact) - { - var actions = new[] { "search", "info" }; - if (positional.Count == 0 || !actions.Contains(positional[0], StringComparer.Ordinal)) - { - var near = positional.Count > 0 ? Suggestions.Nearest(positional[0], actions) : null; - throw new CommandLineException( - $"tazel portal-modules takes search or info{(near is null ? "" : $"; did you mean {near}?")}"); - } - - if (positional.Count != 2) - { - throw new CommandLineException($"tazel portal-modules {positional[0]} takes one module: module or group:module"); - } - - var query = positional[1]; - CheckModuleQuery(positional[0], query, exact); - return new PortalModulesCommand(output, positional[0] == "search" ? PortalAction.Search : PortalAction.Info, query, exact); - } - - private static Command MakeShellCompletion(Scanned scanned, OutputOptions output) - { - if (scanned.Positional.Count != 1) - { - throw new CommandLineException("tazel shell-completion takes one shell: bash, zsh, fish or powershell"); - } - - var raw = scanned.Positional[0]; - if (ShellCompletion.TryParse(raw, out var shell)) - { - return new ShellCompletionCommand(output, shell); - } - - var near = raw.Equals("pwsh", StringComparison.OrdinalIgnoreCase) || raw.Equals("ps", StringComparison.OrdinalIgnoreCase) - ? "powershell" - : Suggestions.Nearest(raw, ShellCompletion.Names); - throw new CommandLineException( - $"tazel shell-completion takes bash, zsh, fish or powershell, not '{raw}'" + - (near is null ? "" : $"; did you mean {near}?")); - } - - private static ExplainCommand Explain(OutputOptions output, List<string> positional, bool @short, bool @long) - { - if (@short && @long) - { - throw new CommandLineException("--short and --long cannot be used together"); - } - - if (positional.Count != 1) - { - throw new CommandLineException("tazel explain takes one error code, such as tazel explain E0406"); - } - - var code = positional[0].ToUpperInvariant(); - if (!ErrorCodes.IsCode(code)) - { - throw new CommandLineException($"'{positional[0]}' is not an error code: E and four digits, such as E0406"); - } - - if (ErrorCodes.Find(code) is null) - { - var near = Suggestions.Nearest(code, ErrorCodes.Codes.Select(entry => entry.Code).ToList()); - throw new CommandLineException($"Tazel has no error {code}{(near is null ? "" : $"; did you mean {near}?")}"); - } - - var length = @short ? HelpLength.Short : @long ? HelpLength.Long : HelpLength.Medium; - return new ExplainCommand(output, code, length); - } - - private static void NoPositional(string command, List<string> positional) - { - if (positional.Count > 0) - { - throw new CommandLineException($"tazel {command} takes no '{positional[0]}'"); - } - } - private static CommandLineException UnknownCommand(string word) { var near = Suggestions.Nearest(word, Commands); diff --git a/src/Progmasoft.Tazel/Cli/CommandMakers.cs b/src/Progmasoft.Tazel/Cli/CommandMakers.cs new file mode 100644 index 0000000..c75182f --- /dev/null +++ b/src/Progmasoft.Tazel/Cli/CommandMakers.cs @@ -0,0 +1,404 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using Progmasoft.Tazel.Dsl.Common; +using Progmasoft.Tazel.Output; + +namespace Progmasoft.Tazel.Cli; + +/// <summary>Constructs strongly typed <see cref="Command"/> instances from a <see cref="ScannedCommand"/>.</summary> +internal static class CommandMakers +{ + private static readonly Dictionary<string, Func<ScannedCommand, OutputOptions, Command>> Makers = new(StringComparer.Ordinal) + { + ["build"] = (scanned, output) => new BuildCommand(output, scanned.Positional, Flags(scanned)), + ["test"] = (scanned, output) => new TestCommand(output, scanned.Positional, Flags(scanned)), + ["coverage"] = (scanned, output) => new CoverageCommand(output, scanned.Positional, Flags(scanned)), + ["run"] = MakeRun, + ["query"] = (scanned, output) => MakeQuery(scanned, output, QueryKind.Query), + ["cquery"] = (scanned, output) => MakeQuery(scanned, output, QueryKind.CQuery), + ["aquery"] = (scanned, output) => MakeQuery(scanned, output, QueryKind.AQuery), + ["explain"] = (scanned, output) => Explain(output, scanned.Positional, scanned.Has("short"), scanned.Has("long")), + ["fetch"] = (scanned, output) => Bare(scanned, new FetchCommand(output)), + ["vendor"] = (scanned, output) => Bare(scanned, new VendorCommand(output)), + ["portal-modules"] = (scanned, output) => PortalModules(output, scanned.Positional, scanned.Has("exact")), + ["clean"] = (scanned, output) => Bare(scanned, new CleanCommand(output, scanned.Has("expunge"), scanned.Has("dry-run"))), + ["package"] = (scanned, output) => Bare(scanned, new PackageCommand(output, scanned.Has("list"), scanned.Has("allow-dirty"), scanned.Last("output-dir"))), + ["custom"] = (scanned, output) => scanned.Positional.Count == 0 + ? throw new CommandLineException("tazel custom needs the name of a command of tazel_custom.txt") + : new CustomCommandLine(output, scanned.Positional), + ["version"] = MakeVersion, + ["license"] = MakeLicense, + ["shell-completion"] = MakeShellCompletion, + // The one word after it is case-sensitive: C#, as the language is written. + ["limited-time"] = (scanned, output) => scanned.Positional is ["C#"] + ? new MigrateCommand(output) + : throw new CommandLineException("tazel limited-time takes one word, C#: tazel limited-time C#"), + ["dotnet"] = MakeDaemon, + ["options"] = MakeOptions, + ["tool"] = MakeTool, + ["ignore"] = MakeIgnore, + ["custom-cmds"] = (scanned, output) => MakeValidate(scanned, output, ConfigurationKind.Custom), + ["envs"] = (scanned, output) => MakeValidate(scanned, output, ConfigurationKind.Envs), + ["cache"] = MakeCache, + }; + + /// <summary>Dispatches a scanned command to its typed maker, defaulting to <c>tazel help</c>.</summary> + public static Command Build(ScannedCommand scanned, OutputOptions output) => + Makers.TryGetValue(scanned.Command, out var make) ? make(scanned, output) : MakeHelp(scanned, output); + + private static Command Bare(ScannedCommand scanned, Command command) + { + NoPositional(scanned.Command, scanned.Positional); + return command; + } + + private static Command MakeRun(ScannedCommand scanned, OutputOptions output) + { + var positional = scanned.Positional; + return positional.Count > 1 + ? throw new CommandLineException( + $"tazel run takes one target; the program's own arguments go after --: tazel run {positional[0]} -- {string.Join(' ', positional.Skip(1))}") + : new RunCommand(output, positional.FirstOrDefault(), Flags(scanned), scanned.AfterDashes); + } + + private static Command MakeQuery(ScannedCommand scanned, OutputOptions output, QueryKind kind) + { + if (scanned.Positional.Count == 0) + { + throw new CommandLineException($"tazel {scanned.Command} needs an expression, such as deps(//app:greeter)"); + } + + var format = scanned.Last("output") ?? "label"; + var allowed = new[] { "label", "label_kind" }; + if (!allowed.Contains(format, StringComparer.Ordinal)) + { + var near = Suggestions.Nearest(format, allowed); + throw new CommandLineException( + $"--output takes {string.Join(", ", allowed)}, not '{format}'" + + (near is null ? "" : $"; did you mean --output {near}?")); + } + + return new QueryCommand(output, kind, string.Join(' ', scanned.Positional), Flags(scanned), format); + } + + private static Command MakeVersion(ScannedCommand scanned, OutputOptions output) + { + NoPositional(scanned.Command, scanned.Positional); + var raw = scanned.Last("output"); + if (raw is null || raw == "text") + { + return new VersionCommand(output, scanned.Has("verbose"), false); + } + + if (raw == "json") + { + return new VersionCommand(output, scanned.Has("verbose"), true); + } + + var allowed = new[] { "text", "json" }; + var near = Suggestions.Nearest(raw, allowed); + throw new CommandLineException( + $"--output takes {string.Join(", ", allowed)}, not '{raw}'" + + (near is null ? "" : $"; did you mean --output {near}?")); + } + + private static Command MakeLicense(ScannedCommand scanned, OutputOptions output) + { + NoPositional(scanned.Command, scanned.Positional); + var raw = scanned.Last("output"); + if (raw is null) + { + return new LicenseCommand(output, LicenseFormat.Text); + } + + var allowed = new[] { "text", "md", "markdown", "rst" }; + return raw switch + { + "text" => new LicenseCommand(output, LicenseFormat.Text), + "md" or "markdown" => new LicenseCommand(output, LicenseFormat.Markdown), + "rst" => new LicenseCommand(output, LicenseFormat.RestructuredText), + _ => throw new CommandLineException( + $"--output takes {string.Join(", ", allowed)}, not '{raw}'" + + (Suggestions.Nearest(raw, allowed) is { } near ? $"; did you mean --output {near}?" : "")), + }; + } + + // --user, --project or --local: one at most; null for none. + private static Dsl.Configuration.LayerKind? Scope(ScannedCommand scanned) + { + var given = new[] { ("user", Dsl.Configuration.LayerKind.User), ("project", Dsl.Configuration.LayerKind.Project), ("local", Dsl.Configuration.LayerKind.Local) } + .Where(flag => scanned.Has(flag.Item1)).ToList(); + return given.Count switch + { + 0 => null, + 1 => given[0].Item2, + _ => throw new CommandLineException($"--{given[0].Item1} and --{given[1].Item1} name two layers; give one"), + }; + } + + private static Command MakeOptions(ScannedCommand scanned, OutputOptions output) + { + var positional = scanned.Positional; + var scope = Scope(scanned); + if (scanned.Has("create") && positional is not ["update", ..]) + { + throw new CommandLineException("--create is an option of tazel options update"); + } + + return positional switch + { + ["validate"] => new OptionsCommand(output, "validate", null, null, scope, false), + ["get", var name] => new OptionsCommand(output, "get", name, null, scope, false), + ["what-is", var name] => scope is null ? new OptionsCommand(output, "what-is", name, null, null, false) + : throw new CommandLineException("tazel options what-is reads no file, so it takes no layer"), + ["update", var name, var value] => scope == Dsl.Configuration.LayerKind.Local + ? throw new CommandLineException("tazel options update writes ./tazel_options.txt without a flag; --local is for reading") + : new OptionsCommand(output, "update", name, value, scope, scanned.Has("create")), + _ => throw new CommandLineException( + "tazel options takes validate, get <setting>, what-is <setting> or update <setting> <value>" + + (positional.Count > 0 && Suggestions.Nearest(positional[0], ["validate", "get", "what-is", "update"]) is { } near ? $"; did you mean {near}?" : "")), + }; + } + + private static Command MakeIgnore(ScannedCommand scanned, OutputOptions output) => scanned.Positional switch + { + ["validate"] => new IgnoreCommand(output, "validate", null), + ["create-from" or "add", var argument] => new IgnoreCommand(output, scanned.Positional[0], argument), + ["delete" or "del", var argument] => new IgnoreCommand(output, "delete", argument), + [var action, ..] when action is "create-from" or "add" or "delete" or "del" => + throw new CommandLineException($"tazel ignore {action} takes one {(action == "create-from" ? "path" : "pattern")}; quote a pattern with spaces"), + _ => throw new CommandLineException("tazel ignore takes create-from <path>, validate, add <pattern>, delete <pattern> or del <pattern>" + + (scanned.Positional.Count > 0 && Suggestions.Nearest(scanned.Positional[0], ["create-from", "validate", "add", "delete"]) is { } near + ? $"; did you mean {near}?" : "")), + }; + + private static Command MakeTool(ScannedCommand scanned, OutputOptions output) + { + var positional = scanned.Positional; + var directory = scanned.Last("output-dir"); + if (directory is not null && positional is not ["create-wrapper", ..]) + { + throw new CommandLineException("-o and --output-dir are options of tazel tool create-wrapper"); + } + + return positional switch + { + ["launch", var name, .. var rest] => new ToolLaunchCommand(output, name, rest), + ["create-wrapper", var name] => new ToolWrapperCommand(output, name, directory), + ["launch"] or ["create-wrapper"] => throw new CommandLineException($"tazel tool {positional[0]} needs the name of a tool"), + ["create-wrapper", ..] => throw new CommandLineException("tazel tool create-wrapper takes one name"), + _ => throw new CommandLineException("tazel tool takes launch <name> [arguments] or create-wrapper <name> [-o dir]" + + (positional.Count > 0 && Suggestions.Nearest(positional[0], ["launch", "create-wrapper"]) is { } near ? $"; did you mean {near}?" : "")), + }; + } + + private static Command MakeValidate(ScannedCommand scanned, OutputOptions output, ConfigurationKind kind) => scanned.Positional is ["validate"] + ? new ValidateCommand(output, kind, Scope(scanned)) + : throw new CommandLineException($"tazel {scanned.Command} takes validate"); + + private static Command MakeDaemon(ScannedCommand scanned, OutputOptions output) + { + var actions = new[] { "start-daemon", "restart-daemon", "stop-daemon", "get", "what-is" }; + var positional = scanned.Positional; + if (positional is ["get", var setting]) + { + return new DotnetGetCommand(output, setting); + } + + if (positional is ["what-is", var term]) + { + return new DotnetWhatIsCommand(output, term); + } + + if (positional is ["get" or "what-is", ..]) + { + throw new CommandLineException($"tazel dotnet {positional[0]} takes one {(positional[0] == "get" ? "setting" : "term")}"); + } + + if (positional.Count == 1 && actions.Contains(positional[0], StringComparer.Ordinal)) + { + return new DaemonCommand(output, positional[0]); + } + + var near = positional.Count == 1 ? Suggestions.Nearest(positional[0], actions) : null; + throw new CommandLineException( + $"tazel dotnet takes get, what-is, start-daemon, restart-daemon or stop-daemon{(near is null ? "" : $"; did you mean {near}?")}"); + } + + private static Command MakeCache(ScannedCommand scanned, OutputOptions output) + { + if (scanned.Positional is ["validate" or "repair"]) + { + return scanned.Last("algorithm") is not null + ? throw new CommandLineException("--algorithm is an option of tazel cache keygen") + : new CacheCheckCommand(output, scanned.Positional[0] == "repair", scanned.Has("remote")); + } + + if (scanned.Positional is not ["keygen"]) + { + throw new CommandLineException("tazel cache takes keygen [--algorithm ed25519|rsa-4096|ml-dsa], validate [--remote] or repair [--remote]"); + } + + if (scanned.Has("remote")) + { + throw new CommandLineException("--remote is an option of tazel cache validate and repair"); + } + + var algorithm = scanned.Last("algorithm") switch + { + "rsa-4096" => Cache.SigningAlgorithm.Rsa4096, + "ml-dsa" => Cache.SigningAlgorithm.MlDsa65, + _ => Cache.SigningAlgorithm.Ed25519, + }; + return new CacheKeygenCommand(output, algorithm); + } + + private static Command MakeHelp(ScannedCommand scanned, OutputOptions output) + { + if (scanned.Positional.Count > 1) + { + throw new CommandLineException("tazel help takes one command"); + } + + if (scanned.Has("short") && scanned.Has("long")) + { + throw new CommandLineException("--short and --long cannot be used together"); + } + + var length = scanned.Has("short") ? HelpLength.Short : scanned.Has("long") ? HelpLength.Long : HelpLength.Medium; + return new HelpCommand(output, scanned.Positional.FirstOrDefault(), length); + } + + private static BuildFlags Flags(ScannedCommand scanned) + { + int? jobs = null; + if (scanned.Last("jobs") is { } jobsText) + { + jobs = int.TryParse(jobsText, NumberStyles.None, CultureInfo.InvariantCulture, out var parsed) && parsed is > 0 and <= 4096 + ? parsed + : throw new CommandLineException($"--jobs takes a number from 1 to 4096, not '{jobsText}'"); + } + + Platform? platform = scanned.Last("platform") is { } name && Platforms.TryParse(name, out var found) ? found : null; + var flags = new BuildFlags + { + Mode = scanned.Last("mode") switch + { + "debug" => BuildMode.Debug, + "release" => BuildMode.Release, + _ => null, + }, + Platform = platform, + Jobs = jobs, + KeepGoing = scanned.Has("keep-going"), + Recompile = scanned.Has("recompile"), + NoCache = scanned.Has("no-cache"), + }; + flags.FeaturesOn.AddRange(scanned.All("feature")); + flags.FeaturesOff.AddRange(scanned.All("no-feature")); + flags.CompilerOptions.AddRange(scanned.All("compiler-options")); + flags.CCompilerOptions.AddRange(scanned.All("c-compiler-options")); + flags.CppCompilerOptions.AddRange(scanned.All("cpp-compiler-options")); + flags.LinkerOptions.AddRange(scanned.All("linker-options")); + flags.ZigCompilerOptions.AddRange(scanned.All("zig-compiler-options")); + return flags; + } + + /// <summary>A query is module or group:module, and group:module with --exact.</summary> + private static void CheckModuleQuery(string action, string query, bool exact) + { + var parts = query.Split(':'); + if (exact && (parts.Length != 2 || parts[0].Length == 0)) + { + throw new CommandLineException( + $"with -e or --exact, tazel portal-modules {action} needs the group too: group:{query.TrimStart(':')}"); + } + + if (parts.Length > 2 || parts.Any(part => part.Length == 0)) + { + throw new CommandLineException($"'{query}' is not module or group:module"); + } + } + + private static PortalModulesCommand PortalModules(OutputOptions output, List<string> positional, bool exact) + { + var actions = new[] { "search", "info" }; + if (positional.Count == 0 || !actions.Contains(positional[0], StringComparer.Ordinal)) + { + var near = positional.Count > 0 ? Suggestions.Nearest(positional[0], actions) : null; + throw new CommandLineException( + $"tazel portal-modules takes search or info{(near is null ? "" : $"; did you mean {near}?")}"); + } + + if (positional.Count != 2) + { + throw new CommandLineException($"tazel portal-modules {positional[0]} takes one module: module or group:module"); + } + + var query = positional[1]; + CheckModuleQuery(positional[0], query, exact); + return new PortalModulesCommand(output, positional[0] == "search" ? PortalAction.Search : PortalAction.Info, query, exact); + } + + private static Command MakeShellCompletion(ScannedCommand scanned, OutputOptions output) + { + if (scanned.Positional.Count != 1) + { + throw new CommandLineException("tazel shell-completion takes one shell: bash, zsh, fish or powershell"); + } + + var raw = scanned.Positional[0]; + if (ShellCompletion.TryParse(raw, out var shell)) + { + return new ShellCompletionCommand(output, shell); + } + + var near = raw.Equals("pwsh", StringComparison.OrdinalIgnoreCase) || raw.Equals("ps", StringComparison.OrdinalIgnoreCase) + ? "powershell" + : Suggestions.Nearest(raw, ShellCompletion.Names); + throw new CommandLineException( + $"tazel shell-completion takes bash, zsh, fish or powershell, not '{raw}'" + + (near is null ? "" : $"; did you mean {near}?")); + } + + private static ExplainCommand Explain(OutputOptions output, List<string> positional, bool @short, bool @long) + { + if (@short && @long) + { + throw new CommandLineException("--short and --long cannot be used together"); + } + + if (positional.Count != 1) + { + throw new CommandLineException("tazel explain takes one error code, such as tazel explain E0406"); + } + + var code = positional[0].ToUpperInvariant(); + if (!ErrorCodes.IsCode(code)) + { + throw new CommandLineException($"'{positional[0]}' is not an error code: E and four digits, such as E0406"); + } + + if (ErrorCodes.Find(code) is null) + { + var near = Suggestions.Nearest(code, ErrorCodes.Codes.Select(entry => entry.Code).ToList()); + throw new CommandLineException($"Tazel has no error {code}{(near is null ? "" : $"; did you mean {near}?")}"); + } + + var length = @short ? HelpLength.Short : @long ? HelpLength.Long : HelpLength.Medium; + return new ExplainCommand(output, code, length); + } + + private static void NoPositional(string command, List<string> positional) + { + if (positional.Count > 0) + { + throw new CommandLineException($"tazel {command} takes no '{positional[0]}'"); + } + } +} diff --git a/src/Progmasoft.Tazel/Cli/CommandTypes.cs b/src/Progmasoft.Tazel/Cli/CommandTypes.cs new file mode 100644 index 0000000..412f943 --- /dev/null +++ b/src/Progmasoft.Tazel/Cli/CommandTypes.cs @@ -0,0 +1,185 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System; +using System.Collections.Generic; +using System.Linq; +using Progmasoft.Tazel.Dsl.Common; +using Progmasoft.Tazel.Output; + +namespace Progmasoft.Tazel.Cli; + +/// <summary>A command line that cannot be taken, with a hint when there is one.</summary> +public sealed class CommandLineException : Exception +{ + /// <summary>What cannot be taken, and how to put it right.</summary> + public CommandLineException(string message) + : base(message) + { + } + + /// <summary>Nothing to say.</summary> + public CommandLineException() + { + } + + /// <summary>What cannot be taken, and why.</summary> + public CommandLineException(string message, Exception inner) + : base(message, inner) + { + } +} + +/// <summary>How Tazel prints, for every command.</summary> +internal sealed record OutputOptions(When Color, When Progress, bool Quiet, MessageFormat Format = MessageFormat.RichStdio); + +/// <summary>What <c>build</c>, <c>run</c> and <c>test</c> take beyond their labels.</summary> +internal sealed record BuildFlags +{ + public BuildMode? Mode { get; init; } + + public Platform? Platform { get; init; } + + public int? Jobs { get; init; } + + public bool KeepGoing { get; init; } + + /// <summary><c>--recompile</c>: every compilation runs.</summary> + public bool Recompile { get; init; } + + /// <summary><c>--no-cache</c>: every action and test runs, and no cache is read or written.</summary> + public bool NoCache { get; init; } + + public List<string> FeaturesOn { get; } = []; + + public List<string> FeaturesOff { get; } = []; + + public List<string> CompilerOptions { get; } = []; + + public List<string> CCompilerOptions { get; } = []; + + public List<string> CppCompilerOptions { get; } = []; + + public List<string> LinkerOptions { get; } = []; + + public List<string> ZigCompilerOptions { get; } = []; +} + +/// <summary>A command line, read into what it asks for; later stages do not look at its text again.</summary> +internal abstract record Command(OutputOptions Output) +{ + /// <summary>The settings of --dotnet-settings, Name=Value each, checked; for this Tazel process alone.</summary> + public IReadOnlyList<Runtime.DotnetSetting> DotnetSettings { get; init; } = []; +} + +internal sealed record BuildCommand(OutputOptions Output, IReadOnlyList<string> Labels, BuildFlags Flags) : Command(Output); + +internal sealed record TestCommand(OutputOptions Output, IReadOnlyList<string> Labels, BuildFlags Flags) : Command(Output); + +internal sealed record RunCommand(OutputOptions Output, string? Label, BuildFlags Flags, IReadOnlyList<string> Arguments) + : Command(Output); + +internal sealed record CleanCommand(OutputOptions Output, bool Expunge, bool DryRun) : Command(Output); + +internal sealed record PackageCommand(OutputOptions Output, bool List, bool AllowDirty, string? OutputDir) : Command(Output); + +internal sealed record CustomCommandLine(OutputOptions Output, IReadOnlyList<string> Words) : Command(Output); + +internal sealed record VersionCommand(OutputOptions Output, bool Verbose, bool Json) : Command(Output); + +/// <summary>How much <c>tazel help</c> or <c>tazel explain</c> prints: <c>--short</c>, medium (default), or <c>--long</c>.</summary> +internal enum HelpLength +{ + Short, + Medium, + Long, +} + +/// <summary>Output format of <c>tazel license</c>.</summary> +internal enum LicenseFormat +{ + Text, + Markdown, + RestructuredText, +} + +internal sealed record LicenseCommand(OutputOptions Output, LicenseFormat Format) : Command(Output); + +internal sealed record HelpCommand(OutputOptions Output, string? Topic, HelpLength Length = HelpLength.Medium) : Command(Output) +{ + public bool Long => Length == HelpLength.Long; +} + +internal sealed record CoverageCommand(OutputOptions Output, IReadOnlyList<string> Labels, BuildFlags Flags) : Command(Output); + +/// <summary>Which query: of the target graph, of the configured targets, or of the actions.</summary> +internal enum QueryKind +{ + Query, + CQuery, + AQuery, +} + +internal sealed record QueryCommand(OutputOptions Output, QueryKind Kind, string Expression, BuildFlags Flags, string Format) + : Command(Output); + +internal sealed record FetchCommand(OutputOptions Output) : Command(Output); + +internal sealed record ExplainCommand(OutputOptions Output, string Code, HelpLength Length = HelpLength.Medium) : Command(Output) +{ + public bool Long => Length == HelpLength.Long; +} + +/// <summary><c>tazel shell-completion bash|zsh|fish|powershell</c>: emits a shell completion script to stdout.</summary> +internal sealed record ShellCompletionCommand(OutputOptions Output, ShellKind Shell) : Command(Output); + +/// <summary><c>tazel dotnet get <setting></c>: what this process has for a .NET setting.</summary> +internal sealed record DotnetGetCommand(OutputOptions Output, string Setting) : Command(Output); + +/// <summary><c>tazel dotnet what-is <term></c>: what a term of .NET means.</summary> +internal sealed record DotnetWhatIsCommand(OutputOptions Output, string Term) : Command(Output); + +/// <summary><c>tazel limited-time C#</c>: the migration to BUILD.csx and labels in Depends, until Tazel 1.0.0.</summary> +internal sealed record MigrateCommand(OutputOptions Output) : Command(Output); + +internal sealed record VendorCommand(OutputOptions Output) : Command(Output); + +/// <summary>Searching Tazel Portal Modules, or the versions of one module.</summary> +internal enum PortalAction +{ + Search, + Info, +} + +internal sealed record PortalModulesCommand(OutputOptions Output, PortalAction Action, string Query, bool Exact) + : Command(Output); + +internal sealed record DaemonCommand(OutputOptions Output, string Action) : Command(Output); + +/// <summary><c>tazel cache keygen</c>: a key pair for signing the results of a remote cache.</summary> +internal sealed record CacheKeygenCommand(OutputOptions Output, Cache.SigningAlgorithm Algorithm) : Command(Output); + +internal enum OptionKind +{ + Flag, + Value, + List, + + // A list after whitespace too: --dotnet-settings A=1,B=2. + ListAlways, +} + +internal sealed record OptionSpec(string Long, OptionKind Kind, IReadOnlyList<string>? Values, params string[] Scope); + +/// <summary>The words of a command line, scanned into command name, positionals, after-dashes, and options.</summary> +internal sealed record ScannedCommand( + string Command, List<string> Positional, List<string> AfterDashes, List<(OptionSpec Option, List<string> Values, string Written)> Seen) +{ + public bool Has(string option) => Seen.Any(entry => entry.Option.Long == option); + + public string? Last(string name) => + Seen.LastOrDefault(entry => entry.Option.Long == name).Values?.LastOrDefault(); + + public IEnumerable<string> All(string name) => + Seen.Where(entry => entry.Option.Long == name).SelectMany(entry => entry.Values); +}