Skip to content

Commit e91497e

Browse files
committed
Added InferName option for Switches
1 parent 90cec2d commit e91497e

5 files changed

Lines changed: 57 additions & 22 deletions

File tree

README.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,41 @@ using Codeaddicts.libArgument.Attributes;
1818
// A simple class with some command-line options
1919
public class MyOptions
2020
{
21-
// -i path/to/file
21+
// --str "Hello, World!"
22+
[Argument]
23+
public string str;
24+
2225
// --input path/to/file
23-
[Argument ("i", "input")]
26+
[Argument ("input")]
2427
public string Input;
2528

2629
// -o path/to/file
2730
// --output path/to/file
2831
[Argument ("o", "output")]
2932
public string Output;
30-
31-
// --log
32-
[Switch ("", "log")]
33-
public bool EnableLog;
34-
33+
34+
// --test "Test"
35+
// --woop "Test"
36+
[Argument ("test")]
37+
[Argument ("woop")]
38+
public string Test;
39+
3540
// --num 123
36-
[Argument ("", "num")]
37-
[CastAs (CastingType.UInt64)]
41+
[Argument ("num")]
3842
public UInt64 ANumber;
43+
44+
// --log
45+
[Switch]
46+
public bool log;
47+
48+
// --enable-something
49+
[Switch ("enable-something")]
50+
public bool Something;
51+
52+
// -a
53+
// --annoyme
54+
[Switch ("a", "annoyme")]
55+
public bool AnotherSwitch;
3956
}
4057

4158
public class Program
@@ -49,11 +66,12 @@ public class Program
4966
Console.WriteLine ("Input file: " + options.Input);
5067
Console.WriteLine ("Output file: " + options.Output);
5168
Console.WriteLine ("Your number: " + options.ANumber);
69+
...
5270
}
5371
}
5472
```
5573

56-
Now call the application like that:
74+
Now call the application like that:
5775
$ MyApp.exe -i test -o test --log --num 123 or
5876
$ MyApp.exe --input test --output test --log --num 123
5977

src/libArgument/libArgument.Tests/NewFeatureTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ public class Options {
1414

1515
[Argument]
1616
public int num;
17+
18+
[Switch]
19+
public bool something;
1720
}
1821

1922
[Test]
2023
public void TestInferName () {
21-
var args = new string[] { "--msg", "Test", "--num", "1234" };
24+
var args = new string[] { "--msg", "Test", "--num", "1234", "--something" };
2225
var options = ArgumentParser.Parse<Options> (args);
2326
StringAssert.AreEqualIgnoringCase ("Test", options.msg);
2427
Assert.AreEqual (1234, options.num);
28+
Assert.That (options.something);
2529
}
2630
}
2731
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<Properties StartupItem="libArgument.Tests\libArgument.Tests.csproj">
22
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
3-
<MonoDevelop.Ide.Workbench ActiveDocument="libArgument\ArgumentParser.cs">
3+
<MonoDevelop.Ide.Workbench ActiveDocument="libArgument.Tests\NewFeatureTests.cs">
44
<Files>
5-
<File FileName="libArgument\ArgumentParser.cs" Line="1" Column="1" />
6-
<File FileName="libArgument.Tests\Test.cs" Line="40" Column="40" />
5+
<File FileName="libArgument\ArgumentParser.cs" Line="7" Column="7" />
6+
<File FileName="libArgument.Tests\NewFeatureTests.cs" Line="36" Column="36" />
77
</Files>
88
<Pads>
99
<Pad Id="MonoDevelop.NUnit.TestPad">
10-
<State expanded="True">
10+
<State expanded="True" selected="True">
1111
<Node name="libArgument.Tests" expanded="True">
1212
<Node name="Codeaddicts" expanded="True">
1313
<Node name="libArgument" expanded="True">
1414
<Node name="Tests" expanded="True">
15-
<Node name="Tests+GeneralTests" expanded="True" selected="True" />
15+
<Node name="GeneralTests" expanded="True" />
16+
<Node name="NewFeatureTests" expanded="True" />
1617
</Node>
1718
</Node>
1819
</Node>
@@ -22,10 +23,7 @@
2223
</Pads>
2324
</MonoDevelop.Ide.Workbench>
2425
<MonoDevelop.Ide.DebuggingService.Breakpoints>
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>
26+
<BreakpointStore />
2927
</MonoDevelop.Ide.DebuggingService.Breakpoints>
3028
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
3129
</Properties>

src/libArgument/libArgument/ArgumentParser.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public static class ArgumentParser
7070
var @switch = attrib as Switch;
7171
if (@switch != null) {
7272

73+
// Check if infername is set to true
74+
if (@switch.infername)
75+
76+
// Instantiate a new Switch with the name of the field
77+
@switch = @switch.InferName (field.Name);
78+
7379
// Check if the current attribute contains a valid parameter name
7480
if (args.Contains (@switch.FriendlyShort) || args.Contains (@switch.FriendlyFull)) {
7581

@@ -89,11 +95,10 @@ public static class ArgumentParser
8995
continue;
9096

9197
// Check if infername is set to true
92-
if (attribute.infername) {
98+
if (attribute.infername)
9399

94100
// Instantiate a new Attribute with the name of the field
95101
attribute = attribute.InferName (field.Name);
96-
}
97102

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

src/libArgument/libArgument/Attributes/Switch.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ namespace Codeaddicts.libArgument.Attributes
55
[AttributeUsage (AttributeTargets.Field, AllowMultiple = true)]
66
public class Switch : Argument
77
{
8+
public Switch () : base () {
9+
}
10+
11+
public Switch (string fullname) : base (fullname) {
12+
}
13+
814
public Switch (string shortname, string fullname) : base (shortname, fullname) {
915
}
16+
17+
public Switch InferName (string name) {
18+
return new Switch (name);
19+
}
1020
}
1121
}
1222

0 commit comments

Comments
 (0)