-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Arguments
class to reduce ExpressionMatch code size.
- Loading branch information
Showing
2 changed files
with
97 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace find2 | ||
{ | ||
internal sealed class Arguments | ||
{ | ||
public int Index { get; set; } | ||
|
||
private readonly string[]? _arguments; | ||
|
||
private string? _arg; | ||
|
||
public Arguments(string[]? arguments) | ||
{ | ||
_arguments = arguments; | ||
} | ||
|
||
public string? Get() | ||
{ | ||
_arg = Index >= _arguments!.Length ? null : _arguments[Index++]; | ||
return _arg; | ||
} | ||
|
||
public string GetValue() | ||
{ | ||
if (Index >= _arguments!.Length) | ||
{ | ||
throw new ArgumentNullException(_arg, $"Argument \"{_arg}\" requires a value."); | ||
} | ||
|
||
return _arguments[Index++]; | ||
} | ||
|
||
public string GetFileValue() | ||
{ | ||
var file = GetValue(); | ||
// TODO: Exception | ||
if (!File.Exists(file)) throw new FileNotFoundException(""); | ||
// TODO: If the file is a symbolic link and -H/-L are specified, follow the link. | ||
return file; | ||
} | ||
|
||
public int GetIntValue() | ||
{ | ||
var value = GetValue(); | ||
if (int.TryParse(value, out var intValue)) return intValue; | ||
throw new ArgumentOutOfRangeException(_arg, value, "Expected integral value."); | ||
} | ||
|
||
public int GetPositiveIntValue() | ||
{ | ||
var value = GetIntValue(); | ||
if (value >= 0) return value; | ||
throw new ArgumentOutOfRangeException(_arg, value, "Expected positive integral value."); | ||
} | ||
|
||
public int GetPositiveNonZeroIntValue() | ||
{ | ||
var value = GetPositiveIntValue(); | ||
if (value > 0) return value; | ||
throw new ArgumentOutOfRangeException(_arg, value, "Expected positive, non-zero, integral value."); | ||
} | ||
} | ||
} |
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