33using System . IO ;
44using System . Linq ;
55using System . Reflection ;
6+ using CommandLine ;
67using InEngine . Core ;
78using InEngine . Core . Exceptions ;
89using NLog ;
@@ -35,17 +36,29 @@ public void Interpret(string[] args)
3536 {
3637 if ( options == null )
3738 Environment . Exit ( CommandLine . Parser . DefaultExitCodeFail ) ;
39+
40+ var plugin = plugins . FirstOrDefault ( x => x . Name == options . PlugInName ) ;
41+ if ( plugin == null )
42+ ExitWithFailure ( "Plugin does not exist: " + options . PlugInName ) ;
3843
39- var pluginOptions = plugins . FirstOrDefault ( x => x . Name == options . PlugInName ) . MakeOptions ( ) ;
44+ var pluginOptionList = plugin . MakeOptions ( ) ;
45+
4046 var pluginArgs = args . Skip ( 1 ) . ToArray ( ) ;
4147 if ( ! pluginArgs . ToList ( ) . Any ( ) ) {
4248 // If the plugin's args are empty, print the plugin's help screen and exit.
43- CommandLine . Parser . Default . ParseArguments ( pluginArgs , pluginOptions ) ;
44- Console . WriteLine ( pluginOptions . GetUsage ( "" ) ) ;
49+ foreach ( var pluginOptions in pluginOptionList ) {
50+ CommandLine . Parser . Default . ParseArguments ( pluginArgs , pluginOptions ) ;
51+ Console . WriteLine ( pluginOptions . GetUsage ( "" ) ) ;
52+ }
4553 ExitWithSuccess ( ) ;
4654 }
4755
48- InterpretPluginArguments ( pluginArgs , pluginOptions ) ;
56+ var commandVerbName = pluginArgs . First ( ) ;
57+ foreach ( var ops in pluginOptionList )
58+ foreach ( var prop in ops . GetType ( ) . GetProperties ( ) )
59+ foreach ( object attr in prop . GetCustomAttributes ( true ) )
60+ if ( attr is VerbOptionAttribute commandVerb && ( commandVerb . LongName == commandVerbName || commandVerb . ShortName . ToString ( ) == commandVerbName ) )
61+ InterpretPluginArguments ( pluginArgs , ops ) ;
4962 }
5063 }
5164
@@ -57,6 +70,14 @@ public void ExitWithSuccess(string message = null)
5770 Environment . Exit ( 0 ) ;
5871 }
5972
73+ public void ExitWithFailure ( string message = null )
74+ {
75+ if ( string . IsNullOrWhiteSpace ( message ) )
76+ message = "fail" ;
77+ Logger . Error ( $ "✘ { message } ") ;
78+ Environment . Exit ( CommandLine . Parser . DefaultExitCodeFail ) ;
79+ }
80+
6081 public void ExitWithFailure ( Exception exception = null )
6182 {
6283 Logger . Error ( exception ?? new CommandFailedException ( ) , "✘ fail" ) ;
@@ -104,7 +125,7 @@ public void InterpretPluginArguments(string[] pluginArgs, IOptions pluginOptions
104125 } ) ;
105126
106127 if ( ! isSuccessful )
107- ExitWithFailure ( ) ;
128+ ExitWithFailure ( "Could not parse plugin arguments" ) ;
108129 }
109130 }
110131}
0 commit comments