11using System ;
2+ using System . Linq ;
3+ using System . Reflection ;
24using InEngine . Core ;
35using InEngine . Core . Exceptions ;
46using NLog ;
@@ -16,46 +18,63 @@ public ArgumentInterpreter()
1618
1719 public void Interpret ( string [ ] args )
1820 {
21+ var parser = new CommandLine . Parser ( with => with . IgnoreUnknownArguments = true ) ;
1922 var options = new Options ( ) ;
20- var isSuccessful = CommandLine . Parser . Default . ParseArguments ( args , options , ( verb , subOptions ) =>
23+
24+ if ( parser . ParseArguments ( args , options ) )
2125 {
22- try
26+ if ( options == null )
27+ Environment . Exit ( CommandLine . Parser . DefaultExitCodeFail ) ;
28+ var targetAssembly = Assembly . LoadFrom ( options . PlugInName + ".dll" ) ;
29+ var types = targetAssembly . GetTypes ( ) ;
30+ var optionType = types . FirstOrDefault ( x => typeof ( IOptions ) . IsAssignableFrom ( x ) ) ;
31+ if ( optionType == null )
32+ Environment . Exit ( CommandLine . Parser . DefaultExitCodeFail ) ;
33+ var pluginOptions = targetAssembly . CreateInstance ( optionType . FullName ) as IOptions ;
34+ var isSuccessful = CommandLine
35+ . Parser
36+ . Default
37+ . ParseArguments ( args . Skip ( 1 ) . ToArray ( ) , pluginOptions , ( verb , subOptions ) =>
2338 {
24- if ( subOptions == null )
25- Environment . Exit ( CommandLine . Parser . DefaultExitCodeFail ) ;
39+ try
40+ {
41+ if ( subOptions == null )
42+ ExitWithFailure ( new CommandFailedException ( "Could not parse plugin options" ) ) ;
2643
27- var command = subOptions as ICommand ;
44+ var command = subOptions as ICommand ;
2845
29- if ( command is AbstractCommand )
30- ( command as AbstractCommand ) . Name = verb . Normalize ( ) ;
46+ if ( command is AbstractCommand )
47+ ( command as AbstractCommand ) . Name = verb . Normalize ( ) ;
3148
32- var commandResult = command . Run ( ) ;
49+ var commandResult = command . Run ( ) ;
3350
34- if ( commandResult . IsSuccessful )
35- ExitWithSuccess ( commandResult . Message ) ;
36- else
37- ExitWithFailure ( new CommandFailedException ( commandResult . Message ) ) ;
38- }
39- catch ( Exception exception )
40- {
41- ExitWithFailure ( exception ) ;
42- }
43- } ) ;
51+ if ( commandResult . IsSuccessful )
52+ ExitWithSuccess ( commandResult . Message ) ;
53+ else
54+ ExitWithFailure ( new CommandFailedException ( commandResult . Message ) ) ;
55+ }
56+ catch ( Exception exception )
57+ {
58+ ExitWithFailure ( exception ) ;
59+ }
60+ } ) ;
4461
45- if ( ! isSuccessful )
46- ExitWithFailure ( ) ;
62+ if ( ! isSuccessful )
63+ ExitWithFailure ( ) ;
64+ }
4765 }
4866
4967 public void ExitWithSuccess ( string message )
5068 {
51- Logger . Debug ( "Command successful: " + message ) ;
69+ if ( string . IsNullOrWhiteSpace ( message ) )
70+ message = "success" ;
71+ Logger . Debug ( $ "✔ { message } ") ;
5272 Environment . Exit ( 0 ) ;
5373 }
5474
5575 public void ExitWithFailure ( Exception exception = null )
5676 {
57- if ( exception != null )
58- Logger . Error ( exception , "Command failed" ) ;
77+ Logger . Error ( exception != null ? exception : new CommandFailedException ( ) , "✘ fail" ) ;
5978 Environment . Exit ( CommandLine . Parser . DefaultExitCodeFail ) ;
6079 }
6180 }
0 commit comments