|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace PCL.Core.App.Cli; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// 命令行模型 |
| 8 | +/// </summary> |
| 9 | +public class CommandLine |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// 命令文本 |
| 13 | + /// </summary> |
| 14 | + public required string CommandText { get; init; } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// 子命令 |
| 18 | + /// </summary> |
| 19 | + public CommandLine? Subcommand { get; init; } = null; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// 子命令文本 |
| 23 | + /// </summary> |
| 24 | + public string? SubcommandText => Subcommand?.CommandText; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// 参数字典 |
| 28 | + /// </summary> |
| 29 | + public required IReadOnlyDictionary<string, CommandArgument> Arguments { get; init; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// 尝试获取参数值 |
| 33 | + /// </summary> |
| 34 | + /// <param name="key">参数键</param> |
| 35 | + /// <param name="value">参数值,若获取失败则为对应类型默认值</param> |
| 36 | + /// <typeparam name="TValue">参数值的类型</typeparam> |
| 37 | + /// <returns>是否获取成功,若不存在该键或值类型不匹配则失败</returns> |
| 38 | + public bool TryGetArgumentValue<TValue>(string key, out TValue? value) |
| 39 | + { |
| 40 | + var result = Arguments.TryGetValue(key, out var arg); |
| 41 | + if (result && arg!.TryCastValue(out TValue? typedValue)) |
| 42 | + { |
| 43 | + value = typedValue; |
| 44 | + return true; |
| 45 | + } |
| 46 | + value = default; |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// 尝试获取参数值 |
| 52 | + /// </summary> |
| 53 | + /// <param name="key">参数键</param> |
| 54 | + /// <typeparam name="TValue">参数值的类型</typeparam> |
| 55 | + /// <returns>参数值</returns> |
| 56 | + /// <exception cref="InvalidCastException">不存在该键或值类型不匹配</exception> |
| 57 | + public TValue? GetArgumentValue<TValue>(string key) |
| 58 | + { |
| 59 | + var result = TryGetArgumentValue(key, out TValue? value); |
| 60 | + return result ? value : throw new InvalidCastException($"Key '{key}' not found or value type mismatch"); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// 解析参数数组,第一个元素会被视为主命令 |
| 65 | + /// </summary> |
| 66 | + /// <param name="args">参数数组</param> |
| 67 | + /// <param name="subcommands">各级子命令列表</param> |
| 68 | + /// <returns>命令行模型实例</returns> |
| 69 | + public static CommandLine Parse(ReadOnlySpan<string> args, IEnumerable<SubcommandDefinition>? subcommands = null) |
| 70 | + { |
| 71 | + subcommands ??= []; |
| 72 | + SubcommandDefinition root = (args[0], subcommands); |
| 73 | + return CommandLineParser.Parse(args, root); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +file static class CommandLineParser |
| 78 | +{ |
| 79 | + private static (CommandArgument, bool) _ParseArgument(string key, string possibleValueText) |
| 80 | + { |
| 81 | + if (possibleValueText.Length == 0 || possibleValueText.StartsWith("--")) |
| 82 | + return (new BoolArgument { Key = key, ValueText = string.Empty }, false); |
| 83 | + if (possibleValueText.ToLowerInvariant() is "true" or "false") |
| 84 | + return (new BoolArgument { Key = key, ValueText = possibleValueText }, true); |
| 85 | + if (decimal.TryParse(possibleValueText, out var d)) |
| 86 | + return (new DecimalArgument { Key = key, ValueText = possibleValueText, Value = d }, true); |
| 87 | + return (new TextArgument { Key = key, ValueText = possibleValueText }, true); |
| 88 | + } |
| 89 | + |
| 90 | + public static CommandLine Parse(ReadOnlySpan<string> args, SubcommandDefinition subcommands) |
| 91 | + { |
| 92 | + if (args.IsEmpty) throw new ArgumentException("The argument span must contain at least 1 element", nameof(args)); |
| 93 | + var i = 1; |
| 94 | + var commandText = args[0]; |
| 95 | + var argumentList = new Dictionary<string, CommandArgument>(); |
| 96 | + CommandLine? subcommand = null; |
| 97 | + while (i < args.Length) |
| 98 | + { |
| 99 | + var currentText = args[i]; |
| 100 | + if (subcommands.Contains(currentText)) |
| 101 | + { |
| 102 | + subcommand = Parse(args[i..], subcommands.SubcommandMap[currentText]); |
| 103 | + break; |
| 104 | + } |
| 105 | + var (commandArgument, hasValueText) = _ParseArgument(currentText, (i == args.Length - 1) ? "" : args[i + 1]); |
| 106 | + argumentList[commandArgument.Key] = commandArgument; |
| 107 | + i += hasValueText ? 2 : 1; |
| 108 | + } |
| 109 | + return new CommandLine |
| 110 | + { |
| 111 | + CommandText = commandText, |
| 112 | + Arguments = argumentList.AsReadOnly(), |
| 113 | + Subcommand = subcommand |
| 114 | + }; |
| 115 | + } |
| 116 | +} |
0 commit comments