Skip to content

Commit 054bb84

Browse files
committed
+ RunCommand now return FlowCommandResult containing info about result of handling commands
1 parent 02c28a0 commit 054bb84

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed

src/FlowCommandLine/CommandLine.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,31 +123,52 @@ public Task RunCommandAsync () {
123123
/// <summary>
124124
/// Run the command from the command line.
125125
/// </summary>
126-
public void RunCommand () {
126+
public FlowCommandResult RunCommand () {
127127
if ( m_asyncCommands.Any () ) throw new ArgumentException ( "You have asynchronized commands, you need to use RunCommandAsync method inside!" );
128128

129129
if ( IsVersion () ) {
130130
ShowVersion ();
131-
return;
131+
132+
return new FlowCommandResult {
133+
EmptyInput = string.IsNullOrEmpty ( m_commandLine ),
134+
CommandHandled = false,
135+
Handled = true
136+
};
132137
}
133138

134139
var parts = GetParts ();
135140
if ( string.IsNullOrEmpty ( m_commandLine ) || !parts.Any () || IsHelpParameter ( m_commandLine ) || parts.Any ( IsHelpParameter ) ) {
136141
ShowHelp ( parts, true );
137-
return;
142+
143+
return new FlowCommandResult {
144+
EmptyInput = string.IsNullOrEmpty ( m_commandLine ),
145+
CommandHandled = false,
146+
Handled = parts.Any ()
147+
};
138148
}
139149

140150
ParseParameters ( parts, out var command, out var parameters );
141151

142152
try {
143153
if ( m_commands.TryGetValue ( command, out var flowCommand ) ) {
144154
flowCommand.Execute ( parameters, m_commandLineProvider );
145-
return;
155+
156+
return new FlowCommandResult {
157+
EmptyInput = string.IsNullOrEmpty ( m_commandLine ),
158+
CommandHandled = true,
159+
Handled = false
160+
};
146161
}
147162
} catch {
148163
}
149164

150165
ShowHelp ( parts );
166+
167+
return new FlowCommandResult {
168+
EmptyInput = string.IsNullOrEmpty ( m_commandLine ),
169+
CommandHandled = false,
170+
Handled = false
171+
};
151172
}
152173

153174
public CommandLine AddOption ( string shortName = "", string fullName = "", string description = "", string propertyName = "", bool required = false ) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace FlowCommandLine {
2+
3+
public class FlowCommandResult {
4+
5+
/// <summary>
6+
/// It was empty input.
7+
/// Can be useful if you need to make default behaviour if command line was empty.
8+
/// </summary>
9+
public bool EmptyInput { get; set; }
10+
11+
/// <summary>
12+
/// Command was found handled.
13+
/// </summary>
14+
public bool CommandHandled { get; set; }
15+
16+
/// <summary>
17+
/// Was handed but not command (as example --version, --help etc)
18+
/// </summary>
19+
public bool Handled { get; set; }
20+
21+
}
22+
23+
}

src/FlowCommandLineTests/CommandLineUnitTests.cs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void RunCommand_Success_Empty_ShowHelp () {
1616
var commandLine = new CommandLine ( fakeProvider );
1717

1818
//act
19-
commandLine
19+
var result = commandLine
2020
.Application ( "TestApplication", "1.0.0" )
2121
.RunCommand ();
2222

@@ -25,6 +25,9 @@ public void RunCommand_Success_Empty_ShowHelp () {
2525
Assert.Equal ( 2, messages.Count );
2626
Assert.Equal ( "TestApplication version 1.0.0", messages.First () );
2727
Assert.Equal ( " ", messages.ElementAt ( 1 ) );
28+
Assert.True ( result.EmptyInput );
29+
Assert.False ( result.Handled );
30+
Assert.False ( result.CommandHandled );
2831
}
2932

3033
[Fact]
@@ -37,7 +40,33 @@ public void RunCommand_Success_OnlyDescription_ShowHelp () {
3740
var commandLine = new CommandLine ( fakeProvider );
3841

3942
//act
40-
commandLine
43+
var result = commandLine
44+
.Application ( "TestApplication", "1.0.0", "Application description!!!!" )
45+
.RunCommand ();
46+
47+
//assert
48+
Assert.NotEmpty ( messages );
49+
Assert.Equal ( 4, messages.Count );
50+
Assert.Equal ( "TestApplication version 1.0.0", messages.First () );
51+
Assert.Equal ( " ", messages.ElementAt ( 1 ) );
52+
Assert.Equal ( "Application description!!!!", messages.ElementAt ( 2 ) );
53+
Assert.Equal ( " ", messages.ElementAt ( 3 ) );
54+
Assert.True ( result.EmptyInput );
55+
Assert.False ( result.Handled );
56+
Assert.False ( result.CommandHandled );
57+
}
58+
59+
[Fact]
60+
public void RunCommand_Success_OnlyDescription_ShowHelp_Parameter () {
61+
//arrange
62+
var messages = new List<string> ();
63+
var fakeProvider = A.Fake<ICommandLineProvider> ();
64+
A.CallTo ( () => fakeProvider.GetCommandLine () ).Returns ( "--help" );
65+
A.CallTo ( () => fakeProvider.WriteLine ( A<string>._ ) ).Invokes ( ( string fake ) => { messages.Add ( fake ); } );
66+
var commandLine = new CommandLine ( fakeProvider );
67+
68+
//act
69+
var result = commandLine
4170
.Application ( "TestApplication", "1.0.0", "Application description!!!!" )
4271
.RunCommand ();
4372

@@ -48,6 +77,9 @@ public void RunCommand_Success_OnlyDescription_ShowHelp () {
4877
Assert.Equal ( " ", messages.ElementAt ( 1 ) );
4978
Assert.Equal ( "Application description!!!!", messages.ElementAt ( 2 ) );
5079
Assert.Equal ( " ", messages.ElementAt ( 3 ) );
80+
Assert.False ( result.EmptyInput );
81+
Assert.True ( result.Handled );
82+
Assert.False ( result.CommandHandled );
5183
}
5284

5385
[Fact]
@@ -60,7 +92,7 @@ public void RunCommand_Success_OnlyExecutable_ShowHelp () {
6092
var commandLine = new CommandLine ( fakeProvider );
6193

6294
//act
63-
commandLine
95+
var result = commandLine
6496
.Application ( "TestApplication", "1.0.0", applicationExecutable: "testapp" )
6597
.RunCommand ();
6698

@@ -71,6 +103,9 @@ public void RunCommand_Success_OnlyExecutable_ShowHelp () {
71103
Assert.Equal ( " ", messages.ElementAt ( 1 ) );
72104
Assert.Equal ( "usage: testapp [<command>] [<parameters>]", messages.ElementAt ( 2 ) );
73105
Assert.Equal ( " ", messages.ElementAt ( 3 ) );
106+
Assert.True ( result.EmptyInput );
107+
Assert.False ( result.Handled );
108+
Assert.False ( result.CommandHandled );
74109
}
75110

76111
public record RunCommand_Success_OnlyCommands_ShowHelp_Class { }
@@ -120,7 +155,7 @@ public void RunCommand_Success_OnlyCommands_SingleCommand () {
120155
var isCorrect = false;
121156

122157
//act
123-
commandLine
158+
var result = commandLine
124159
.Application ( "TestApplication", "1.0.0" )
125160
.AddCommand (
126161
"test",
@@ -140,6 +175,9 @@ public void RunCommand_Success_OnlyCommands_SingleCommand () {
140175

141176
//assert
142177
Assert.True ( isCorrect );
178+
Assert.False ( result.EmptyInput );
179+
Assert.False ( result.Handled );
180+
Assert.True ( result.CommandHandled );
143181
}
144182

145183
public record RunCommand_Success_IntLongDoubleFloatTypes_Class {
@@ -1144,7 +1182,7 @@ public void RunOptions_Success_ComplexParameters () {
11441182
"",
11451183
databaseAdjustments
11461184
)
1147-
.RunCommand();
1185+
.RunCommand ();
11481186

11491187
//assert
11501188
Assert.NotNull ( result );

0 commit comments

Comments
 (0)