| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | public static partial class ProgramArgsExtensions |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Gets the conventional base-path value. |
| | 7 | | /// </summary> |
| | 8 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 9 | | public static string? GetBasePathValue(this ProgramArgs? args) |
| 5 | 10 | | { |
| 5 | 11 | | var isBasePathRequired = args.HasArg(ProgramArgs.BasePathRequired, requiresValue: false); |
| 5 | 12 | | if (!args.HasArg(ProgramArgs.BasePath, isBasePathRequired)) return null; |
| | 13 | |
|
| 5 | 14 | | var basePath = args.GetArgValue(ProgramArgs.BasePath); |
| 5 | 15 | | if (!Directory.Exists(basePath)) throw new ArgumentException($"{basePath} does not exist."); |
| | 16 | |
|
| 5 | 17 | | return basePath; |
| 5 | 18 | | } |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Gets the path to the output file specified by conventional arguments. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 24 | | /// <remarks> |
| | 25 | | /// This member will generate a file when it does not exist. |
| | 26 | | /// </remarks> |
| | 27 | | public static string? GetOutputPath(this ProgramArgs? args) |
| 4 | 28 | | { |
| 4 | 29 | | var outputFile = args.GetArgValue(ProgramArgs.OutputFile); |
| | 30 | |
|
| 4 | 31 | | if (args.HasArg(ProgramArgs.OutputUnderBasePath, requiresValue: false)) |
| 2 | 32 | | { |
| 2 | 33 | | var basePath = args.GetBasePathValue(); |
| | 34 | |
|
| 2 | 35 | | outputFile = ProgramFileUtility.GetCombinedPath(basePath, outputFile); |
| 2 | 36 | | } |
| | 37 | |
|
| 4 | 38 | | if (!File.Exists(outputFile)) File.WriteAllText(outputFile!, string.Empty); |
| | 39 | |
|
| 4 | 40 | | return outputFile; |
| 4 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the conventional settings file <see cref="string"/> content. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 47 | | public static string GetSettings(this ProgramArgs? args) |
| 1 | 48 | | { |
| 1 | 49 | | var path = args.GetSettingsFilePath(); |
| | 50 | |
|
| 1 | 51 | | if (File.Exists(path)) return File.ReadAllText(path); |
| | 52 | |
|
| 1 | 53 | | var basePath = args.GetBasePathValue(); |
| | 54 | |
|
| 1 | 55 | | path = ProgramFileUtility.GetCombinedPath(basePath, path, fileIsExpected: true); |
| | 56 | |
|
| 1 | 57 | | return File.ReadAllText(path); |
| 1 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Gets the conventional settings file path. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 64 | | public static string? GetSettingsFilePath(this ProgramArgs? args) |
| 2 | 65 | | { |
| 2 | 66 | | var settingsFileName = args.GetArgValue(ProgramArgs.SettingsFile); |
| | 67 | |
|
| 2 | 68 | | return settingsFileName; |
| 2 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Gets <see cref="string"/> input |
| | 73 | | /// from either <see cref="ProgramArgs.InputString"/> |
| | 74 | | /// or <see cref="ProgramArgs.InputFile"/>. |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 77 | | public static string GetStringInput(this ProgramArgs? args) |
| 3 | 78 | | { |
| 3 | 79 | | string? input = null; |
| | 80 | |
|
| 3 | 81 | | if (args.HasArg(ProgramArgs.InputString, requiresValue: false)) |
| 1 | 82 | | { |
| 1 | 83 | | input = args.GetArgValue(ProgramArgs.InputString); |
| 1 | 84 | | } |
| 2 | 85 | | else if (args.HasArg(ProgramArgs.InputFile, requiresValue: true)) |
| 2 | 86 | | { |
| 2 | 87 | | var path = args.GetArgValue(ProgramArgs.InputFile); |
| | 88 | |
|
| 2 | 89 | | if (!File.Exists(path)) |
| 1 | 90 | | { |
| 1 | 91 | | var basePath = args.GetBasePathValue(); |
| | 92 | |
|
| 1 | 93 | | path = ProgramFileUtility |
| 1 | 94 | | .GetCombinedPath(basePath, path, fileIsExpected: true); |
| | 95 | |
|
| 1 | 96 | | if (!File.Exists(path)) |
| 0 | 97 | | throw new FileNotFoundException($"The expected input file, `{path}`, is not here."); |
| 1 | 98 | | } |
| | 99 | |
|
| 2 | 100 | | input = File.ReadAllText(path); |
| 2 | 101 | | } |
| | 102 | |
|
| 3 | 103 | | return input.ToReferenceTypeValueOrThrow(); |
| 3 | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Determines whether args contain the <see cref="ProgramArgs.Help"/> flag. |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| 3 | 110 | | public static bool IsHelpRequest(this ProgramArgs? args) => args.HasArg(ProgramArgs.Help, requiresValue: false); |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Converts the <c>args</c> key to a conventional Configuration key. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 116 | | /// <param name="argKey">The arguments key.</param> |
| | 117 | | public static string ToConfigurationKey(this ProgramArgs? args, string argKey) |
| 0 | 118 | | { |
| 0 | 119 | | argKey.ThrowWhenNullOrWhiteSpace(); |
| | 120 | |
|
| 0 | 121 | | return argKey |
| 0 | 122 | | .TrimStart('-') |
| 0 | 123 | | .Replace("/", string.Empty) |
| 0 | 124 | | .Replace("=", string.Empty); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Returns <see cref="ProgramArgs"/> |
| | 129 | | /// </summary> |
| | 130 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 131 | | public static ProgramArgs? WithDefaultHelpText(this ProgramArgs? args) |
| 1 | 132 | | { |
| 1 | 133 | | if (args == null) return null; |
| | 134 | |
|
| 1 | 135 | | if (!args.HelpSet.ContainsKey(ProgramArgs.BasePath)) |
| 1 | 136 | | args.HelpSet.Add(ProgramArgs.BasePath, |
| 1 | 137 | | "The path to the Directory where the Activity will set its context."); |
| | 138 | |
|
| 1 | 139 | | if (!args.HelpSet.ContainsKey(ProgramArgs.BasePathRequired)) |
| 1 | 140 | | args.HelpSet.Add(ProgramArgs.BasePathRequired, $"Indicates that {ProgramArgs.BasePath} is required."); |
| | 141 | |
|
| 1 | 142 | | if (!args.HelpSet.ContainsKey(ProgramArgs.Help)) |
| 1 | 143 | | args.HelpSet.Add(ProgramArgs.Help, "Displays this help text."); |
| | 144 | |
|
| 1 | 145 | | if (!args.HelpSet.ContainsKey(ProgramArgs.InputFile)) |
| 1 | 146 | | args.HelpSet.Add(ProgramArgs.InputFile, |
| 1 | 147 | | $"The path to the file to load as Activity input. {ProgramArgs.InputString} can be used alternatively.") |
| | 148 | |
|
| 1 | 149 | | if (!args.HelpSet.ContainsKey(ProgramArgs.InputString)) |
| 1 | 150 | | args.HelpSet.Add(ProgramArgs.InputString, |
| 1 | 151 | | $"The string literal used as Activity input. {ProgramArgs.InputFile} can be used alternatively."); |
| | 152 | |
|
| 1 | 153 | | if (!args.HelpSet.ContainsKey(ProgramArgs.OutputFile)) |
| 1 | 154 | | args.HelpSet.Add(ProgramArgs.OutputFile, |
| 1 | 155 | | $"The path to the file to write as Activity output. This can be an absolute path or relative to {Program |
| | 156 | |
|
| 1 | 157 | | if (!args.HelpSet.ContainsKey(ProgramArgs.OutputUnderBasePath)) |
| 1 | 158 | | args.HelpSet.Add(ProgramArgs.OutputUnderBasePath, $"See {ProgramArgs.OutputFile}."); |
| | 159 | |
|
| 1 | 160 | | if (!args.HelpSet.ContainsKey(ProgramArgs.SettingsFile)) |
| 1 | 161 | | args.HelpSet.Add(ProgramArgs.SettingsFile, |
| 1 | 162 | | $"The path to the file to load as Activity Settings input. This can be an absolute path or relative to { |
| | 163 | |
|
| 1 | 164 | | return args; |
| 1 | 165 | | } |
| | 166 | |
|
| | 167 | | /// <summary> |
| | 168 | | /// Writes the <see cref="string"/> output |
| | 169 | | /// to the file specified by <see cref="ProgramArgs.OutputFile"/>. |
| | 170 | | /// </summary> |
| | 171 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 172 | | /// <param name="output">the output to write</param> |
| | 173 | | public static void WriteOutputToFile(this ProgramArgs? args, string output) |
| 2 | 174 | | { |
| 2 | 175 | | var outputFile = args.GetOutputPath(); |
| | 176 | |
|
| 2 | 177 | | File.WriteAllText(outputFile!, output); |
| 2 | 178 | | } |
| | 179 | |
|
| | 180 | | /// <summary> |
| | 181 | | /// Writes the <see cref="byte"/> array output |
| | 182 | | /// to the file specified by <see cref="ProgramArgs.OutputFile"/>. |
| | 183 | | /// </summary> |
| | 184 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 185 | | /// <param name="output">the output to write</param> |
| | 186 | | public static void WriteOutputToFile(this ProgramArgs? args, byte[] output) |
| 0 | 187 | | { |
| 0 | 188 | | var outputFile = args.GetOutputPath().ToReferenceTypeValueOrThrow(); |
| | 189 | |
|
| 0 | 190 | | File.WriteAllBytes(outputFile, output); |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | /// <summary> |
| | 194 | | /// Writes the <see cref="Stream"/> output |
| | 195 | | /// to the file specified by <see cref="ProgramArgs.OutputFile"/>. |
| | 196 | | /// </summary> |
| | 197 | | /// <param name="args">The <see cref="ProgramArgs"/>.</param> |
| | 198 | | /// <param name="stream">the <see cref="Stream"/> to write</param> |
| | 199 | | /// <remarks> |
| | 200 | | /// [ see https://stackoverflow.com/a/5515894/22944 ] |
| | 201 | | /// </remarks> |
| | 202 | | public static void WriteOutputToFile(this ProgramArgs? args, Stream stream) |
| 0 | 203 | | { |
| 0 | 204 | | var outputFile = args.GetOutputPath().ToReferenceTypeValueOrThrow(); |
| | 205 | |
|
| 0 | 206 | | using var fileStream = File.Create(outputFile); |
| | 207 | |
|
| 0 | 208 | | stream.Seek(0, SeekOrigin.Begin); |
| 0 | 209 | | stream.CopyTo(fileStream); |
| 0 | 210 | | } |
| | 211 | | } |