1+ using System . CommandLine ;
2+ using System . CommandLine . Builder ;
3+ using System . CommandLine . Invocation ;
4+ using System . CommandLine . Parsing ;
5+ using System . Reflection ;
6+ using Microsoft . Extensions . Configuration ;
7+ using Microsoft . Extensions . DependencyInjection ;
8+ using Microsoft . Extensions . Hosting ;
9+ using Microsoft . Extensions . Logging ;
10+ using Microsoft . Extensions . Logging . Console ;
11+
12+ namespace _01_Way ;
13+
14+ public static class Program
15+ {
16+ public static async Task Main ( string [ ] args )
17+ {
18+ using var host = CreateHostBuilder ( args ) . Build ( ) ;
19+
20+ var rootCommand = new RootCommand
21+ {
22+ BuildUpperSubCommand ( ) ,
23+ BuildLowerSubCommand ( )
24+ } ;
25+
26+ var commandLine = new CommandLineBuilder ( rootCommand )
27+ . UseDefaults ( )
28+ . Build ( ) ;
29+
30+ await commandLine . InvokeAsync ( args ) ;
31+ }
32+
33+ private static Command BuildUpperSubCommand ( )
34+ {
35+ var command = new Command ( "upper" )
36+ {
37+ Handler = CommandHandler . Create < string > ( input => Console . WriteLine ( input . ToUpper ( ) ) )
38+ } ;
39+
40+ command . AddArgument ( new Argument < string > ( "input" ) ) ;
41+
42+ return command ;
43+ }
44+
45+ private static Command BuildLowerSubCommand ( )
46+ {
47+ var command = new Command ( "lower" )
48+ {
49+ Handler = CommandHandler . Create < string > ( input => Console . WriteLine ( input . ToLower ( ) ) )
50+ } ;
51+
52+ command . AddArgument ( new Argument < string > ( "input" ) ) ;
53+
54+ return command ;
55+ }
56+
57+ private static IHostBuilder CreateHostBuilder ( string [ ] args ) =>
58+ Host . CreateDefaultBuilder ( args )
59+ . ConfigureAppConfiguration ( ( _ , config ) =>
60+ {
61+ config . AddCommandLine ( args ) ;
62+ config . AddEnvironmentVariables ( ) ;
63+ config . SetBasePath ( GetDirectoryPath ( ) ) ;
64+ var environment = Environment . GetEnvironmentVariable ( "ENVIRONMENT" ) ;
65+ config . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true ) ;
66+ config . AddJsonFile ( $ "appsettings.{ environment } .json", optional : true , reloadOnChange : true ) ;
67+ } )
68+ . ConfigureLogging ( ( hostingContext , loggingBuilder ) =>
69+ {
70+ loggingBuilder . AddConsoleLogger ( ) ;
71+ loggingBuilder . AddNonGenericLogger ( ) ;
72+ loggingBuilder . AddConfiguration ( hostingContext . Configuration . GetSection ( "Logging" ) ) ;
73+ } ) ;
74+
75+ private static void AddConsoleLogger ( this ILoggingBuilder loggingBuilder )
76+ {
77+ if ( ! File . Exists ( GetSettingFilePath ( ) ) )
78+ {
79+ loggingBuilder . AddSimpleConsole ( options =>
80+ {
81+ options . SingleLine = true ;
82+ options . IncludeScopes = true ;
83+ options . UseUtcTimestamp = true ;
84+ options . TimestampFormat = "[HH:mm:ss:fff] " ;
85+ options . ColorBehavior = LoggerColorBehavior . Enabled ;
86+ } ) ;
87+ }
88+ }
89+
90+ private static void AddNonGenericLogger ( this ILoggingBuilder loggingBuilder )
91+ {
92+ var categoryName = typeof ( Program ) . Namespace ?? string . Empty ;
93+ var services = loggingBuilder . Services ;
94+ services . AddSingleton ( serviceProvider =>
95+ {
96+ var loggerFactory = serviceProvider . GetRequiredService < ILoggerFactory > ( ) ;
97+ return loggerFactory . CreateLogger ( categoryName ) ;
98+ } ) ;
99+ }
100+
101+ private static string GetSettingFilePath ( ) => Path . GetFullPath ( Path . Combine ( GetDirectoryPath ( ) , @"appsettings.json" ) ) ;
102+
103+ private static string GetDirectoryPath ( )
104+ {
105+ try
106+ {
107+ return Path . GetDirectoryName ( GetAssemblyLocation ( ) ) ?? "./" ;
108+ }
109+ catch
110+ {
111+ return Directory . GetCurrentDirectory ( ) ;
112+ }
113+ }
114+
115+ private static string GetAssemblyLocation ( ) => Assembly . GetExecutingAssembly ( ) . Location ;
116+ }
0 commit comments