-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathHeadingInfo.cs
138 lines (125 loc) · 5.86 KB
/
HeadingInfo.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
132
133
134
135
136
137
138
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.IO;
using System.Reflection;
using System.Text;
using CommandLine.Infrastructure;
using CSharpx;
namespace CommandLine.Text
{
/// <summary>
/// Models the heading part of an help text.
/// You can assign it where you assign any <see cref="System.String"/> instance.
/// </summary>
public class HeadingInfo
{
private readonly string programName;
private readonly string version;
/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.Text.HeadingInfo"/> class
/// specifying program name and version.
/// </summary>
/// <param name="programName">The name of the program.</param>
/// <param name="version">The version of the program.</param>
/// <exception cref="System.ArgumentException">Thrown when parameter <paramref name="programName"/> is null or empty string.</exception>
public HeadingInfo(string programName, string version = null)
{
if (string.IsNullOrWhiteSpace("programName")) throw new ArgumentException("programName");
this.programName = programName;
this.version = version;
}
/// <summary>
/// An empty object used for initialization.
/// </summary>
public static HeadingInfo Empty
{
get
{
return new HeadingInfo("");
}
}
/// <summary>
/// Gets the default heading instance.
/// The title is retrieved from <see cref="AssemblyTitleAttribute"/>,
/// or the assembly short name if its not defined.
/// The version is retrieved from <see cref="AssemblyInformationalVersionAttribute"/>,
/// or the assembly version if its not defined.
/// </summary>
public static HeadingInfo Default
{
get
{
var title = ReflectionHelper.GetAttribute<AssemblyTitleAttribute>()
.MapValueOrDefault(
titleAttribute => titleAttribute.Title,
ReflectionHelper.GetAssemblyName());
var version = ReflectionHelper.GetAttribute<AssemblyInformationalVersionAttribute>()
.MapValueOrDefault(
versionAttribute => versionAttribute.InformationalVersion,
ReflectionHelper.GetAssemblyVersion());
return new HeadingInfo(title, version);
}
}
/// <summary>
/// Converts the heading to a <see cref="System.String"/>.
/// </summary>
/// <param name="info">This <see cref="CommandLine.Text.HeadingInfo"/> instance.</param>
/// <returns>The <see cref="System.String"/> that contains the heading.</returns>
public static implicit operator string(HeadingInfo info)
{
return info.ToString();
}
/// <summary>
/// Returns the heading as a <see cref="System.String"/>.
/// </summary>
/// <returns>The <see cref="System.String"/> that contains the heading.</returns>
public override string ToString()
{
var isVersionNull = string.IsNullOrEmpty(version);
return new StringBuilder(programName.Length +
(!isVersionNull ? version.Length + 1 : 0))
.Append(programName)
.AppendWhen(!isVersionNull, " ", version)
.ToString();
}
/// <summary>
/// Writes out a string and a new line using the program name specified in the constructor
/// and <paramref name="message"/> parameter.
/// </summary>
/// <param name="message">The <see cref="System.String"/> message to write.</param>
/// <param name="writer">The target <see cref="System.IO.TextWriter"/> derived type.</param>
/// <exception cref="System.ArgumentException">Thrown when parameter <paramref name="message"/> is null or empty string.</exception>
/// <exception cref="System.ArgumentNullException">Thrown when parameter <paramref name="writer"/> is null.</exception>
public void WriteMessage(string message, TextWriter writer)
{
if (string.IsNullOrWhiteSpace(message)) throw new ArgumentException(nameof(message));
if (writer == null) throw new ArgumentNullException(nameof(writer));
writer.WriteLine(
new StringBuilder(programName.Length + message.Length + 2)
.Append(programName)
.Append(": ")
.Append(message)
.ToString());
}
/// <summary>
/// Writes out a string and a new line using the program name specified in the constructor
/// and <paramref name="message"/> parameter to standard output stream.
/// </summary>
/// <param name="message">The <see cref="System.String"/> message to write.</param>
/// <exception cref="System.ArgumentException">Thrown when parameter <paramref name="message"/> is null or empty string.</exception>
public void WriteMessage(string message)
{
WriteMessage(message, Console.Out);
}
/// <summary>
/// Writes out a string and a new line using the program name specified in the constructor
/// and <paramref name="message"/> parameter to standard error stream.
/// </summary>
/// <param name="message">The <see cref="System.String"/> message to write.</param>
/// <exception cref="System.ArgumentException">Thrown when parameter <paramref name="message"/> is null or empty string.</exception>
public void WriteError(string message)
{
WriteMessage(message, Console.Error);
}
}
}