22
33namespace EssentialCSharp . ListingManager ;
44
5- public class Program
5+ public sealed class Program
66{
7- private const string IntelliTect =
8- @" _____ _ _ _ _______ _
9- |_ _| | | | | ( )|__ __| | |
10- | | _ __ | |_ ___| | |_ | | ___ ___| |_
11- | | | '_ \| __/ _ \ | | | | |/ _ \/ __| __|
12- _| |_| | | | || __/ | | | | | __/ (__| |_
13- |_____|_| |_|\__\___|_|_|_| |_|\___|\___|\__|" ;
14- private static int Main ( string [ ] args )
7+ private static Task < int > Main ( string [ ] args )
158 {
16- var directoryIn = new Option < DirectoryInfo > (
17- name : "--path" ,
18- description : "The directory of the chapter to update listings on." )
19- { IsRequired = true } ;
20-
21- // With proper logging implemented, this option will hopefully be removed
22- var verboseOption = new Option < bool > (
23- name : "--verbose" ,
24- description : "Displays more detailed messages in the log" ) ;
25-
26- var previewOption = new Option < bool > (
27- name : "--preview" ,
28- description : "Writes all logs to console as if changes will be made without actually making changes." ) ;
29-
30- var allChaptersOption = new Option < bool > (
31- name : "--allChapters" ,
32- description : "The passed in path is the parent directory to many chapter directories rather than a single chapter directory." ) ;
9+ CliConfiguration configuration = GetConfiguration ( ) ;
10+ return configuration . InvokeAsync ( args ) ;
11+ }
3312
34- // TODO: Add better descriptions when their functionality becomes clearer
35- var byFolderOption = new Option < bool > (
36- name : "--byfolder" ,
37- description : "" ) ;
13+ public static CliConfiguration GetConfiguration ( )
14+ {
15+ // Use the ExistingOnly method to only parse the arguments that are defined in the configuration
3816
39- var singleDirOption = new Option < bool > (
40- name : "--singleDir" ,
41- description : "All listings are in a single directory and not separated into chapter and chapter test directories" ) ;
17+ CliArgument < DirectoryInfo > directoryInArgument = new ( "directoryIn" )
18+ {
19+ Description = "The directory of the chapter to update listings on." ,
20+ } ;
21+ directoryInArgument . AcceptExistingOnly ( ) ;
4222
43- var listingUpdating = new Command ( "update" , "Updates namespaces and filenames for all listings and accompanying tests within a chapter" )
23+ // With proper logging implemented, this option will hopefully be removed
24+ CliOption < bool > verboseOption = new ( "--verbose" )
4425 {
45- directoryIn ,
46- verboseOption ,
47- previewOption ,
48- byFolderOption ,
49- singleDirOption ,
50- allChaptersOption
26+ Description = "Displays more detailed messages in the log." ,
5127 } ;
5228
53- // Give better description when intent and functionality becomes more flushed out
54- var scan = new Command ( "scan" , "Scans for various things" )
29+ CliOption < bool > previewOption = new ( "--preview" )
5530 {
56- directoryIn
31+ Description = "Displays the changes that will be made without actually making them." ,
5732 } ;
5833
59- var scanForMismatchedListings = new Command ( "listings" , "Scans for mismatched listings ")
34+ CliOption < bool > allChaptersOption = new ( "--all-chapters ")
6035 {
61- directoryIn
36+ Description = "The passed in path is the parent directory to many chapter directories rather than a single chapter directory." ,
6237 } ;
6338
64- var scanForMissingTests = new Command ( "tests" , "Scans for missing tests" )
39+ // TODO: Add better descriptions when their functionality becomes clearer
40+ CliOption < bool > byFolderOption = new ( "--by-folder" )
6541 {
66- directoryIn ,
67- allChaptersOption ,
68- singleDirOption
42+ Description = "Updates namespaces and filenames for all listings and accompanying tests within a folder." ,
6943 } ;
7044
71- scan . AddCommand ( scanForMismatchedListings ) ;
72- scan . AddCommand ( scanForMissingTests ) ;
45+ CliOption < bool > singleDirOption = new ( "--single-dir" )
46+ {
47+ Description = "All listings are in a single directory and not separated into chapter and chapter test directories." ,
48+ } ;
7349
74- var rootCommand = new RootCommand ( )
50+ CliCommand listingUpdating = new ( "update" , "Updates namespaces and filenames for all listings and accompanying tests within a chapter" )
7551 {
76- listingUpdating ,
77- scan
52+ directoryInArgument ,
53+ verboseOption ,
54+ previewOption ,
55+ byFolderOption ,
56+ singleDirOption ,
57+ allChaptersOption
7858 } ;
7959
80- listingUpdating . SetHandler ( ( directoryIn , verbose , preview , byFolder , singleDir , allChapters ) =>
60+ listingUpdating . SetAction ( ( ParseResult parseResult ) =>
8161 {
82- Console . WriteLine ( IntelliTect ) ;
62+ DirectoryInfo directoryIn = parseResult . CommandResult . GetValue ( directoryInArgument ) ! ;
63+ bool verbose = parseResult . CommandResult . GetValue ( verboseOption ) ;
64+ bool preview = parseResult . CommandResult . GetValue ( previewOption ) ;
65+ bool byFolder = parseResult . CommandResult . GetValue ( byFolderOption ) ;
66+ bool singleDir = parseResult . CommandResult . GetValue ( singleDirOption ) ;
67+ bool allChapters = parseResult . CommandResult . GetValue ( allChaptersOption ) ;
68+
8369 Console . WriteLine ( $ "Updating listings within: { directoryIn } ") ;
8470 ListingManager listingManager = new ( directoryIn ) ;
8571 if ( allChapters )
@@ -90,23 +76,40 @@ private static int Main(string[] args)
9076 {
9177 listingManager . UpdateChapterListingNumbers ( directoryIn , verbose , preview , byFolder , singleDir ) ;
9278 }
93- } , directoryIn , verboseOption , previewOption , byFolderOption , singleDirOption , allChaptersOption ) ;
79+ } ) ;
9480
95- scanForMismatchedListings . SetHandler ( ( directoryIn ) =>
81+ CliCommand scan = new ( "scan" , "Scans for various things" ) ;
82+
83+ CliCommand listings = new ( "listings" , "Scans for mismatched listings" )
9684 {
97- Console . WriteLine ( IntelliTect ) ;
98- var extraListings = ListingManager . GetAllExtraListings ( directoryIn ! . FullName ) . OrderBy ( x => x ) ;
85+ directoryInArgument
86+ } ;
87+
88+ listings . SetAction ( ( ParseResult parseResult ) =>
89+ {
90+ DirectoryInfo directoryIn = parseResult . CommandResult . GetValue ( directoryInArgument ) ! ;
91+ var extraListings = ListingManager . GetAllExtraListings ( directoryIn . FullName ) . OrderBy ( x => x ) ;
9992
10093 Console . WriteLine ( "---Extra Listings---" ) ;
10194 foreach ( string extraListing in extraListings )
10295 {
10396 Console . WriteLine ( extraListing ) ;
10497 }
105- } , directoryIn ) ;
98+ } ) ;
99+ scan . Subcommands . Add ( listings ) ;
106100
107- scanForMissingTests . SetHandler ( ( directoryIn , allChapters , singleDir ) =>
101+ CliCommand tests = new ( "tests" , "Scans for mismatched tests" )
108102 {
109- Console . WriteLine ( IntelliTect ) ;
103+ directoryInArgument ,
104+ allChaptersOption ,
105+ singleDirOption
106+ } ;
107+
108+ tests . SetAction ( ( ParseResult parseResult ) =>
109+ {
110+ DirectoryInfo directoryIn = parseResult . CommandResult . GetValue ( directoryInArgument ) ! ;
111+ bool allChapters = parseResult . CommandResult . GetValue ( allChaptersOption ) ;
112+ bool singleDir = parseResult . CommandResult . GetValue ( singleDirOption ) ;
110113
111114 Console . WriteLine ( "---Missing Tests---" ) ;
112115 if ( allChapters )
@@ -117,8 +120,16 @@ private static int Main(string[] args)
117120 {
118121 ScanManager . ScanForMissingTests ( directoryIn , singleDir ) ;
119122 }
120- } , directoryIn , allChaptersOption , singleDirOption ) ;
123+ } ) ;
124+
125+ scan . Subcommands . Add ( tests ) ;
126+
127+ CliRootCommand rootCommand = new ( "The EssentialCSharp.ListingManager helps to organize and manage the EssentialCSharp source code" )
128+ {
129+ listingUpdating ,
130+ scan
131+ } ;
121132
122- return rootCommand . Invoke ( args ) ;
133+ return new CliConfiguration ( rootCommand ) ;
123134 }
124135}
0 commit comments