Skip to content

Commit 273e875

Browse files
authored
Usage help commands (#20)
* Update test * Rename to description * Fix codefactor issues in tests, renaming. * Codefactor fixes * Sort usings * Better error message * Change command to only have a name instead of both short and long name. * SMall refactor of arg manager. * Add convention options * Update command * Generic command builder exposed wrong return type. * Add subcommand test * Add subcommands * Subcommands fire * Improve usage system * Imrpove subcommand tests * Update sample app. * Improved command system to use correct configure.
1 parent 9c159bb commit 273e875

54 files changed

Lines changed: 1004 additions & 263 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CommandLineParser.Tests/Command/CommandTests.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System.Linq;
2+
23
using MatthiWare.CommandLine;
3-
using MatthiWare.CommandLine.Abstractions;
44
using MatthiWare.CommandLine.Abstractions.Command;
5+
56
using Xunit;
67

78
namespace MatthiWare.CommandLineParser.Tests.Command
89
{
910
public class CommandTests
1011
{
11-
1212
[Fact]
1313
public void ConfiguringCommandsIncreasesTotalCommandInParser()
1414
{
@@ -19,8 +19,8 @@ public void ConfiguringCommandsIncreasesTotalCommandInParser()
1919

2020
Assert.Equal(2, parser.Commands.Count);
2121

22-
Assert.NotNull(parser.Commands.First(cmd => cmd.ShortName.Equals("x")));
23-
Assert.NotNull(parser.Commands.First(cmd => cmd.ShortName.Equals("y")));
22+
Assert.NotNull(parser.Commands.First(cmd => cmd.Name.Equals("x")));
23+
Assert.NotNull(parser.Commands.First(cmd => cmd.Name.Equals("y")));
2424
}
2525

2626
[Fact]
@@ -33,8 +33,8 @@ public void AddOptionLessCommand()
3333

3434
Assert.Equal(2, parser.Commands.Count);
3535

36-
Assert.NotNull(parser.Commands.First(cmd => cmd.ShortName.Equals("x")));
37-
Assert.NotNull(parser.Commands.First(cmd => cmd.ShortName.Equals("y")));
36+
Assert.NotNull(parser.Commands.First(cmd => cmd.Name.Equals("x")));
37+
Assert.NotNull(parser.Commands.First(cmd => cmd.Name.Equals("y")));
3838
}
3939

4040
[Fact]
@@ -46,7 +46,7 @@ public void AddCommandType()
4646

4747
Assert.Equal(1, parser.Commands.Count);
4848

49-
Assert.NotNull(parser.Commands.First(cmd => cmd.ShortName.Equals("-bla")));
49+
Assert.NotNull(parser.Commands.First(cmd => cmd.Name.Equals("bla")));
5050
}
5151

5252
[Fact]
@@ -58,7 +58,7 @@ public void AddCommandTypeWithGenericOption()
5858

5959
Assert.Equal(1, parser.Commands.Count);
6060

61-
Assert.NotNull(parser.Commands.First(cmd => cmd.ShortName.Equals("-bla")));
61+
Assert.NotNull(parser.Commands.First(cmd => cmd.Name.Equals("bla")));
6262
}
6363

6464
private class MyComand : Command<object, object>
@@ -67,14 +67,13 @@ public override void OnConfigure(ICommandConfigurationBuilder builder)
6767
{
6868
base.OnConfigure(builder);
6969

70-
builder.Name("-bla").Required();
70+
builder.Name("bla").Required();
7171
}
7272

