-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathExample.cs
131 lines (116 loc) · 5.19 KB
/
Example.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
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
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CommandLine.Text
{
/// <summary>
/// Models a command line usage example.
/// </summary>
public sealed class Example : IEquatable<Example>
{
private readonly string helpText;
private readonly IEnumerable<UnParserSettings> formatStyles;
private readonly object sample;
/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.Text.Example"/> class.
/// </summary>
/// <param name="helpText">Example description.</param>
/// <param name="formatStyles">A <see cref="CommandLine.UnParserSettings"/> instances sequence that defines command line arguments format.</param>
/// <param name="sample">A sample instance.</param>
public Example(string helpText, IEnumerable<UnParserSettings> formatStyles, object sample)
{
if (string.IsNullOrEmpty(helpText)) throw new ArgumentException("helpText can't be null or empty", nameof(helpText));
if (formatStyles == null) throw new ArgumentNullException(nameof(formatStyles));
if (sample == null) throw new ArgumentNullException(nameof(sample));
this.helpText = helpText;
this.formatStyles = formatStyles;
this.sample = sample;
}
/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.Text.Example"/> class.
/// </summary>
/// <param name="helpText">Example description.</param>
/// <param name="formatStyle">A <see cref="CommandLine.UnParserSettings"/> instance that defines command line arguments format.</param>
/// <param name="sample">A sample instance.</param>
public Example(string helpText, UnParserSettings formatStyle, object sample)
: this(helpText, new[] { formatStyle }, sample)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.Text.Example"/> class.
/// </summary>
/// <param name="helpText">Example description.</param>
/// <param name="sample">A sample instance.</param>
public Example(string helpText, object sample)
: this(helpText, Enumerable.Empty<UnParserSettings>(), sample)
{
}
/// <summary>
/// Example description.
/// </summary>
public string HelpText
{
get { return helpText; }
}
/// <summary>
/// A sequence of format styles.
/// </summary>
public IEnumerable<UnParserSettings> FormatStyles
{
get { return this.formatStyles; }
}
/// <summary>
/// A sample instance.
/// </summary>
public object Sample
{
get { return sample; }
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="System.Object"/>.</param>
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
if (obj is Example other)
{
return Equals(other);
}
return base.Equals(obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <remarks>A hash code for the current <see cref="System.Object"/>.</remarks>
public override int GetHashCode()
{
return new { HelpText, FormatStyles, Sample }.GetHashCode();
}
/// <summary>
/// Returns a value that indicates whether the current instance and a specified <see cref="CommandLine.Text.Example"/> have the same value.
/// </summary>
/// <param name="other">The <see cref="CommandLine.Text.Example"/> instance to compare.</param>
/// <returns><value>true</value> if this instance of <see cref="CommandLine.Text.Example"/> and <paramref name="other"/> have the same value; otherwise, <value>false</value>.</returns>
public bool Equals(Example other)
{
if (other == null)
{
return false;
}
return HelpText.Equals(other.HelpText)
&& FormatStyles.SequenceEqual(other.FormatStyles)
&& Sample.Equals(other.Sample);
}
}
static class ExampleExtensions
{
public static IEnumerable<UnParserSettings> GetFormatStylesOrDefault(this Example example)
{
return example.FormatStyles.Any()
? example.FormatStyles
: new[] { new UnParserSettings { Consumed = true } };
}
}
}