Skip to content

Commit ae78187

Browse files
committed
Improve CLI output
1 parent 7f4b518 commit ae78187

File tree

10 files changed

+146
-18
lines changed

10 files changed

+146
-18
lines changed

src/InEngine.Commands/MoreOptions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ namespace InEngine.Commands
77
{
88
public class MoreOptions : IOptions
99
{
10-
[VerbOption("sample:show-progress")]
10+
[VerbOption("sample:show-progress", HelpText = "A sample command to demonstrate the progress bar.")]
1111
public ShowProgress ShowProgress { get; set; }
1212

13-
[VerbOption("sample:say-hello")]
13+
[VerbOption("sample:say-hello", HelpText = "A sample command to say \"hello\".")]
1414
public SayHello SayHello { get; set; }
1515

16-
[HelpVerbOption]
1716
public string GetUsage(string verb)
1817
{
1918
return HelpText.AutoBuild(this, verb);

src/InEngine.Commands/Options.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using CommandLine;
2-
using CommandLine.Text;
32
using InEngine.Commands.Sample;
43
using InEngine.Core;
54

@@ -10,10 +9,9 @@ public class Options : IOptions
109
[VerbOption("sample:minimal")]
1110
public Minimal Minimal { get; set; }
1211

13-
[HelpVerbOption]
1412
public string GetUsage(string verb)
1513
{
16-
return HelpText.AutoBuild(this, verb);
14+
return "\tsample:minimal \t\tA minimal implementation of a command.";
1715
}
1816
}
1917
}

src/InEngine.Commands/Sample/SayHello.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class SayHello : AbstractCommand
77
{
88
public override CommandResult Run()
99
{
10-
Console.WriteLine("hello...");
10+
Console.WriteLine("hello");
1111
return new CommandResult(true);
1212
}
1313
}

src/InEngine.Core/Commands/Options.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ public class Options : IOptions
1111
[VerbOption("echo", HelpText= "Echo some text to the console. Useful for end-to-end testing.")]
1212
public Echo Echo { get; set; }
1313

14-
[HelpVerbOption]
1514
public string GetUsage(string verb)
1615
{
17-
return HelpText.AutoBuild(this, verb);
16+
return null;
1817
}
1918
}
2019
}

src/InEngine.Core/IO/IWrite.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace InEngine.Core.IO
4+
{
5+
public interface IWrite
6+
{
7+
void Info(string val);
8+
void Warning(string val);
9+
void Error(string val);
10+
void Text(string val);
11+
void Line(string val, ConsoleColor consoleColor = ConsoleColor.White);
12+
}
13+
}

src/InEngine.Core/IO/Write.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
3+
namespace InEngine.Core.IO
4+
{
5+
public class Write
6+
{
7+
public ConsoleColor InfoColor { get; set; } = ConsoleColor.Green;
8+
public ConsoleColor WarningColor { get; set; } = ConsoleColor.Yellow;
9+
public ConsoleColor ErrorColor { get; set; } = ConsoleColor.Red;
10+
public ConsoleColor LineColor { get; set; } = ConsoleColor.White;
11+
12+
public Write Newline()
13+
{
14+
Console.WriteLine();
15+
return this;
16+
}
17+
18+
public Write Info(string val)
19+
{
20+
ColoredLine(val, InfoColor);
21+
return this;
22+
}
23+
24+
public Write Error(string val)
25+
{
26+
ColoredLine(val, ErrorColor);
27+
return this;
28+
}
29+
30+
public Write Warning(string val)
31+
{
32+
ColoredLine(val, WarningColor);
33+
return this;
34+
}
35+
36+
public Write Line(string val)
37+
{
38+
ColoredLine(val, LineColor);
39+
return this;
40+
}
41+
42+
public static void ColoredLine(string val, ConsoleColor consoleColor)
43+
{
44+
Console.ForegroundColor = consoleColor;
45+
Console.WriteLine(val);
46+
Console.ResetColor();
47+
}
48+
49+
50+
public Write InfoText(string val)
51+
{
52+
ColoredText(val, InfoColor);
53+
return this;
54+
}
55+
56+
public Write ErrorText(string val)
57+
{
58+
ColoredText(val, ErrorColor);
59+
return this;
60+
}
61+
62+
public Write WarningText(string val)
63+
{
64+
ColoredText(val, WarningColor);
65+
return this;
66+
}
67+
68+
public Write LineText(string val)
69+
{
70+
ColoredText(val, LineColor);
71+
return this;
72+
}
73+
74+
public static void ColoredText(string val, ConsoleColor consoleColor)
75+
{
76+
Console.ForegroundColor = consoleColor;
77+
Console.Write(val);
78+
Console.ResetColor();
79+
}
80+
}
81+
}

src/InEngine.Core/InEngine.Core.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
<Compile Include="Commands\Echo.cs" />
134134
<Compile Include="Commands\Null.cs" />
135135
<Compile Include="Commands\Options.cs" />
136+
<Compile Include="IO\Write.cs" />
137+
<Compile Include="IO\IWrite.cs" />
136138
</ItemGroup>
137139
<ItemGroup>
138140
<None Include="packages.config" />
@@ -144,6 +146,7 @@
144146
<Folder Include="Queue\" />
145147
<Folder Include="Queue\Commands\" />
146148
<Folder Include="Commands\" />
149+
<Folder Include="IO\" />
147150
</ItemGroup>
148151
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
149152
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

src/InEngine.Core/Plugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Plugin
1212
{
1313
public Assembly Assembly { get; set; }
1414
public string Name { get { return Assembly.GetName().Name; } }
15+
public string Version { get { return Assembly.GetName().Version.ToString(); } }
1516

1617
public Plugin(Assembly assembly)
1718
{

src/InEngineCli/ArgumentInterpreter.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
using System;
22
using System.Linq;
33
using CommandLine;
4+
using CommandLine.Text;
45
using InEngine.Core;
56
using InEngine.Core.Exceptions;
67
using NLog;
8+
using InEngine.Core.IO;
79

810
namespace 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;

src/InEngineCli/InEngineCli.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@
4747
<Reference Include="System.Data" />
4848
<Reference Include="System.Net.Http" />
4949
<Reference Include="System.Xml" />
50-
<Reference Include="Konsole">
51-
<HintPath>..\packages\Goblinfactory.Konsole.ProgressBar.Core.1.0.2\lib\netstandard1.4\Konsole.dll</HintPath>
52-
</Reference>
5350
<Reference Include="CommandLine">
5451
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
5552
</Reference>

0 commit comments

Comments
 (0)