7373
public override void OnExecute(object options, object commandOptions)
7474
{
7575
base.OnExecute(options, commandOptions);
7676
}
7777
}
78-
7978
}
8079
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using System.Threading;
3+
using MatthiWare.CommandLine;
4+
using MatthiWare.CommandLine.Abstractions;
5+
using MatthiWare.CommandLine.Abstractions.Command;
6+
using MatthiWare.CommandLine.Core.Attributes;
7+
using Xunit;
8+
9+
namespace MatthiWare.CommandLineParser.Tests.Command
10+
{
11+
public class SubCommandTests
12+
{
13+
[Fact]
14+
public void TestSubCommandWorksCorrectly()
15+
{
16+
var lock1 = new ManualResetEventSlim();
17+
var lock2 = new ManualResetEventSlim();
18+
19+
var containerResolver = new CustomInstantiator(lock1, lock2);
20+
21+
var parser = new CommandLineParser<MainModel>(containerResolver);
22+
23+
var result = parser.Parse(new[] { "main", "-b", "something", "sub", "-i", "15", "-n", "-1" });
24+
25+
Assert.False(result.HasErrors);
26+
27+
Assert.True(lock1.Wait(1000), "MainCommand didn't execute in time.");
28+
Assert.True(lock2.Wait(1000), "SubCommand didn't execute in time.");
29+
}
30+
31+
private class CustomInstantiator : IContainerResolver
32+
{
33+
private readonly ManualResetEventSlim lock1;
34+
private readonly ManualResetEventSlim lock2;
35+
36+
public CustomInstantiator(ManualResetEventSlim lock1, ManualResetEventSlim lock2)
37+
{
38+
this.lock1 = lock1;
39+
this.lock2 = lock2;
40+
}
41+
42+
public T Resolve<T>()
43+
{
44+
if (typeof(T) == typeof(MainCommand))
45+
return (T)Activator.CreateInstance(typeof(T), lock1);
46+
else if (typeof(T) == typeof(SubCommand))
47+
return (T)Activator.CreateInstance(typeof(T), lock2);
48+
else
49+
return default;
50+
}
51+
}
52+
}
53+
54+
public class MainCommand : Command<MainModel, SubModel>
55+
{
56+
private readonly ManualResetEventSlim locker;
57+
58+
public MainCommand(ManualResetEventSlim locker)
59+
{
60+
this.locker = locker;
61+
}
62+
63+
public override void OnConfigure(ICommandConfigurationBuilder<SubModel> builder)
64+
{
65+
builder
66+
.Name("main")
67+
.Required();
68+
}
69+
70+
public override void OnExecute(MainModel options, SubModel commandOptions)
71+
{
72+
base.OnExecute(options, commandOptions);
73+
74+
locker.Set();
75+
}
76+
}
77+
78+
public class SubCommand : Command<MainModel, SubSubModel>
79+
{
80+
private readonly ManualResetEventSlim locker;
81+
82+
public SubCommand(ManualResetEventSlim locker)
83+
{
84+
this.locker = locker;
85+
}
86+
87+
public override void OnConfigure(ICommandConfigurationBuilder<SubSubModel> builder)
88+
{
89+
builder
90+
.Name("sub")
91+
.Required();
92+
}
93+
94+
public override void OnExecute(MainModel options, SubSubModel commandOptions)
95+
{
96+
base.OnExecute(options, commandOptions);
97+
98+
locker.Set();
99+
}
100+
}
101+
102+
public class MainModel
103+
{
104+
[Required, Name("b")]
105+
public string Bla { get; set; }
106+
public MainCommand MainCommand { get; set; }
107+
}
108+
109+
public class SubModel
110+
{
111+
[Required, Name("i")]
112+
public int Item { get; set; }
113+
public SubCommand SubCommand { get; set; }
114+
}
115+
116+
public class SubSubModel
117+
{
118+
[Required, Name("n")]
119+
public int Nothing { get; set; }
120+
}
121+
}

CommandLineParser.Tests/CommandLineModelTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MatthiWare.CommandLine;
22
using MatthiWare.CommandLine.Core.Attributes;
3+
34
using Xunit;
45

56
namespace MatthiWare.CommandLineParser.Tests
@@ -25,7 +26,7 @@ public void TestBasicModel()
2526
Assert.True(message.HasDefault);
2627
Assert.True(message.IsRequired);
2728

28-
Assert.Equal("Help", message.HelpText);
29+
Assert.Equal("Help", message.Description);
2930
}
3031

3132
[Fact]
@@ -35,7 +36,7 @@ public void TestBasicModelWithOverwritingUsingFluentApi()
3536

3637
parser.Configure(_ => _.Message)
3738
.Required(false)
38-
.HelpText("Different");
39+
.Description("Different");
3940

4041
Assert.Equal(1, parser.Options.Count);
4142

@@ -51,12 +52,12 @@ public void TestBasicModelWithOverwritingUsingFluentApi()
5152
Assert.True(message.HasDefault);
5253
Assert.False(message.IsRequired);
5354

54-
Assert.Equal("Different", message.HelpText);
55+
Assert.Equal("Different", message.Description);
5556
}
5657

5758
private class Model
5859
{
59-
[Required, Name("-m", "--message"), DefaultValue("not found"), HelpText("Help")]
60+
[Required, Name("m", "message"), DefaultValue("not found"), Description("Help")]
6061
public string Message { get; set; }
6162
}
6263
}

0 commit comments

Comments
 (0)