-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathRegexValueArgument.cs
More file actions
172 lines (155 loc) · 7.01 KB
/
RegexValueArgument.cs
File metadata and controls
172 lines (155 loc) · 7.01 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Text.RegularExpressions;
using CommandLineParser.Compatibility;
using CommandLineParser.Exceptions;
using Microsoft.Extensions.Logging;
namespace CommandLineParser.Arguments;
/// <summary>
/// Use RegexValueArgument for an argument whose value must match a regular expression.
/// </summary>
public class RegexValueArgument : CertifiedValueArgument<string>
{
/// <summary>
/// Regular expression which the value must match
/// </summary>
public Regex? Regex { get; set; }
/// <summary>
/// Sample value that would be displayed to the user as a suggestion when the user enters a wrong value.
/// </summary>
public string? SampleValue { get; set; }
#region constructor
/// <summary>
/// Creates new argument with a <see cref="Argument.ShortName">short name</see>,
/// <see cref="Argument.LongName">long name</see> and <see cref="Argument.Description">description</see>.
/// </summary>
/// <param name="shortName">Short name of the argument</param>
/// <param name="regex">regular expression which the value must match</param>
public RegexValueArgument(char shortName, Regex regex) : base(shortName)
{
Regex = regex;
}
/// <summary>
/// Creates new argument with a <see cref="Argument.ShortName">short name</see>,
/// <see cref="Argument.LongName">long name</see> and <see cref="Argument.Description">description</see>.
/// </summary>
/// <param name="longName">Long name of the argument </param>
/// <param name="regex">regular expression which the value must match</param>
public RegexValueArgument(string longName, Regex regex) : base(longName)
{
Regex = regex;
}
/// <summary>
/// Creates new argument with a <see cref="Argument.ShortName">short name</see>,
/// <see cref="Argument.LongName">long name</see> and <see cref="Argument.Description">description</see>.
/// </summary>
/// <param name="shortName">Short name of the argument</param>
/// <param name="longName">Long name of the argument </param>
/// <param name="regex">regular expression which the value must match</param>
public RegexValueArgument(char shortName, string longName, Regex regex) : base(shortName, longName)
{
Regex = regex;
}
/// <summary>
/// Creates new argument with a <see cref="Argument.ShortName">short name</see>,
/// <see cref="Argument.LongName">long name</see> and <see cref="Argument.Description">description</see>.
/// </summary>
/// <param name="shortName">Short name of the argument</param>
/// <param name="longName">Long name of the argument </param>
/// <param name="description">description of the argument</param>
/// <param name="regex">regular expression which the value must match</param>
public RegexValueArgument(char shortName, string longName, string description, Regex regex) : base(shortName, longName, description)
{
Regex = regex;
}
#endregion
protected override void Certify(string value)
{
// override the Certify method to validate value against regex
if (Regex != null && !Regex.IsMatch(value))
{
if (SampleValue == null)
{
throw new CommandLineArgumentOutOfRangeException($"Argument '{value}' does not match the regex pattern '{Regex}'.", Name);
}
throw new CommandLineArgumentOutOfRangeException($"Argument '{value}' does not match the regex pattern '{Regex}'. An example of a valid value would be '{SampleValue}'.", Name);
}
}
}
/// <summary>
/// <para>
/// Attribute for declaring a class' field a <see cref="RegexValueArgumentAttribute"/> and
/// thus binding a field's value to a certain command line switch argument.
/// </para>
/// <para>
/// Instead of creating an argument explicitly, you can assign a class' field an argument
/// attribute and let the CommandLineParse take care of binding the attribute to the field.
/// </para>
/// </summary>
/// <remarks>Applicable to fields and properties (public).</remarks>
/// <remarks>Use <see cref="CommandLineParser.ExtractArgumentAttributes"/> for each object
/// you where you have declared argument attributes.</remarks>
public sealed class RegexValueArgumentAttribute : ArgumentAttribute
{
private readonly Type _argumentType;
/// <summary>
/// Creates new instance of RegexValueArgumentAttribute which uses underlying <see cref="RegexValueArgument"/>.
/// </summary>
/// <param name="shortName"><see cref="Argument.ShortName">short name</see> of the underlying argument</param>
/// <param name="pattern">Regex pattern</param>
public RegexValueArgumentAttribute(char shortName, string pattern)
: base(typeof(RegexValueArgument), shortName, new Regex(pattern))
{
_argumentType = typeof(RegexValueArgument);
}
/// <summary>
/// Creates new instance of RegexValueArgumentAttribute which uses underlying <see cref="RegexValueArgument"/>.
/// </summary>
/// <param name="longName"><see cref="Argument.LongName">short name</see> of the underlying argument</param>
/// <param name="pattern">Regex pattern</param>
public RegexValueArgumentAttribute(string longName, string pattern)
: base(typeof(RegexValueArgument), longName, new Regex(pattern))
{
_argumentType = typeof(RegexValueArgument);
}
/// <summary>
/// Creates new instance of RegexValueArgumentAttribute which uses underlying <see cref="RegexValueArgument"/>.
/// </summary>
/// <param name="shortName"><see cref="Argument.ShortName">short name</see> of the underlying argument</param>
/// <param name="longName"><see cref="Argument.LongName">long name</see> of the underlying argument</param>
/// <param name="pattern">Regex pattern</param>
public RegexValueArgumentAttribute(char shortName, string longName, string pattern)
: base(typeof(RegexValueArgument), shortName, longName, new Regex(pattern))
{
_argumentType = typeof(RegexValueArgument);
}
/// <summary>
/// Default value
/// </summary>
public object? DefaultValue
{
get => _argumentType.GetPropertyValue<object>("DefaultValue", Argument);
set => _argumentType.SetPropertyValue("DefaultValue", Argument, value);
}
/// <summary>
/// Sample value that would be displayed to the user as a suggestion when
/// the user enters a wrong value.
/// </summary>
public string? SampleValue
{
get => _argumentType.GetPropertyValue<string>("SampleValue", Argument);
set => _argumentType.SetPropertyValue("SampleValue", Argument, value);
}
/// <summary>
/// When set to true, argument can appear on the command line with or without value, e.g. both is allowed:
/// <code>
/// myexe.exe -Arg Value
/// OR
/// myexe.exe -Arg
/// </code>
/// </summary>
public bool ValueOptional
{
get => _argumentType.GetPropertyValue<bool>("ValueOptional", Argument);
set => _argumentType.SetPropertyValue("ValueOptional", Argument, value);
}
}