11using System ;
22using System . Linq ;
33using CommandLine ;
4+ using CommandLine . Text ;
45using InEngine . Core ;
56using InEngine . Core . Exceptions ;
67using NLog ;
8+ using InEngine . Core . IO ;
79
810namespace InEngineCli
911{
1012 public class ArgumentInterpreter
1113 {
1214 public Logger Logger { get ; set ; }
13-
15+ public string CliLogo { get ; set ; }
1416 public ArgumentInterpreter ( )
1517 {
1618 Logger = LogManager . GetCurrentClassLogger ( ) ;
19+ CliLogo = @"
20+ ___ _____ _ _ _ _____ _____
21+ |_ _|_ __ | ____|_ __ __ _(_)_ __ ___ | \ | | ____|_ _|
22+ | || '_ \| _| | '_ \ / _` | | '_ \ / _ \ | \| | _| | |
23+ | || | | | |___| | | | (_| | | | | | __/_| |\ | |___ | |
24+ |___|_| |_|_____|_| |_|\__, |_|_| |_|\___(_|_| \_|_____| |_|
25+ |___/
26+ " ;
1727 }
1828
1929 public void Interpret ( string [ ] args )
2030 {
31+ var write = new Write ( ) ;
2132 var plugins = Plugin . Discover < IOptions > ( ) ;
2233 if ( ! args . Any ( ) )
2334 {
24- Console . WriteLine ( "Available plugins... " ) ;
25- plugins . ForEach ( x => Console . WriteLine ( x . Name ) ) ;
35+ write . Info ( CliLogo ) ;
36+ write . Warning ( "Usage:" ) ;
37+ write . Line ( $ " -p[<plugin_name>] [<command_name>]") ;
38+ write . Newline ( ) ;
39+ write . Warning ( "Plugins:" ) ;
40+ plugins . ForEach ( x => Console . WriteLine ( $ " { x . Name } ") ) ;
2641 ExitWithSuccess ( ) ;
2742 }
2843
@@ -39,13 +54,30 @@ public void Interpret(string[] args)
3954 ExitWithFailure ( "Plugin does not exist: " + options . PluginName ) ;
4055
4156 var pluginOptionList = plugin . Make < IOptions > ( ) ;
42-
4357 var pluginArgs = args . Skip ( 1 ) . ToArray ( ) ;
4458 if ( ! pluginArgs . ToList ( ) . Any ( ) ) {
59+ write . Info ( CliLogo ) ;
60+
61+ write . WarningText ( "Plugin: " ) ;
62+ write . LineText ( $ "{ plugin . Name } ") ;
63+ write . Newline ( ) . Newline ( ) ;
64+ write . Warning ( "Commands:" ) ;
4565 // If the plugin's args are empty, print the plugin's help screen and exit.
4666 foreach ( var pluginOptions in pluginOptionList ) {
4767 Parser . Default . ParseArguments ( pluginArgs , pluginOptions ) ;
48- Console . WriteLine ( pluginOptions . GetUsage ( "" ) ) ;
68+
69+ var verbs = pluginOptions
70+ . GetType ( )
71+ . GetProperties ( )
72+ . SelectMany ( x => x . GetCustomAttributes ( true ) )
73+ . Where ( x => x is BaseOptionAttribute )
74+ . ToList ( ) ;
75+
76+ verbs . ForEach ( x => {
77+ var optionAttribute = ( x as BaseOptionAttribute ) ;
78+ Console . WriteLine ( $ " { optionAttribute . LongName } \t { optionAttribute . HelpText } ") ;
79+ } ) ;
80+
4981 }
5082 ExitWithSuccess ( ) ;
5183 }
@@ -70,13 +102,15 @@ public void ExitWithSuccess(string message = null)
70102 public void ExitWithFailure ( string message = null )
71103 {
72104 Logger . Error ( MakeErrorMessage ( message ) ) ;
105+ new Write ( ) . Error ( message ) ;
73106 Environment . Exit ( Parser . DefaultExitCodeFail ) ;
74107 }
75108
76109 public void ExitWithFailure ( Exception exception = null )
77110 {
78111 var ex = exception ?? new Exception ( "Unspecified failure" ) ;
79112 Logger . Error ( ex , MakeErrorMessage ( ex . Message ) ) ;
113+ new Write ( ) . Error ( ex . Message ) ;
80114 Environment . Exit ( Parser . DefaultExitCodeFail ) ;
81115 }
82116
@@ -92,7 +126,10 @@ public void InterpretPluginArguments(string[] pluginArgs, IOptions pluginOptions
92126 var isSuccessful = Parser . Default . ParseArguments ( pluginArgs , pluginOptions , ( verb , subOptions ) => {
93127 try
94128 {
95- if ( subOptions == null )
129+ var lastArg = pluginArgs . ToList ( ) . LastOrDefault ( ) ;
130+ if ( subOptions == null && ( lastArg == "-h" || lastArg == "--help" ) )
131+ ExitWithSuccess ( ) ;
132+ else if ( subOptions == null )
96133 ExitWithFailure ( new CommandFailedException ( "Could not parse plugin options" ) ) ;
97134
98135 var command = subOptions as ICommand ;
0 commit comments