1+ #region License
2+
3+ /*
4+ * File: ConsoleHelper.cs
5+ *
6+ * The MIT License
7+ *
8+ * Copyright © 2017 AVSP Ltd
9+ *
10+ * Permission is hereby granted, free of charge, to any person obtaining a copy
11+ * of this software and associated documentation files (the "Software"), to deal
12+ * in the Software without restriction, including without limitation the rights
13+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+ * copies of the Software, and to permit persons to whom the Software is
15+ * furnished to do so, subject to the following conditions:
16+ *
17+ * The above copyright notice and this permission notice shall be included in
18+ * all copies or substantial portions of the Software.
19+ *
20+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+ * THE SOFTWARE.
27+ */
28+
29+ #endregion License
30+
31+ using System ;
32+ using System . Collections . Generic ;
33+ using System . Diagnostics ;
34+ using System . Reflection ;
35+
36+ namespace AVSP . ConsoleSupport
37+ {
38+ /// <summary>
39+ /// Console support library for banner, copyright, version, command line arguments
40+ /// </summary>
41+ internal static class ConsoleHelper
42+ {
43+ public delegate int RunFunction ( SimpleArguments arguments ) ;
44+
45+ public static SimpleArguments Arguments { get ; private set ; }
46+
47+ /// <summary>
48+ /// Runs a console app with parsed options, sensible exit codes, exception handler
49+ ///
50+ /// Example:
51+ /// private static int Main(string[] args)
52+ /// {
53+ /// return ConsoleHelper.RunProgram(args, Run);
54+ /// }
55+ /// </summary>
56+ /// <param name="args">arguments from Main()</param>
57+ /// <param name="run">delegate to run app</param>
58+ /// <returns>program exit code</returns>
59+ public static int RunProgram ( IList < string > args , RunFunction run )
60+ {
61+ try
62+ {
63+ ConsoleHelper . Startup ( args ) ;
64+ ConsoleHelper . WriteBanner ( ) ;
65+
66+ int returnValue = run ( ConsoleHelper . Arguments ) ;
67+
68+ return ( Environment . ExitCode == 0 ) ? returnValue : Environment . ExitCode ;
69+ }
70+ catch ( Exception e )
71+ {
72+ Console . Error . WriteLine ( e . Message ) ;
73+ Trace . TraceError ( e . ToString ( ) ) ;
74+
75+ return Environment . ExitCode != 0
76+ ? Environment . ExitCode : 100 ;
77+ }
78+ }
79+
80+ public static void Startup ( IList < string > args )
81+ {
82+ Arguments = new SimpleArguments ( args ) ;
83+
84+ // enable trace with v parameter
85+ if ( Arguments . GetFlag ( "v" ) )
86+ {
87+ Trace . Listeners . Add ( new ConsoleTraceListener ( true ) ) ;
88+ }
89+ }
90+
91+ public static string GetProductVersion ( )
92+ {
93+ return Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version . ToString ( ) ;
94+ }
95+
96+ public static string GetProductName ( )
97+ {
98+ return Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ;
99+ }
100+
101+ public static string GetDescription ( )
102+ {
103+ //Type of attribute that is desired
104+ Type type = typeof ( AssemblyDescriptionAttribute ) ;
105+
106+ //Is there an attribute of this type already defined?
107+ if ( AssemblyDescriptionAttribute . IsDefined ( Assembly . GetExecutingAssembly ( ) , type ) )
108+ {
109+ //if there is, get attribute of desired type
110+ AssemblyDescriptionAttribute assemblyDescriptionAttribute = ( AssemblyDescriptionAttribute ) AssemblyDescriptionAttribute . GetCustomAttribute ( Assembly . GetExecutingAssembly ( ) , type ) ;
111+
112+ return assemblyDescriptionAttribute . Description ;
113+ }
114+
115+ return null ;
116+ }
117+
118+ public static string GetCopyright ( )
119+ {
120+ //Type of attribute that is desired
121+ Type type = typeof ( AssemblyCopyrightAttribute ) ;
122+
123+ //Is there an attribute of this type already defined?
124+ if ( AssemblyCopyrightAttribute . IsDefined ( Assembly . GetExecutingAssembly ( ) , type ) )
125+ {
126+ //if there is, get attribute of desired type
127+ AssemblyCopyrightAttribute assemblyCopyrightAttribute = ( AssemblyCopyrightAttribute ) AssemblyCopyrightAttribute . GetCustomAttribute ( Assembly . GetExecutingAssembly ( ) , type ) ;
128+
129+ return assemblyCopyrightAttribute . Copyright ;
130+ }
131+
132+ return null ;
133+ }
134+
135+ public static void WriteBanner ( )
136+ {
137+ Console . WriteLine ( GetProductName ( ) + " v" + GetProductVersion ( ) ) ;
138+ Console . WriteLine ( GetCopyright ( ) ) ;
139+ Console . WriteLine ( ) ;
140+ }
141+ }
142+ }
0 commit comments