Skip to content

Commit be82c56

Browse files
committed
Refactor
1 parent de5fe6a commit be82c56

File tree

7 files changed

+87
-51
lines changed

7 files changed

+87
-51
lines changed

src/InEngine.Commands/CommandOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CommandLine;
2+
using CommandLine.Text;
23
using InEngine.Commands.Sample;
34
using InEngine.Core;
45

@@ -14,5 +15,11 @@ public class Options : IOptions
1415

1516
[VerbOption("sample:say-hello")]
1617
public SayHello SayHello { get; set; }
18+
19+
[HelpVerbOption]
20+
public string GetUsage(string verb)
21+
{
22+
return HelpText.AutoBuild(this, verb);
23+
}
1724
}
1825
}

src/InEngine.Core/IOptions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace InEngine.Core
22
{
33
public interface IOptions
4-
{}
4+
{
5+
string GetUsage(string verb);
6+
}
57
}

src/InEngine.Core/InEngine.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="Config.cs" />
8989
<Compile Include="Queue\AbstractBrokerCommand.cs" />
9090
<Compile Include="IOptions.cs" />
91+
<Compile Include="Queue\Options.cs" />
9192
</ItemGroup>
9293
<ItemGroup>
9394
<None Include="packages.config" />

src/InEngine.Core/Queue/Options.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using CommandLine;
2+
using CommandLine.Text;
3+
using InEngine.Core.Queue.Commands;
4+
5+
namespace InEngine.Core.Queue
6+
{
7+
public class Options : IOptions
8+
{
9+
[VerbOption("queue:publish")]
10+
public Publish QueuePublish { get; set; }
11+
12+
[VerbOption("queue:consume")]
13+
public Consume QueueConsume { get; set; }
14+
15+
[VerbOption("queue:length")]
16+
public GetLength QueueGetLength { get; set; }
17+
18+
[VerbOption("queue:clear")]
19+
public ClearAll QueueClearAll { get; set; }
20+
21+
[HelpVerbOption]
22+
public string GetUsage(string verb)
23+
{
24+
return HelpText.AutoBuild(this, verb);
25+
}
26+
}
27+
}

src/InEngine.Net.userprefs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@
22
<MonoDevelop.Ide.ItemProperties.InEngineScheduler PreferredExecutionTarget="MonoDevelop.Default" />
33
<MonoDevelop.Ide.Workbench ActiveDocument="InEngineCli/ArgumentInterpreter.cs">
44
<Files>
5-
<File FileName="InEngineScheduler/Jobs.cs" Line="15" Column="1" />
6-
<File FileName="InEngineCli/ArgumentInterpreter.cs" Line="72" Column="28" />
7-
<File FileName="InEngine.Core/AbstractCommand.cs" Line="40" Column="1" />
8-
<File FileName="InEngineCli/packages.config" Line="8" Column="12" />
9-
<File FileName="InEngine.Core/IOptions.cs" Line="3" Column="22" />
10-
<File FileName="InEngineCli/Program.cs" Line="7" Column="1" />
5+
<File FileName="InEngineCli/ArgumentInterpreter.cs" Line="46" Column="17" />
116
</Files>
127
<Pads>
138
<Pad Id="ProjectPad">
149
<State name="__root__">
1510
<Node name="InEngine.Net" expanded="True">
16-
<Node name="InEngine.Commands" expanded="True">
17-
<Node name="References" expanded="True" />
18-
</Node>
11+
<Node name="InEngine.Commands" expanded="True" />
1912
<Node name="InEngine.Core" expanded="True">
20-
<Node name="Exceptions" expanded="True" />
21-
</Node>
22-
<Node name="InEngineCli" expanded="True">
23-
<Node name="ArgumentInterpreter.cs" selected="True" />
13+
<Node name="Queue" expanded="True" />
2414
</Node>
15+
<Node name="InEngineCli" expanded="True" selected="True" />
2516
<Node name="InEngineScheduler" expanded="True" />
2617
</Node>
2718
</State>
@@ -30,7 +21,10 @@
3021
</MonoDevelop.Ide.Workbench>
3122
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
3223
<MonoDevelop.Ide.DebuggingService.Breakpoints>
33-
<BreakpointStore />
24+
<BreakpointStore>
25+
<Breakpoint file="/Users/hanne01/InEngine.NET/src/InEngine.Commands/CommandOptions.cs" relfile="InEngine.Commands/CommandOptions.cs" line="8" column="1" />
26+
<Breakpoint file="/Users/hanne01/InEngine.NET/src/InEngineCli/ArgumentInterpreter.cs" relfile="InEngineCli/ArgumentInterpreter.cs" line="55" column="1" />
27+
</BreakpointStore>
3428
</MonoDevelop.Ide.DebuggingService.Breakpoints>
3529
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
3630
<MultiItemStartupConfigurations />

