11using System . CommandLine ;
2+ using System . CommandLine . Invocation ;
23using LineCount ;
34using LineCount . Logging ;
45
910var exceptFilterOption = new Option < string > ( [ "-x" , "--exclude-filter" ] , "A glob-pattern for files not to include." ) ;
1011var exceptLineFilterOption = new Option < string > ( [ "-w" , "--exlude-line-filter" ] , "A RegEx for the lines not to count." ) ;
1112var listFilesOption = new Option < bool > ( "--list" , "Whether to list the files as they are being processed." ) ;
13+ var formatOption = new Option < Format > ( "--format" , "The output format of the result." ) ;
1214var excludeDirectoriesOption = new Option < string [ ] > ( "--exclude-directories" , "A list of directories to exclude." )
1315{
1416 Arity = ArgumentArity . OneOrMore ,
3032rootCommand . AddOption ( exceptFilterOption ) ;
3133rootCommand . AddOption ( exceptLineFilterOption ) ;
3234rootCommand . AddOption ( listFilesOption ) ;
35+ rootCommand . AddOption ( formatOption ) ;
3336
34- rootCommand . SetHandler ( async ( path , filter , lineFilter , exceptFilter , exceptLineFilter , excludeDirectories , excludeFilesOption , listFiles ) =>
35- {
36- LineCountData data = new LineCountData ( filter , lineFilter , exceptFilter , exceptLineFilter , listFiles ) ;
37- var result = await LineCount . LineCount . Run ( path , data , excludeDirectories , excludeFilesOption ) ;
37+ rootCommand . SetHandler ( async ( InvocationContext context ) =>
38+ {
39+ var filter = context . ParseResult . GetValueForOption ( filterOption ) ;
40+ var lineFilter = context . ParseResult . GetValueForOption ( lineFilterOption ) ;
41+ var exceptFilter = context . ParseResult . GetValueForOption ( exceptFilterOption ) ;
42+ var exceptLineFilter = context . ParseResult . GetValueForOption ( exceptLineFilterOption ) ;
43+ var listFiles = context . ParseResult . GetValueForOption ( listFilesOption ) ;
44+ var format = context . ParseResult . GetValueForOption ( formatOption ) ;
45+ var excludeDirectories = context . ParseResult . GetValueForOption ( excludeDirectoriesOption ) ;
46+ var excludeFiles = context . ParseResult . GetValueForOption ( excludeFilesOption ) ;
47+ var path = context . ParseResult . GetValueForArgument ( pathArgument ) ;
48+
49+ LineCountData data = new LineCountData ( filter , lineFilter , exceptFilter , exceptLineFilter )
50+ {
51+ ListFiles = listFiles ,
52+ Format = format
53+ } ;
54+
55+ var result = await LineCount . LineCount . Run ( path , data , excludeDirectories ?? [ ] , excludeFiles ?? [ ] ) ;
3856
3957 if ( listFiles )
4058 {
4563 report => Logger . LogReport ( report ) ,
4664 error => Logger . LogError ( error )
4765 ) ;
48- } , pathArgument , filterOption , lineFilterOption , exceptFilterOption , exceptLineFilterOption , excludeDirectoriesOption , excludeFilesOption , listFilesOption ) ;
66+ } ) ;
4967
5068return await rootCommand . InvokeAsync ( args ) ;
0 commit comments