#region License
//
// Copyright 2015-2013 Giacomo Stelluti Scala
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
#region Using Directives
using System;
using System.IO;
using System.Reflection;
using System.Text;
using CommandLine.Infrastructure;
#endregion
namespace CommandLine.Text
{
///
/// Models the heading part of an help text.
/// You can assign it where you assign any instance.
///
public class HeadingInfo
{
private readonly string _programName;
private readonly string _version;
///
/// Initializes a new instance of the class
/// specifying program name.
///
/// The name of the program.
/// Thrown when parameter is null or empty string.
public HeadingInfo(string programName)
: this(programName, null)
{
}
///
/// Initializes a new instance of the class
/// specifying program name and version.
///
/// The name of the program.
/// The version of the program.
/// Thrown when parameter is null or empty string.
public HeadingInfo(string programName, string version)
{
Assumes.NotNullOrEmpty(programName, "programName");
_programName = programName;
_version = version;
}
///
/// Gets the default heading instance.
/// The title is retrieved from ,
/// or the assembly short name if its not defined.
/// The version is retrieved from ,
/// or the assembly version if its not defined.
///
public static HeadingInfo Default
{
get
{
var titleAttribute = ReflectionHelper.GetAttribute();
string title = titleAttribute == null
? ReflectionHelper.AssemblyFromWhichToPullInformation.GetName().Name
: Path.GetFileNameWithoutExtension(titleAttribute.Title);
var versionAttribute = ReflectionHelper.GetAttribute();
string version = versionAttribute == null
? ReflectionHelper.AssemblyFromWhichToPullInformation.GetName().Version.ToString()
: versionAttribute.InformationalVersion;
return new HeadingInfo(title, version);
}
}
///
/// Converts the heading to a .
///
/// This instance.
/// The that contains the heading.
public static implicit operator string(HeadingInfo info)
{
return info.ToString();
}
///
/// Returns the heading as a .
///
/// The that contains the heading.
public override string ToString()
{
bool isVersionNull = string.IsNullOrEmpty(_version);
var builder = new StringBuilder(_programName.Length +
(!isVersionNull ? _version.Length + 1 : 0));
builder.Append(_programName);
if (!isVersionNull)
{
builder.Append(' ');
builder.Append(_version);
}
return builder.ToString();
}
///
/// Writes out a string and a new line using the program name specified in the constructor
/// and parameter.
///
/// The message to write.
/// The target derived type.
/// Thrown when parameter is null or empty string.
/// Thrown when parameter is null.
public void WriteMessage(string message, TextWriter writer)
{
Assumes.NotNullOrEmpty(message, "message");
Assumes.NotNull(writer, "writer");
var builder = new StringBuilder(_programName.Length + message.Length + 2);
builder.Append(_programName);
builder.Append(": ");
builder.Append(message);
writer.WriteLine(builder.ToString());
}
///
/// Writes out a string and a new line using the program name specified in the constructor
/// and parameter to standard output stream.
///
/// The message to write.
/// Thrown when parameter is null or empty string.
public void WriteMessage(string message)
{
WriteMessage(message, Console.Out);
}
///
/// Writes out a string and a new line using the program name specified in the constructor
/// and parameter to standard error stream.
///
/// The message to write.
/// Thrown when parameter is null or empty string.
public void WriteError(string message)
{
WriteMessage(message, Console.Error);
}
}
}