src/InEngineCli/ArgumentInterpreter.cs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,49 @@ public void Interpret(string[] args)
2525
{
2626
if (options == null)
2727
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
28+
29+
// Get possible types from plugin assembly.
2830
var targetAssembly = Assembly.LoadFrom(options.PlugInName + ".dll");
2931
var types = targetAssembly.GetTypes();
30-
var optionType = types.FirstOrDefault(x => typeof(IOptions).IsAssignableFrom(x));
32+
var optionType = types.FirstOrDefault(x => x.IsClass && typeof(IOptions).IsAssignableFrom(x));
3133
if (optionType == null)
3234
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
35+
36+
// Create an instance of the plugin's options class.
3337
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) =>
38+
39+
// If the plugin's args are empty, print the plugin's help screen and exit.
40+
var pluginArgs = args.Skip(1).ToArray();
41+
if (!pluginArgs.ToList().Any()) {
42+
CommandLine.Parser.Default.ParseArguments(pluginArgs, pluginOptions);
43+
Console.WriteLine(pluginOptions.GetUsage(""));
44+
ExitWithSuccess();
45+
}
46+
47+
InterpretPluginArguments(pluginArgs, pluginOptions);
48+
}
49+
}
50+
51+
public void ExitWithSuccess(string message = null)
52+
{
53+
if (string.IsNullOrWhiteSpace(message))
54+
message = "success";
55+
Logger.Debug($"✔ {message}");
56+
Environment.Exit(0);
57+
}
58+
59+
public void ExitWithFailure(Exception exception = null)
60+
{
61+
Logger.Error(exception ?? new CommandFailedException(), "✘ fail");
62+
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
63+
}
64+
65+
public void InterpretPluginArguments(string[] pluginArgs, IOptions pluginOptions)
66+
{
67+
var isSuccessful = CommandLine
68+
.Parser
69+
.Default
70+
.ParseArguments(pluginArgs, pluginOptions, (verb, subOptions) =>
3871
{
3972
try
4073
{
@@ -59,23 +92,8 @@ public void Interpret(string[] args)
5992
}
6093
});
6194

62-
if (!isSuccessful)
63-
ExitWithFailure();
64-
}
65-
}
66-
67-
public void ExitWithSuccess(string message)
68-
{
69-
if (string.IsNullOrWhiteSpace(message))
70-
message = "success";
71-
Logger.Debug($"✔ {message}");
72-
Environment.Exit(0);
73-
}
74-
75-
public void ExitWithFailure(Exception exception = null)
76-
{
77-
Logger.Error(exception != null ? exception : new CommandFailedException(), "✘ fail");
78-
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
95+
if (!isSuccessful)
96+
ExitWithFailure();
7997
}
8098
}
8199
}

src/InEngineCli/Options.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,9 @@ namespace InEngineCli
77
{
88
public class Options
99
{
10-
[Option('p', "plugin", Required = true, HelpText = "Plug-In to activate.")]
10+
[Option('p', "plugin", Required = true, HelpText = "Plug-In to activate.", DefaultValue = "InEngine.Core")]
1111
public string PlugInName { get; set; }
1212

13-
14-
[VerbOption("queue:publish")]
15-
public Publish QueuePublish { get; set; }
16-
17-
[VerbOption("queue:consume")]
18-
public Consume QueueConsume { get; set; }
19-
20-
[VerbOption("queue:length")]
21-
public GetLength QueueGetLength { get; set; }
22-
23-
[VerbOption("queue:clear")]
24-
public ClearAll QueueClearAll { get; set; }
25-
2613
[HelpVerbOption]
2714
public string GetUsage(string verb)
2815
{

0 commit comments

Comments
 (0)