@@ -13,66 +13,64 @@ public class Program
1313 private static readonly ILoggerFactory _loggerFactory ;
1414 private static readonly ILogger _logger ;
1515
16- private static readonly Option < bool > _includeSubdirectoriesOption = new (
17- name : "--include-subdirectories" ,
18- description : "When the command is dir, converts files in all subdirectories" ,
19- getDefaultValue : ( ) => true ) ;
20-
21- private static readonly Option < bool > _includeUsingsOption = new (
22- name : "--include-usings" ,
23- description : "Include using directives in output" ,
24- getDefaultValue : ( ) => true ) ;
25-
26- private static readonly Option < bool > _includeNamespaceOption = new (
27- name : "--include-namespace" ,
28- description : "Include namespace in output" ,
29- getDefaultValue : ( ) => true ) ;
30-
31- private static readonly Option < bool > _includeCommentsOption = new (
32- name : "--include-comments" ,
33- description : "Include comments in output" ,
34- getDefaultValue : ( ) => true ) ;
35-
36- private static readonly Option < bool > _useDebugAssertOption = new (
37- name : "--use-debug-assert" ,
38- description : "Use Debug.Assert for asserts" ,
39- getDefaultValue : ( ) => false ) ;
40-
41- private static readonly Option < bool > _startInterfaceNamesWithIOption = new (
42- name : "--start-interface-names-with-i" ,
43- description : "Prefix interface names with the letter I" ,
44- getDefaultValue : ( ) => true ) ;
45-
46- private static readonly Option < bool > _commentUnrecognizedCodeOption = new (
47- name : "--comment-unrecognized-code" ,
48- description : "Include unrecognized code in output as commented-out code" ,
49- getDefaultValue : ( ) => true ) ;
50-
51- private static readonly Option < bool > _systemOutToConsoleOption = new (
52- name : "--system-out-to-console" ,
53- description : "Convert System.out calls to Console" ,
54- getDefaultValue : ( ) => false ) ;
55-
56- private static readonly Option < bool > _fileScopedNamespacesOption = new (
57- name : "--file-scoped-namespaces" ,
58- description : "Use file-scoped namespaces in C# output" ,
59- getDefaultValue : ( ) => false ) ;
60-
61- private static readonly Option < bool > _clearDefaultUsingsOption = new (
62- name : "--clear-usings" ,
63- description : "Remove all default usings provided by this app" ,
64- getDefaultValue : ( ) => false ) ;
65-
66- private static readonly Option < List < string > > _addUsingsOption = new (
67- name : "--add-using" ,
68- description : "Adds a using directive to the collection of usings" )
69- {
70- ArgumentHelpName = "namespace"
16+ private static readonly Option < bool > _includeSubdirectoriesOption = new ( "--include-subdirectories" ) {
17+ Description = "When the command is dir, converts files in all subdirectories" ,
18+ DefaultValueFactory = _ => true ,
19+ } ;
20+
21+ private static readonly Option < bool > _includeUsingsOption = new ( "--include-usings" ) {
22+ Description = "Include using directives in output" ,
23+ DefaultValueFactory = _ => true ,
24+ } ;
25+
26+ private static readonly Option < bool > _includeNamespaceOption = new ( "--include-namespace" ) {
27+ Description = "Include namespace in output" ,
28+ DefaultValueFactory = _ => true
29+ } ;
30+
31+ private static readonly Option < bool > _includeCommentsOption = new ( "--include-comments" ) {
32+ Description = "Include comments in output" ,
33+ DefaultValueFactory = _ => true
34+ } ;
35+
36+ private static readonly Option < bool > _useDebugAssertOption = new ( "--use-debug-assert" ) {
37+ Description = "Use Debug.Assert for asserts" ,
38+ DefaultValueFactory = _ => false
39+ } ;
40+
41+ private static readonly Option < bool > _startInterfaceNamesWithIOption = new ( "--start-interface-names-with-i" ) {
42+ Description = "Prefix interface names with the letter I" ,
43+ DefaultValueFactory = _ => true
44+ } ;
45+
46+ private static readonly Option < bool > _commentUnrecognizedCodeOption = new ( "--comment-unrecognized-code" ) {
47+ Description = "Include unrecognized code in output as commented-out code" ,
48+ DefaultValueFactory = _ => true
7149 } ;
7250
73- private static readonly Option < string > _mappingsFileNameOption = new (
74- name : "--mappings-file" ,
75- description : "A yaml file with syntax mappings from imports, methods and annotations" ) ;
51+ private static readonly Option < bool > _systemOutToConsoleOption = new ( "--system-out-to-console" ) {
52+ Description = "Convert System.out calls to Console" ,
53+ DefaultValueFactory = _ => false
54+ } ;
55+
56+ private static readonly Option < bool > _fileScopedNamespacesOption = new ( "--file-scoped-namespaces" ) {
57+ Description = "Use file-scoped namespaces in C# output" ,
58+ DefaultValueFactory = _ => false
59+ } ;
60+
61+ private static readonly Option < bool > _clearDefaultUsingsOption = new ( "--clear-usings" ) {
62+ Description = "Remove all default usings provided by this app" ,
63+ DefaultValueFactory = _ => false
64+ } ;
65+
66+ private static readonly Option < List < string > > _addUsingsOption = new ( "--add-using" ) {
67+ Description = "Adds a using directive to the collection of usings" ,
68+ HelpName = "namespace"
69+ } ;
70+
71+ private static readonly Option < string > _mappingsFileNameOption = new ( "--mappings-file" ) {
72+ Description = "A yaml file with syntax mappings from imports, methods and annotations"
73+ } ;
7674
7775 static Program ( )
7876 {
@@ -88,72 +86,72 @@ public static async Task Main(string[] args)
8886 Description = "A syntactic transformer of source code from Java to C#."
8987 } ;
9088
91- rootCommand . AddCommand ( CreateFileCommand ( ) ) ;
92- rootCommand . AddCommand ( CreateDirectoryCommand ( ) ) ;
89+ rootCommand . Subcommands . Add ( CreateFileCommand ( ) ) ;
90+ rootCommand . Subcommands . Add ( CreateDirectoryCommand ( ) ) ;
9391
94- rootCommand . AddGlobalOption ( _includeSubdirectoriesOption ) ;
95- rootCommand . AddGlobalOption ( _includeUsingsOption ) ;
96- rootCommand . AddGlobalOption ( _includeNamespaceOption ) ;
97- rootCommand . AddGlobalOption ( _includeCommentsOption ) ;
98- rootCommand . AddGlobalOption ( _useDebugAssertOption ) ;
99- rootCommand . AddGlobalOption ( _startInterfaceNamesWithIOption ) ;
100- rootCommand . AddGlobalOption ( _commentUnrecognizedCodeOption ) ;
101- rootCommand . AddGlobalOption ( _systemOutToConsoleOption ) ;
102- rootCommand . AddGlobalOption ( _fileScopedNamespacesOption ) ;
103- rootCommand . AddGlobalOption ( _clearDefaultUsingsOption ) ;
104- rootCommand . AddGlobalOption ( _addUsingsOption ) ;
105- rootCommand . AddGlobalOption ( _mappingsFileNameOption ) ;
92+ rootCommand . Options . Add ( _includeSubdirectoriesOption ) ;
93+ rootCommand . Options . Add ( _includeUsingsOption ) ;
94+ rootCommand . Options . Add ( _includeNamespaceOption ) ;
95+ rootCommand . Options . Add ( _includeCommentsOption ) ;
96+ rootCommand . Options . Add ( _useDebugAssertOption ) ;
97+ rootCommand . Options . Add ( _startInterfaceNamesWithIOption ) ;
98+ rootCommand . Options . Add ( _commentUnrecognizedCodeOption ) ;
99+ rootCommand . Options . Add ( _systemOutToConsoleOption ) ;
100+ rootCommand . Options . Add ( _fileScopedNamespacesOption ) ;
101+ rootCommand . Options . Add ( _clearDefaultUsingsOption ) ;
102+ rootCommand . Options . Add ( _addUsingsOption ) ;
103+ rootCommand . Options . Add ( _mappingsFileNameOption ) ;
106104
107- await rootCommand . InvokeAsync ( args ) ;
105+ await rootCommand . Parse ( args ) . InvokeAsync ( ) ;
108106
109107 // flush logs
110108 _loggerFactory . Dispose ( ) ;
111109 }
112110
113111 private static Command CreateFileCommand ( )
114112 {
115- var inputArgument = new Argument < FileInfo > (
116- name : "input" ,
117- description : "A Java source code file to convert" ) ;
113+ var inputArgument = new Argument < FileInfo > ( "input" ) {
114+ Description = "A Java source code file to convert"
115+ } ;
118116
119- var outputArgument = new Argument < FileInfo ? > (
120- name : " output",
121- description : "Path to place the C# output file, or stdout if omitted" ,
122- getDefaultValue : ( ) => null ) ;
117+ var outputArgument = new Argument < FileInfo ? > ( "output" ) {
118+ Description = "Path to place the C# output file, or stdout if omitted ",
119+ DefaultValueFactory = _ => null
120+ } ;
123121
124122 var fileCommand = new Command ( "file" , "Convert a Java file to C#" ) ;
125- fileCommand . AddArgument ( inputArgument ) ;
126- fileCommand . AddArgument ( outputArgument ) ;
123+ fileCommand . Arguments . Add ( inputArgument ) ;
124+ fileCommand . Arguments . Add ( outputArgument ) ;
127125
128- fileCommand . SetHandler ( context =>
126+ fileCommand . SetAction ( context =>
129127 {
130- var input = context . ParseResult . GetValueForArgument ( inputArgument ) ;
131- var output = context . ParseResult . GetValueForArgument ( outputArgument ) ;
128+ var input = context . GetValue ( inputArgument ) ;
129+ var output = context . GetValue ( outputArgument ) ;
132130
133131 var options = GetJavaConversionOptions ( context ) ;
134132
135- ConvertToCSharpFile ( input , output , options ) ;
133+ ConvertToCSharpFile ( input ! , output , options ) ;
136134 } ) ;
137135
138136 return fileCommand ;
139137 }
140138
141- private static JavaConversionOptions GetJavaConversionOptions ( InvocationContext context )
139+ private static JavaConversionOptions GetJavaConversionOptions ( ParseResult context )
142140 {
143141 var options = new JavaConversionOptions
144142 {
145- IncludeSubdirectories = context . ParseResult . GetValueForOption ( _includeSubdirectoriesOption ) ,
146- IncludeUsings = context . ParseResult . GetValueForOption ( _includeUsingsOption ) ,
147- IncludeComments = context . ParseResult . GetValueForOption ( _includeCommentsOption ) ,
148- IncludeNamespace = context . ParseResult . GetValueForOption ( _includeNamespaceOption ) ,
149- ConvertSystemOutToConsole = context . ParseResult . GetValueForOption ( _systemOutToConsoleOption ) ,
150- StartInterfaceNamesWithI = context . ParseResult . GetValueForOption ( _startInterfaceNamesWithIOption ) ,
151- UseDebugAssertForAsserts = context . ParseResult . GetValueForOption ( _useDebugAssertOption ) ,
152- UseUnrecognizedCodeToComment = context . ParseResult . GetValueForOption ( _commentUnrecognizedCodeOption ) ,
153- UseFileScopedNamespaces = context . ParseResult . GetValueForOption ( _fileScopedNamespacesOption ) ,
143+ IncludeSubdirectories = context . GetValue ( _includeSubdirectoriesOption ) ,
144+ IncludeUsings = context . GetValue ( _includeUsingsOption ) ,
145+ IncludeComments = context . GetValue ( _includeCommentsOption ) ,
146+ IncludeNamespace = context . GetValue ( _includeNamespaceOption ) ,
147+ ConvertSystemOutToConsole = context . GetValue ( _systemOutToConsoleOption ) ,
148+ StartInterfaceNamesWithI = context . GetValue ( _startInterfaceNamesWithIOption ) ,
149+ UseDebugAssertForAsserts = context . GetValue ( _useDebugAssertOption ) ,
150+ UseUnrecognizedCodeToComment = context . GetValue ( _commentUnrecognizedCodeOption ) ,
151+ UseFileScopedNamespaces = context . GetValue ( _fileScopedNamespacesOption ) ,
154152 } ;
155153
156- if ( context . ParseResult . GetValueForOption ( _clearDefaultUsingsOption ) )
154+ if ( context . GetValue ( _clearDefaultUsingsOption ) )
157155 {
158156 options . ClearUsings ( ) ;
159157 }
@@ -166,12 +164,12 @@ private static JavaConversionOptions GetJavaConversionOptions(InvocationContext
166164 options . AddUsing ( "System.Text" ) ;
167165 }
168166
169- foreach ( string ns in context . ParseResult . GetValueForOption ( _addUsingsOption ) ?? new List < string > ( ) )
167+ foreach ( string ns in context . GetValue ( _addUsingsOption ) ?? new List < string > ( ) )
170168 {
171169 options . AddUsing ( ns ) ;
172170 }
173171
174- var mappingsFile = context . ParseResult . GetValueForOption ( _mappingsFileNameOption ) ;
172+ var mappingsFile = context . GetValue ( _mappingsFileNameOption ) ;
175173 if ( ! string . IsNullOrEmpty ( mappingsFile ) )
176174 {
177175 options . SyntaxMappings = ReadMappingsFile ( mappingsFile ) ;
@@ -189,26 +187,26 @@ private static SyntaxMapping ReadMappingsFile(string mappingsFile)
189187
190188 private static Command CreateDirectoryCommand ( )
191189 {
192- var inputArgument = new Argument < DirectoryInfo > (
193- name : "input" ,
194- description : "A directory containing Java source code files to convert" ) ;
190+ var inputArgument = new Argument < DirectoryInfo > ( "input" ) {
191+ Description = "A directory containing Java source code files to convert"
192+ } ;
195193
196- var outputArgument = new Argument < DirectoryInfo > (
197- name : " output" ,
198- description : "Path to place the C# output files" ) ;
194+ var outputArgument = new Argument < DirectoryInfo > ( "output" ) {
195+ Description = "Path to place the C# output files"
196+ } ;
199197
200198 var dirCommand = new Command ( "dir" , "Convert a directory containing Java files to C#" ) ;
201- dirCommand . AddArgument ( inputArgument ) ;
202- dirCommand . AddArgument ( outputArgument ) ;
199+ dirCommand . Arguments . Add ( inputArgument ) ;
200+ dirCommand . Arguments . Add ( outputArgument ) ;
203201
204- dirCommand . SetHandler ( context =>
202+ dirCommand . SetAction ( context =>
205203 {
206- var input = context . ParseResult . GetValueForArgument ( inputArgument ) ;
207- var output = context . ParseResult . GetValueForArgument ( outputArgument ) ;
204+ var input = context . GetValue ( inputArgument ) ;
205+ var output = context . GetValue ( outputArgument ) ;
208206
209207 var options = GetJavaConversionOptions ( context ) ;
210208
211- ConvertToCSharpDir ( input , output , options ) ;
209+ ConvertToCSharpDir ( input ! , output ! , options ) ;
212210 } ) ;
213211
214212 return dirCommand ;
0 commit comments