Skip to content

Commit 797c0a3

Browse files
committed
Latest code
1 parent 73afd18 commit 797c0a3

7 files changed

Lines changed: 128 additions & 11 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using Codeaddicts.libArgument;
3+
using Codeaddicts.libArgument.Attributes;
4+
using NUnit.Framework;
5+
6+
namespace Codeaddicts.libArgument.Tests
7+
{
8+
public static class Tests
9+
{
10+
[TestFixture]
11+
public class GeneralTests
12+
{
13+
public class Options {
14+
[Argument ("", "msg")]
15+
public string Msg;
16+
17+
[Switch ("", "enable-log")]
18+
public bool Log;
19+
20+
[Argument ("n", "num")]
21+
[CastAs (CastingType.Int32)]
22+
public int ANumber;
23+
24+
[Argument ("f", "float")]
25+
[CastAs (CastingType.Float)]
26+
public float AFloat;
27+
}
28+
29+
[Test]
30+
public void TestArgument () {
31+
var args = new [] { "--msg", "Hello, World!" };
32+
StringAssert.AreEqualIgnoringCase ("Hello, World!", ArgumentParser.Parse<Options> (args).Msg);
33+
}
34+
35+
[Test]
36+
public void TestEmptyArgument () {
37+
var args = new [] { "--msg" };
38+
Assert.Throws (typeof(ArgumentOutOfRangeException), () => ArgumentParser.Parse<Options> (args));
39+
}
40+
41+
[Test]
42+
public void TestSwitch () {
43+
var args = new [] { "--enable-log" };
44+
Assert.AreEqual (true, ArgumentParser.Parse<Options> (args).Log);
45+
}
46+
47+
[Test]
48+
public void TestConversion () {
49+
var args = new [] { "-n", "123", "-f", "3.1416" };
50+
var options = ArgumentParser.Parse<Options> (args);
51+
Assert.AreEqual (123, options.ANumber);
52+
Assert.AreEqual (3.1416f, options.AFloat);
53+
}
54+
}
55+
}
56+
}
57+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{48CD96FD-270B-491B-83F6-6E2ECD07DE29}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<RootNamespace>Codeaddicts.libArgument.Tests</RootNamespace>
9+
<AssemblyName>libArgument.Tests</AssemblyName>
10+
</PropertyGroup>
11+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
12+
<DebugSymbols>true</DebugSymbols>
13+
<DebugType>full</DebugType>
14+
<Optimize>false</Optimize>
15+
<OutputPath>bin\Debug</OutputPath>
16+
<DefineConstants>DEBUG;</DefineConstants>
17+
<ErrorReport>prompt</ErrorReport>
18+
<WarningLevel>4</WarningLevel>
19+
<ConsolePause>false</ConsolePause>
20+
</PropertyGroup>
21+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
22+
<DebugType>full</DebugType>
23+
<Optimize>true</Optimize>
24+
<OutputPath>bin\Release</OutputPath>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
<ConsolePause>false</ConsolePause>
28+
</PropertyGroup>
29+
<ItemGroup>
30+
<Reference Include="System" />
31+
<Reference Include="nunit.framework">
32+
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
33+
</Reference>
34+
</ItemGroup>
35+
<ItemGroup>
36+
<Compile Include="Test.cs" />
37+
</ItemGroup>
38+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
39+
<ItemGroup>
40+
<None Include="packages.config" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<ProjectReference Include="..\libArgument\libArgument.csproj">
44+
<Project>{FE5B4B56-39AB-49C0-8274-8ADC348F4185}</Project>
45+
<Name>libArgument</Name>
46+
</ProjectReference>
47+
</ItemGroup>
48+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="NUnit" version="2.6.3" targetFramework="net45" />
4+
</packages>

