BadScript 2
Loading...
Searching...
No Matches
HeadingInfo.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.IO;
5using System.Reflection;
6using System.Text;
7
9
10using CSharpx;
11
12namespace CommandLine.Text
13{
18 public class HeadingInfo
19 {
20 private readonly string programName;
21 private readonly string version;
22
33 public HeadingInfo(string programName, string version = null)
34 {
35 if (string.IsNullOrWhiteSpace("programName"))
36 {
37 throw new ArgumentException("programName");
38 }
39
40 this.programName = programName;
41 this.version = version;
42 }
43
47 public static HeadingInfo Empty => new HeadingInfo("");
48
56 public static HeadingInfo Default
57 {
58 get
59 {
60 string title = ReflectionHelper.GetAttribute<AssemblyTitleAttribute>()
61 .MapValueOrDefault(titleAttribute => titleAttribute.Title,
63 );
64
65 string version = ReflectionHelper.GetAttribute<AssemblyInformationalVersionAttribute>()
66 .MapValueOrDefault(versionAttribute =>
67 versionAttribute.InformationalVersion,
69 );
70
71 return new HeadingInfo(title, version);
72 }
73 }
74
80 public static implicit operator string(HeadingInfo info)
81 {
82 return info.ToString();
83 }
84
89 public override string ToString()
90 {
91 bool isVersionNull = string.IsNullOrEmpty(version);
92
93 return new StringBuilder(programName.Length +
94 (!isVersionNull ? version.Length + 1 : 0)
95 )
96 .Append(programName)
97 .AppendWhen(!isVersionNull, " ", version)
98 .ToString();
99 }
100
109 public void WriteMessage(string message, TextWriter writer)
110 {
111 if (string.IsNullOrWhiteSpace("message"))
112 {
113 throw new ArgumentException("message");
114 }
115
116 if (writer == null)
117 {
118 throw new ArgumentNullException("writer");
119 }
120
121 writer.WriteLine(new StringBuilder(programName.Length + message.Length + 2)
122 .Append(programName)
123 .Append(": ")
124 .Append(message)
125 .ToString()
126 );
127 }
128
135 public void WriteMessage(string message)
136 {
137 WriteMessage(message, Console.Out);
138 }
139
146 public void WriteError(string message)
147 {
148 WriteMessage(message, Console.Error);
149 }
150 }
151}
Models the heading part of an help text. You can assign it where you assign any System....
readonly string programName
void WriteMessage(string message)
Writes out a string and a new line using the program name specified in the constructor and message p...
static HeadingInfo Default
Gets the default heading instance. The title is retrieved from AssemblyTitleAttribute,...
override string ToString()
Returns the heading as a System.String.
static HeadingInfo Empty
An empty object used for initialization.
HeadingInfo(string programName, string version=null)
Initializes a new instance of the CommandLine.Text.HeadingInfo class specifying program name and vers...
void WriteMessage(string message, TextWriter writer)
Writes out a string and a new line using the program name specified in the constructor and message p...
void WriteError(string message)
Writes out a string and a new line using the program name specified in the constructor and message p...