-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathOptionResult.cs
91 lines (79 loc) · 3.73 KB
/
OptionResult.cs
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
// 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.Binding;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace System.CommandLine.Parsing
{
/// <summary>
/// A result produced when parsing an <see cref="Option" />.
/// </summary>
internal sealed class OptionResult : SymbolResult
{
private ArgumentConversionResult? _argumentConversionResult;
internal OptionResult(
CliOption option,
SymbolResultTree symbolResultTree,
CliToken? token = null,
CommandResult? parent = null) :
base(symbolResultTree, parent)
{
Option = option ?? throw new ArgumentNullException(nameof(option));
IdentifierToken = token;
}
private ValueResult? _valueResult;
public ValueResult ValueResult
{
get
{
if (_valueResult is null)
{
// This is not lazy on the assumption that almost everything the user enters will be used, and ArgumentResult is no longer used for defaults
// TODO: Make sure errors are added
var conversionValue = ArgumentConversionResult.Value;
var locations = Tokens.Select(token => token.Location).ToArray();
//TODO: Remove this wrapper later
_valueResult = new ValueResult(Option, conversionValue, locations, ArgumentResult.GetValueResultOutcome(ArgumentConversionResult.Result));
}
return _valueResult;
}
}
/// <summary>
/// The option to which the result applies.
/// </summary>
public CliOption Option { get; }
/// <summary>
/// Indicates whether the result was created implicitly and not due to the option being specified on the command line.
/// </summary>
/// <remarks>Implicit results commonly result from options having a default value.</remarks>
public bool Implicit => IdentifierToken is null || IdentifierToken.Implicit;
// TODO: make internal because exposes tokens
/// <summary>
/// The token that was parsed to specify the option.
/// </summary>
/// <remarks>An identifier token is a token that matches either the option's name or one of its aliases.</remarks>
internal CliToken? IdentifierToken { get; }
// TODO: do we even need IdentifierTokenCount
/*
/// <summary>
/// The number of occurrences of an identifier token matching the option.
/// </summary>
public int IdentifierTokenCount { get; internal set; }
*/
/// <inheritdoc/>
public override string ToString() => $"{nameof(OptionResult)}: {IdentifierToken?.Value ?? Option.Name} {string.Join(" ", Tokens.Select(t => t.Value))}";
/// <summary>
/// Gets the parsed value or the default value for <see cref="Option"/>.
/// </summary>
/// <returns>The parsed value or the default value for <see cref="Option"/></returns>
public T GetValueOrDefault<T>() =>
ArgumentConversionResult
.ConvertIfNeeded(typeof(T))
.GetValueOrDefault<T>();
internal bool IsArgumentLimitReached
=> Option.Argument.Arity.MaximumNumberOfValues == (Implicit ? Tokens.Count - 1 : Tokens.Count);
internal ArgumentConversionResult ArgumentConversionResult
=> _argumentConversionResult ??= GetResult(Option.Argument)!.GetArgumentConversionResult();
internal override bool UseDefaultValueFor(ArgumentResult argument) => Implicit;
}
}