src/libArgument/libArgument.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 2012
44
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "libArgument", "libArgument\libArgument.csproj", "{FE5B4B56-39AB-49C0-8274-8ADC348F4185}"
55
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "libArgument.Tests", "libArgument.Tests\libArgument.Tests.csproj", "{48CD96FD-270B-491B-83F6-6E2ECD07DE29}"
7+
EndProject
68
Global
79
GlobalSection(SolutionConfigurationPlatforms) = preSolution
810
Debug|Any CPU = Debug|Any CPU
911
Release|Any CPU = Release|Any CPU
1012
EndGlobalSection
1113
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{48CD96FD-270B-491B-83F6-6E2ECD07DE29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{48CD96FD-270B-491B-83F6-6E2ECD07DE29}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{48CD96FD-270B-491B-83F6-6E2ECD07DE29}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{48CD96FD-270B-491B-83F6-6E2ECD07DE29}.Release|Any CPU.Build.0 = Release|Any CPU
1218
{FE5B4B56-39AB-49C0-8274-8ADC348F4185}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1319
{FE5B4B56-39AB-49C0-8274-8ADC348F4185}.Debug|Any CPU.Build.0 = Debug|Any CPU
1420
{FE5B4B56-39AB-49C0-8274-8ADC348F4185}.Release|Any CPU.ActiveCfg = Release|Any CPU

src/libArgument/libArgument/ArgumentParser.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,27 @@ public static class ArgumentParser
2222
static void ParseField<T> (T options, List<string> args, string name) where T : class, new()
2323
{
2424
var field = typeof(T).GetField (name);
25-
var attributes = field.GetCustomAttributes (false);
26-
var enumerable = attributes as object[] ?? attributes.ToArray ();
27-
var cast = enumerable.First (attrib => attrib as CastAs != null) as CastAs ?? new CastAs (CastingType.String);
28-
foreach (var attrib in enumerable) {
29-
if (attrib is Switch) {
25+
var attributes = field.GetCustomAttributes (true);
26+
var cast = attributes.FirstOrDefault (attrib => attrib as CastAs != null) as CastAs ?? new CastAs (CastingType.String);
27+
foreach (var attrib in attributes) {
28+
if (attrib as Switch != null) {
3029
field.SetValue (options, true);
3130
return;
3231
}
33-
if (!(attrib is Argument && attrib as Argument != null))
32+
if (attrib as Argument == null)
3433
continue;
3534
var attribute = attrib as Argument;
36-
if (!args.Contains (attribute.FriendlyShort) || args.Contains (attribute.FriendlyFull))
35+
if (!(args.Contains (attribute.FriendlyShort) || args.Contains (attribute.FriendlyFull)))
3736
continue;
3837
var str = args.Contains (attribute.FriendlyShort) ? attribute.FriendlyShort : attribute.FriendlyFull;
39-
var index = 1 + args.IndexOf (str);
38+
var index = args.IndexOf (str) + 1;
4039
var indexInRange = index <= args.Count - 1;
4140
if (cast.Type != CastingType.Boolean && !indexInRange)
4241
throw new ArgumentOutOfRangeException (string.Format ("Parameter of argument {0} out of range.", str));
4342
switch (cast.Type) {
43+
case CastingType.String:
44+
field.SetValue (options, args [index]);
45+
return;
4446
case CastingType.Boolean:
4547
bool bool_result;
4648
if (!Boolean.TryParse (args [index], out bool_result))

src/libArgument/libArgument/Attributes/Argument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public class Argument : Attribute
88
readonly string shortname;
99
readonly string fullname;
1010

11-
public string FriendlyShort { get { return string.Format ("-{0}", shortname); } }
12-
public string FriendlyFull { get { return string.Format ("--{0}", fullname); } }
11+
public string FriendlyShort { get { return string.Format ("{0}{1}", string.IsNullOrEmpty (shortname) ? "" : "-", shortname); } }
12+
public string FriendlyFull { get { return string.Format ("{0}{1}", string.IsNullOrEmpty (fullname) ? "" : "--", fullname); } }
1313

1414
public Argument (string shortname, string fullname) {
1515
this.shortname = shortname;

src/libArgument/libArgument/libArgument.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<MonoDevelop>
4747
<Properties>
4848
<Policies>
49-
<DotNetNamingPolicy DirectoryNamespaceAssociation="Hierarchical" ResourceNamePolicy="FileFormatDefault" />
49+
<DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="FileFormatDefault" />
5050
</Policies>
5151
</Properties>
5252
</MonoDevelop>

0 commit comments

Comments
 (0)