|
| 1 | +using System.Text.RegularExpressions; |
| 2 | +using GitVersion.Helpers; |
| 3 | + |
| 4 | +namespace GitVersion; |
| 5 | + |
| 6 | +internal static class ArgumentParserExtensions |
| 7 | +{ |
| 8 | + private static readonly string[] TrueValues = ["1", "true"]; |
| 9 | + private static readonly string[] FalseValues = ["0", "false"]; |
| 10 | + |
| 11 | + public static bool IsTrue(this string? value) => TrueValues.Contains(value, StringComparer.OrdinalIgnoreCase); |
| 12 | + |
| 13 | + public static bool IsFalse(this string? value) => FalseValues.Contains(value, StringComparer.OrdinalIgnoreCase); |
| 14 | + |
| 15 | + public static bool IsValidPath(this string? path) |
| 16 | + { |
| 17 | + if (path == null) |
| 18 | + return false; |
| 19 | + |
| 20 | + try |
| 21 | + { |
| 22 | + _ = PathHelper.GetFullPath(path); |
| 23 | + } |
| 24 | + catch |
| 25 | + { |
| 26 | + path = PathHelper.Combine(SysEnv.CurrentDirectory, path); |
| 27 | + |
| 28 | + try |
| 29 | + { |
| 30 | + _ = PathHelper.GetFullPath(path); |
| 31 | + } |
| 32 | + catch |
| 33 | + { |
| 34 | + return false; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return Directory.Exists(path); |
| 39 | + } |
| 40 | + |
| 41 | + public static bool IsSwitchArgument(this string? value) |
| 42 | + => value != null |
| 43 | + && (value.StartsWith('-') || value.StartsWith('/')) |
| 44 | + && !Regex.Match(value, @"/\w+:").Success; //Exclude msbuild & project parameters in form /blah:, which should be parsed as values, not switch names. |
| 45 | + |
| 46 | + public static bool IsSwitch(this string? value, string switchName) |
| 47 | + { |
| 48 | + if (value == null) |
| 49 | + return false; |
| 50 | + |
| 51 | + if (value.StartsWith('-')) |
| 52 | + { |
| 53 | + value = value[1..]; |
| 54 | + } |
| 55 | + |
| 56 | + if (value.StartsWith('/')) |
| 57 | + { |
| 58 | + value = value[1..]; |
| 59 | + } |
| 60 | + |
| 61 | + return string.Equals(switchName, value, StringComparison.OrdinalIgnoreCase); |
| 62 | + } |
| 63 | + |
| 64 | + public static bool IsHelp(this string singleArgument) => (singleArgument == "?") || singleArgument.IsSwitch("h") || singleArgument.IsSwitch("help") || singleArgument.IsSwitch("?"); |
| 65 | + |
| 66 | + public static bool ArgumentRequiresValue(this string argument, int argumentIndex) |
| 67 | + { |
| 68 | + var booleanArguments = new[] { "init", "updateassemblyinfo", "ensureassemblyinfo", "nofetch", "nonormalize", "nocache" }; |
| 69 | + |
| 70 | + var argumentMightRequireValue = !booleanArguments.Contains(argument[1..], StringComparer.OrdinalIgnoreCase); |
| 71 | + |
| 72 | + // If this is the first argument that might be a target path, the argument starts with slash and we're on an OS that supports paths with slashes, the argument does not require a value. |
| 73 | + if (argumentMightRequireValue && argumentIndex == 0 && argument.StartsWith('/') && Path.DirectorySeparatorChar == '/' && argument.IsValidPath()) |
| 74 | + return false; |
| 75 | + |
| 76 | + return argumentMightRequireValue; |
| 77 | + } |
| 78 | +} |
0 commit comments