BadScript 2
Loading...
Searching...
No Matches
CopyrightInfo.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.Globalization;
5using System.Reflection;
6using System.Text;
7
9
10using CSharpx;
11
12namespace CommandLine.Text
13{
18 public class CopyrightInfo
19 {
20 private const string DefaultCopyrightWord = "Copyright";
21 private const string SymbolLower = "(c)";
22 private const string SymbolUpper = "(C)";
23 private readonly AssemblyCopyrightAttribute attribute;
24 private readonly string author;
25 private readonly int builderSize;
26 private readonly int[] copyrightYears;
27 private readonly bool isSymbolUpper;
28
36 public CopyrightInfo(string author, int year)
37 : this(true, author, year) { }
38
47 public CopyrightInfo(string author, params int[] years)
48 : this(true, author, years) { }
49
62 public CopyrightInfo(bool isSymbolUpper, string author, params int[] copyrightYears)
63 {
64 if (string.IsNullOrWhiteSpace(author))
65 {
66 throw new ArgumentException("author");
67 }
68
69 if (copyrightYears.Length == 0)
70 {
71 throw new ArgumentOutOfRangeException("copyrightYears");
72 }
73
74 const int ExtraLength = 10;
75 this.isSymbolUpper = isSymbolUpper;
76 this.author = author;
77 this.copyrightYears = copyrightYears;
78 builderSize = 12 + author.Length + 4 * copyrightYears.Length + ExtraLength;
79 }
80
84 protected CopyrightInfo() { }
85
91 private CopyrightInfo(AssemblyCopyrightAttribute attribute)
92 {
93 this.attribute = attribute;
94 }
95
99 public static CopyrightInfo Empty => new CopyrightInfo("author", DateTime.Now.Year);
100
107 public static CopyrightInfo Default
108 {
109 get
110 {
111 // if an exact copyright string has been specified, it takes precedence
113 ReflectionHelper.GetAttribute<AssemblyCopyrightAttribute>();
114
115 switch (copyrightAttr.Tag)
116 {
117 case MaybeType.Just:
118 return new CopyrightInfo(copyrightAttr.FromJustOrFail());
119 default:
121 ReflectionHelper.GetAttribute<AssemblyCompanyAttribute>();
122
123 return companyAttr.IsNothing()
124 //if both copyrightAttr and companyAttr aren't available in Assembly,don't fire Exception
125 ? Empty
126 // if no copyright attribute exist but a company attribute does, use it as copyright holder
127 : new CopyrightInfo(companyAttr.FromJust()
128 .Company,
129 DateTime.Now.Year
130 );
131 }
132 }
133 }
134
138 protected virtual string CopyrightWord => DefaultCopyrightWord;
139
145 public static implicit operator string(CopyrightInfo info)
146 {
147 return info.ToString();
148 }
149
154 public override string ToString()
155 {
156 if (attribute != null)
157 {
158 return attribute.Copyright;
159 }
160
161 return new StringBuilder(builderSize)
162 .Append(CopyrightWord)
163 .Append(' ')
165 .Append(' ')
167 .Append(' ')
168 .Append(author)
169 .ToString();
170 }
171
178 protected virtual string FormatYears(int[] years)
179 {
180 if (years.Length == 1)
181 {
182 return years[0]
183 .ToString(CultureInfo.InvariantCulture);
184 }
185
186 StringBuilder yearsPart = new StringBuilder(years.Length * 6);
187
188 for (int i = 0; i < years.Length; i++)
189 {
190 yearsPart.Append(years[i]
191 .ToString(CultureInfo.InvariantCulture)
192 );
193 int next = i + 1;
194
195 if (next < years.Length)
196 {
197 yearsPart.Append(years[next] - years[i] > 1 ? " - " : ", ");
198 }
199 }
200
201 return yearsPart.ToString();
202 }
203 }
204}
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33
MaybeType Tag
Type discriminator.
Definition Maybe.cs:42
MaybeType
Discriminator for CSharpx.Maybe.
Definition Maybe.cs:19