-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement PreviewProcessArguments class for argument handling a…
…nd parsing
- Loading branch information
1 parent
9b83c14
commit a05f503
Showing
3 changed files
with
150 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace AzureLiquid.Preview; | ||
|
||
/// <summary> | ||
/// Handles the arguments passed to the preview process. | ||
/// </summary> | ||
public class PreviewProcessArguments | ||
{ | ||
/// <summary> | ||
/// The current path of the process. | ||
/// </summary> | ||
private readonly string _path; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PreviewProcessArguments" /> class. | ||
/// </summary> | ||
public PreviewProcessArguments() => _path = Directory.GetCurrentDirectory(); | ||
|
||
/// <summary> | ||
/// Gets the index of the argument, if it exists. | ||
/// </summary> | ||
/// <param name="key">The key.</param> | ||
/// <returns>The index of the argument.</returns> | ||
private static int GetArgumentIndex(string[] args, string key) | ||
{ | ||
for (int i = 0; i < args?.Length; i++) | ||
{ | ||
if (IsArgMatch(args[i], key)) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the argument matches the partial argument key name. | ||
/// </summary> | ||
/// <param name="arg">The argument.</param> | ||
/// <param name="key">The key.</param> | ||
/// <returns> | ||
/// <c>true</c> if argument found; otherwise, <c>false</c>. | ||
/// </returns> | ||
private static bool IsArgMatch(string arg, string key) | ||
{ | ||
return string.CompareOrdinal(arg, "--" + key) == 0; | ||
} | ||
|
||
/// <summary> | ||
/// Parses the argument value. | ||
/// </summary> | ||
/// <param name="args">The arguments.</param> | ||
/// <param name="key">The key.</param> | ||
/// <returns>The argument value.</returns> | ||
public string ParsePath(string[] args, string key) | ||
{ | ||
var index = GetArgumentIndex(args, key); | ||
return | ||
index == -1 || index - 1 >= args?.Length || args == null ? string.Empty : // No match, or no arguments passed | ||
Path.GetFullPath(args[index + 1], _path) // Argument found, parsing path | ||
?? string.Empty; // Argument found, but path is invalid | ||
} | ||
} |
Oops, something went wrong.