// 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
{
///
/// Models a command line usage example.
///
public sealed class Example : IEquatable
{
private readonly string helpText;
private readonly IEnumerable formatStyles;
private readonly object sample;
///
/// Initializes a new instance of the class.
///
/// Example description.
/// A instances sequence that defines command line arguments format.
/// A sample instance.
public Example(string helpText, IEnumerable formatStyles, object sample)
{
if (string.IsNullOrEmpty(helpText)) throw new ArgumentException("helpText can't be null or empty", "helpText");
if (formatStyles == null) throw new ArgumentNullException("formatStyles");
if (sample == null) throw new ArgumentNullException("sample");
this.helpText = helpText;
this.formatStyles = formatStyles;
this.sample = sample;
}
///
/// Initializes a new instance of the class.
///
/// Example description.
/// A instance that defines command line arguments format.
/// A sample instance.
public Example(string helpText, UnParserSettings formatStyle, object sample)
: this(helpText, new[] { formatStyle }, sample)
{
}
///
/// Initializes a new instance of the class.
///
/// Example description.
/// A sample instance.
public Example(string helpText, object sample)
: this(helpText, Enumerable.Empty(), sample)
{
}
///
/// Example description.
///
public string HelpText
{
get { return helpText; }
}
///
/// A sequence of format styles.
///
public IEnumerable FormatStyles
{
get { return this.formatStyles; }
}
///
/// A sample instance.
///
public object Sample
{
get { return sample; }
}
///
/// Determines whether the specified is equal to the current .
///
/// The to compare with the current .
/// true if the specified is equal to the current ; otherwise, false.
public override bool Equals(object obj)
{
var other = obj as Example;
if (other != null)
{
return Equals(other);
}
return base.Equals(obj);
}
///
/// Serves as a hash function for a particular type.
///
/// A hash code for the current .
public override int GetHashCode()
{
return new { HelpText, FormatStyles, Sample }.GetHashCode();
}
///
/// Returns a value that indicates whether the current instance and a specified have the same value.
///
/// The instance to compare.
/// true if this instance of and have the same value; otherwise, false.
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 GetFormatStylesOrDefault(this Example example)
{
return example.FormatStyles.Any()
? example.FormatStyles
: new[] { new UnParserSettings { Consumed = true } };
}
}
}