Skip to content

Commit 90cec2d

Browse files
committed
Added the option to infer names
1 parent 711df50 commit 90cec2d

7 files changed

Lines changed: 143 additions & 76 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using Codeaddicts.libArgument;
3+
using Codeaddicts.libArgument.Attributes;
4+
using NUnit.Framework;
5+
6+
namespace Codeaddicts.libArgument.Tests
7+
{
8+
[TestFixture]
9+
public class GeneralTests
10+
{
11+
public class Options {
12+
[Argument ("", "msg")]
13+
public string Msg = "Test";
14+
15+
[Switch ("", "enable-log")]
16+
public bool Log;
17+
18+
[Argument ("n", "num")]
19+
public int ANumber;
20+
21+
[Argument ("f", "float")]
22+
public float AFloat;
23+
24+
[Argument ("b", "bool")]
25+
public bool ABool;
26+
}
27+
28+
[Test]
29+
public void TestArgument () {
30+
var args = new [] { "--msg", "Hello, World!" };
31+
StringAssert.AreEqualIgnoringCase ("Hello, World!", ArgumentParser.Parse<Options> (args).Msg);
32+
}
33+
34+
[Test]
35+
public void TestDefaultArgument () {
36+
StringAssert.AreEqualIgnoringCase ("Test", ArgumentParser.Parse<Options> (new string[] {}).Msg);
37+
}
38+
39+
[Test]
40+
public void TestDefaultBoolArgument () {
41+
Assert.IsFalse (ArgumentParser.Parse<Options> (new string[] {}).ABool);
42+
}
43+
44+
[Test]
45+
public void TestEmptyArgument () {
46+
var args = new [] { "--msg" };
47+
Assert.Throws (typeof(ArgumentOutOfRangeException), () => ArgumentParser.Parse<Options> (args));
48+
}
49+
50+
[Test]
51+
public void TestSwitch () {
52+
var args = new [] { "--enable-log" };
53+
Assert.AreEqual (true, ArgumentParser.Parse<Options> (args).Log);
54+
}
55+
56+
[Test]
57+
public void TestConversion () {
58+
var args = new [] { "-n", "123", "-f", "3.1416", "--bool", "true" };
59+
var options = ArgumentParser.Parse<Options> (args);
60+
Assert.AreEqual (123, options.ANumber);
61+
Assert.AreEqual (3.1416f, options.AFloat);
62+
Assert.That (options.ABool);
63+
}
64+
}
65+
}
66+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using Codeaddicts.libArgument;
3+
using Codeaddicts.libArgument.Attributes;
4+
using NUnit.Framework;
5+
6+
namespace Codeaddicts.libArgument.Tests
7+
{
8+
[TestFixture]
9+
public class NewFeatureTests
10+
{
11+
public class Options {
12+
[Argument]
13+
public string msg;
14+
15+
[Argument]
16+
public int num;
17+
}
18+
19+
[Test]
20+
public void TestInferName () {
21+
var args = new string[] { "--msg", "Test", "--num", "1234" };
22+
var options = ArgumentParser.Parse<Options> (args);
23+
StringAssert.AreEqualIgnoringCase ("Test", options.msg);
24+
Assert.AreEqual (1234, options.num);
25+
}
26+
}
27+
}
28+

src/libArgument/libArgument.Tests/Test.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/libArgument/libArgument.Tests/libArgument.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
</Reference>
3434
</ItemGroup>
3535
<ItemGroup>
36-
<Compile Include="Test.cs" />
36+
<Compile Include="GeneralTests.cs" />
37+
<Compile Include="NewFeatureTests.cs" />
3738
</ItemGroup>
3839
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
3940
<ItemGroup>

src/libArgument/libArgument.userprefs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@
22
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
33
<MonoDevelop.Ide.Workbench ActiveDocument="libArgument\ArgumentParser.cs">
44
<Files>
5-
<File FileName="libArgument\ArgumentParser.cs" Line="123" Column="123" />
6-
<File FileName="libArgument\Attributes\CastAs.cs" Line="3" Column="3" />
5+
<File FileName="libArgument\ArgumentParser.cs" Line="1" Column="1" />
6+
<File FileName="libArgument.Tests\Test.cs" Line="40" Column="40" />
77
</Files>
8+
<Pads>
9+
<Pad Id="MonoDevelop.NUnit.TestPad">
10+
<State expanded="True">
11+
<Node name="libArgument.Tests" expanded="True">
12+
<Node name="Codeaddicts" expanded="True">
13+
<Node name="libArgument" expanded="True">
14+
<Node name="Tests" expanded="True">
15+
<Node name="Tests+GeneralTests" expanded="True" selected="True" />
16+
</Node>
17+
</Node>
18+
</Node>
19+
</Node>
20+
</State>
21+
</Pad>
22+
</Pads>
823
</MonoDevelop.Ide.Workbench>
924
<MonoDevelop.Ide.DebuggingService.Breakpoints>
10-
<BreakpointStore />
25+
<BreakpointStore>
26+
<RunToCursorBreakpoint file="C:\Users\Splitty\Documents\GitHub\libArgument\src\libArgument\libArgument\Attributes\CastAs.cs" line="11" column="11" />
27+
<RunToCursorBreakpoint file="C:\Users\Splitty\Documents\GitHub\libArgument\src\libArgument\libArgument\Attributes\CastAs.cs" line="11" column="13" />
28+
</BreakpointStore>
1129
</MonoDevelop.Ide.DebuggingService.Breakpoints>
1230
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
1331
</Properties>

src/libArgument/libArgument/ArgumentParser.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,21 @@ public static class ArgumentParser
7979
}
8080
}
8181

82+
// Get the current attribute
83+
var attribute = attrib as Argument;
84+
8285
// Check if the current attribute is an Argument attribute
83-
if (attrib as Argument == null)
86+
if (attribute == null)
8487

8588
// Skip this iteration
8689
continue;
8790

88-
// Get the current attribute
89-
var attribute = attrib as Argument;
91+
// Check if infername is set to true
92+
if (attribute.infername) {
93+
94+
// Instantiate a new Attribute with the name of the field
95+
attribute = attribute.InferName (field.Name);
96+
}
9097

9198
// Check if the arguments contain any of the valid parameter names
9299
if (!(args.Contains (attribute.FriendlyShort) || args.Contains (attribute.FriendlyFull)))

src/libArgument/libArgument/Attributes/Argument.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,30 @@ public class Argument : Attribute
77
{
88
readonly string shortname;
99
readonly string fullname;
10+
readonly public bool infername;
1011

1112
public string FriendlyShort { get { return string.Format ("{0}{1}", string.IsNullOrEmpty (shortname) ? "" : "-", shortname); } }
1213
public string FriendlyFull { get { return string.Format ("{0}{1}", string.IsNullOrEmpty (fullname) ? "" : "--", fullname); } }
1314

15+
public Argument () {
16+
shortname = string.Empty;
17+
fullname = string.Empty;
18+
infername = true;
19+
}
20+
21+
public Argument (string fullname) {
22+
shortname = string.Empty;
23+
this.fullname = fullname;
24+
}
25+
1426
public Argument (string shortname, string fullname) {
1527
this.shortname = shortname;
1628
this.fullname = fullname;
1729
}
30+
31+
public Argument InferName (string name) {
32+
return new Argument (name);
33+
}
1834
}
1935
}
2036

0 commit comments

Comments
 (0)