-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathCmdLineInt.cs
More file actions
102 lines (84 loc) · 2.72 KB
/
Copy pathCmdLineInt.cs
File metadata and controls
102 lines (84 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Taken from https://cmdline.codeplex.com/
*
*/
//TODO: or with this https://github.com/natemcmaster/CommandLineUtils
namespace LogExpert.Classes.CommandLine;
/// <summary>
/// Represents an integer command line parameter.
/// </summary>
public class CmdLineInt : CmdLineParameter
{
#region Fields
private readonly int _max;
private readonly int _min;
#endregion
#region cTor
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of parameter.</param>
/// <param name="required">Require that the parameter is present in the command line.</param>
/// <param name="helpMessage">The explanation of the parameter to add to the help screen.</param>
public CmdLineInt (string name, bool required, string helpMessage)
: base(name, required, helpMessage)
{
_max = int.MaxValue;
_min = int.MinValue;
}
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of parameter.</param>
/// <param name="required">Require that the parameter is present in the command line.</param>
/// <param name="helpMessage">The explanation of the parameter to add to the help screen.</param>
/// <param name="min">The minimum value of the parameter.</param>
/// <param name="max">The maximum valie of the parameter.</param>
public CmdLineInt (string name, bool required, string helpMessage, int min, int max)
: base(name, required, helpMessage)
{
_max = min;
_max = max;
}
#endregion
#region Properties
/// <summary>
/// Returns the current value of the parameter.
/// </summary>
public new int Value { get; private set; }
#endregion
#region Public methods
/// <summary>
/// Sets the value of the parameter.
/// </summary>
/// <param name="value">A string containing a integer expression.</param>
public override void SetValue (string value)
{
base.SetValue(value);
int i;
try
{
i = Convert.ToInt32(value);
}
catch (Exception)
{
throw new CmdLineException(Name, "Value is not an integer.");
}
if (i < _min)
{
throw new CmdLineException(Name, $"Value must be greather or equal to {_min}.");
}
if (i > _max)
{
throw new CmdLineException(Name, $"Value must be less or equal to {_max}.");
}
Value = i;
}
/// <summary>
/// A implicit converion to a int data type.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static implicit operator int (CmdLineInt s) => s.Value;
#endregion
}