-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathOption{T}.cs
More file actions
90 lines (78 loc) · 3.47 KB
/
Option{T}.cs
File metadata and controls
90 lines (78 loc) · 3.47 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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.CommandLine.Parsing;
namespace System.CommandLine
{
/// <inheritdoc cref="Option" />
/// <typeparam name="T">The <see cref="System.Type"/> that the option's arguments are expected to be parsed as.</typeparam>
public class Option<T> : Option
{
internal readonly Argument<T> _argument;
/// <summary>
/// Initializes a new instance of the <see cref="Option"/> class.
/// </summary>
/// <param name="name">The name of the option. This is used during parsing and is displayed in help.</param>
/// <param name="aliases">Optional aliases by which the option can be specified on the command line.</param>
public Option(string name, params string[] aliases)
: this(name, aliases, new Argument<T>(name))
{
}
private protected Option(string name, string[] aliases, Argument<T> argument)
: base(name, aliases)
{
argument.AddParent(this);
_argument = argument;
}
/// <inheritdoc cref="Argument{T}.DefaultValueFactory" />
public Func<ArgumentResult, T>? DefaultValueFactory
{
get => _argument.DefaultValueFactory;
set => _argument.DefaultValueFactory = value;
}
/// <inheritdoc cref="Argument{T}.CustomParser" />
public Func<ArgumentResult, T?>? CustomParser
{
get => _argument.CustomParser;
set => _argument.CustomParser = value;
}
internal sealed override Argument Argument => _argument;
/// <inheritdoc />
public override Type ValueType => _argument.ValueType;
/// <summary>
/// Configures the option to accept only the specified values, and to suggest them as command line completions.
/// </summary>
/// <param name="values">The values that are allowed for the option.</param>
public Option<T> AcceptOnlyFromAmong(params string[] values)
{
_argument.AcceptOnlyFromAmong(values);
return this;
}
/// <summary>
/// Configures the option to accept only the specified values using the specified comparer, and to suggest them as command line completions.
/// </summary>
/// <param name="comparer">The comparer used to match argument values against the allowed values.</param>
/// <param name="values">The values that are allowed for the option.</param>
public Option<T> AcceptOnlyFromAmong(StringComparer comparer, params string[] values)
{
_argument.AcceptOnlyFromAmong(comparer, values);
return this;
}
/// <summary>
/// Configures the option to accept only values representing legal file paths.
/// </summary>
public Option<T> AcceptLegalFilePathsOnly()
{
_argument.AcceptLegalFilePathsOnly();
return this;
}
/// <summary>
/// Configures the option to accept only values representing legal file names.
/// </summary>
/// <remarks>A parse error will result, for example, if file path separators are found in the parsed value.</remarks>
public Option<T> AcceptLegalFileNamesOnly()
{
_argument.AcceptLegalFileNamesOnly();
return this;
}
}
}