summaryrefslogtreecommitdiff
path: root/src/Agent.App/Cli/CommandLine.cs
blob: b2364f4521aa555ea1b8d266f0046ab91f2c272e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace Agent.App.Cli;

public enum AppCommand
{
    Start,
    Apply,
    Show
}

public sealed record CommandLine(
    AppCommand Command,
    string? ConfigPath,
    bool Yes = false)
{
    private static readonly HashSet<string> RemovedCommands = new(StringComparer.Ordinal)
    {
        "serve", "check", "executable", "sandbox"
    };

    public static CommandLine Parse(string[] args)
    {
        if (args.Length == 0) return new(AppCommand.Start, null);
        if (RemovedCommands.Contains(args[0]))
            throw new CommandLineException($"'{args[0]}' was removed in schema_version 2. Use 'agent-mcp', 'agent-mcp apply', or 'agent-mcp show'.");
        if (args.Any(static value => value is "--config" or "--lang" or "--transport" or "--path" or "--runtime-root" or "--no-runtime" or "--format"))
            throw new CommandLineException("Configuration override options were removed in schema_version 2. Put values in YAML and pass the config path as a positional argument.");

        AppCommand command = args[0] switch
        {
            "apply" => AppCommand.Apply,
            "show" => AppCommand.Show,
            _ when !args[0].StartsWith("--", StringComparison.Ordinal) => AppCommand.Start,
            _ => throw new CommandLineException($"Unknown option '{args[0]}'.")
        };
        int index = command == AppCommand.Start ? 0 : 1;
        string? config = null;
        bool yes = false;
        while (index < args.Length)
        {
            string value = args[index++];
            if (value == "--yes" && command == AppCommand.Apply && !yes) yes = true;
            else if (!value.StartsWith("--", StringComparison.Ordinal) && config is null) config = value;
            else throw new CommandLineException($"Unknown, duplicate, or invalid argument '{value}' for this command.");
        }
        return new(command, config, yes);
    }
}

public sealed class CommandLineException(string message) : Exception(message);