Skip to content

Commit 87043db

Browse files
committed
Added functionality.
1 parent 7776fc1 commit 87043db

9 files changed

Lines changed: 260 additions & 0 deletions

File tree

src/libArgument/libArgument.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2012
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "libArgument", "libArgument\libArgument.csproj", "{FE5B4B56-39AB-49C0-8274-8ADC348F4185}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{FE5B4B56-39AB-49C0-8274-8ADC348F4185}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{FE5B4B56-39AB-49C0-8274-8ADC348F4185}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{FE5B4B56-39AB-49C0-8274-8ADC348F4185}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{FE5B4B56-39AB-49C0-8274-8ADC348F4185}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
EndGlobal
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Properties StartupItem="libArgument\libArgument.csproj">
2+
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
3+
<MonoDevelop.Ide.Workbench ActiveDocument="libArgument\Attributes\Switch.cs">
4+
<Files>
5+
<File FileName="libArgument\ArgumentParser.cs" Line="3" Column="3" />
6+
<File FileName="libArgument\Attributes\Switch.cs" Line="1" Column="1" />
7+
</Files>
8+
</MonoDevelop.Ide.Workbench>
9+
<MonoDevelop.Ide.DebuggingService.Breakpoints>
10+
<BreakpointStore />
11+
</MonoDevelop.Ide.DebuggingService.Breakpoints>
12+
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
13+
</Properties>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Linq;
5+
6+
namespace libArgument
7+
{
8+
public static class ArgumentParser
9+
{
10+
public static T Parse<T> (string[] args) where T : class, new()
11+
{
12+
T options = new T ();
13+
var list = new List<string> (args);
14+
var fields = typeof (T).GetFields (BindingFlags.Instance | BindingFlags.Public);
15+
foreach (var item in fields)
16+
ParseField<T> (options, list, item.Name);
17+
return options;
18+
}
19+
20+
static void ParseField<T> (T options, List<string> args, string name) where T : class, new()
21+
{
22+
var field = typeof(T).GetField (name);
23+
var attributes = field.GetCustomAttributes ();
24+
var enumerable = attributes as object[] ?? attributes.ToArray ();
25+
var cast = enumerable.First (attrib => attrib as CastAs != null) as CastAs ?? new CastAs (CastingType.String);
26+
foreach (var attrib in enumerable) {
27+
if (attrib is Switch) {
28+
field.SetValue (options, true);
29+
return;
30+
}
31+
if (!(attrib is Argument && attrib as Argument != null))
32+
continue;
33+
var attribute = attrib as Argument;
34+
if (!args.Contains (attribute.FriendlyShort) || args.Contains (attribute.FriendlyFull))
35+
continue;
36+
var str = args.Contains (attribute.FriendlyShort) ? attribute.FriendlyShort : attribute.FriendlyFull;
37+
var index = 1 + args.IndexOf (str);
38+
var indexInRange = index <= args.Count - 1;
39+
if (cast.Type != CastingType.Boolean && !indexInRange)
40+
throw new ArgumentOutOfRangeException (string.Format ("Parameter of argument {0} out of range.", str));
41+
switch (cast.Type) {
42+
case CastingType.Boolean:
43+
var bool_result = false;
44+
if (!Boolean.TryParse (args [index], out bool_result))
45+
throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to Boolean.", str));
46+
field.SetValue (options, bool_result);
47+
return;
48+
case CastingType.Object:
49+
field.SetValue (options, args [index]);
50+
return;
51+
case CastingType.Int32:
52+
var int32_result = 0;
53+
if (!Int32.TryParse (args [index], out int32_result))
54+
throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to Int32.", str));
55+
field.SetValue (options, int32_result);
56+
return;
57+
case CastingType.Int64:
58+
var int64_result = 0L;
59+
if (!Int64.TryParse (args [index], out int64_result))
60+
throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to Int64.", str));
61+
field.SetValue (options, int64_result);
62+
return;
63+
case CastingType.UInt32:
64+
var uint32_result = 0u;
65+
if (!UInt32.TryParse (args [index], out uint32_result))
66+
throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to UInt32.", str));
67+
field.SetValue (options, uint32_result);
68+
return;
69+
case CastingType.UInt64:
70+
var uint64_result = 0uL;
71+
if (!UInt64.TryParse (args [index], out uint64_result))
72+
throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to UInt64.", str));
73+
field.SetValue (options, uint64_result);
74+
return;
75+
case CastingType.Long:
76+
var long_result = 0L;
77+
if (!long.TryParse (args [index], out long_result))
78+
throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to long.", str));
79+
field.SetValue (options, long_result);
80+
return;
81+
case CastingType.ULong:
82+
var ulong_result = 0uL;
83+
if (!ulong.TryParse (args [index], out ulong_result))
84+
throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to ulong.", str));
85+
field.SetValue (options, ulong_result);
86+
return;
87+
}
88+
}
89+
}
90+
}
91+
}
92+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace libArgument
4+
{
5+
[AttributeUsage (AttributeTargets.Field, AllowMultiple = true)]
6+
public class Argument : Attribute
7+
{
8+
readonly string shortname;
9+
readonly string fullname;
10+
11+
public string FriendlyShort { get { return string.Format ("-{0}", shortname); } }
12+
public string FriendlyFull { get { return string.Format ("--{0}", fullname); } }
13+
14+
public Argument (string shortname, string fullname) {
15+
this.shortname = shortname;
16+
this.fullname = fullname;
17+
}
18+
}
19+
}
20+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace libArgument
4+
{
5+
[AttributeUsage (AttributeTargets.Field, AllowMultiple = false)]
6+
public class CastAs : Attribute
7+
{
8+
public CastingType Type;
9+
10+
public CastAs (CastingType type)
11+
{
12+
this.Type = type;
13+
}
14+
}
15+
}
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace libArgument
4+
{
5+
public enum CastingType {
6+
Object = 0,
7+
String,
8+
Boolean,
9+
Int32,
10+
Int64,
11+
UInt32,
12+
UInt64,
13+
Long,
14+
ULong
15+
}
16+
}
17+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace libArgument
4+
{
5+
[AttributeUsage (AttributeTargets.Field, AllowMultiple = true)]
6+
public class Switch : Argument
7+
{
8+
public Switch (string shortname, string fullname) : base (shortname, fullname) {
9+
}
10+
}
11+
}
12+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
// Information about this assembly is defined by the following attributes.
5+
// Change them to the values specific to your project.
6+
7+
[assembly: AssemblyTitle ("libArgument")]
8+
[assembly: AssemblyDescription ("")]
9+
[assembly: AssemblyConfiguration ("")]
10+
[assembly: AssemblyCompany ("splitty.de")]
11+
[assembly: AssemblyProduct ("")]
12+
[assembly: AssemblyCopyright ("SplittyDev")]
13+
[assembly: AssemblyTrademark ("")]
14+
[assembly: AssemblyCulture ("")]
15+
16+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
18+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
19+
20+
[assembly: AssemblyVersion ("1.0.*")]
21+
22+
// The following attributes are used to specify the signing key for the assembly,
23+
// if desired. See the Mono documentation for more information about signing.
24+
25+
//[assembly: AssemblyDelaySign(false)]
26+
//[assembly: AssemblyKeyFile("")]
27+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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>{FE5B4B56-39AB-49C0-8274-8ADC348F4185}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<RootNamespace>libArgument</RootNamespace>
9+
<AssemblyName>libArgument</AssemblyName>
10+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
11+
</PropertyGroup>
12+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
13+
<DebugSymbols>true</DebugSymbols>
14+
<DebugType>full</DebugType>
15+
<Optimize>false</Optimize>
16+
<OutputPath>bin\Debug</OutputPath>
17+
<DefineConstants>DEBUG;</DefineConstants>
18+
<ErrorReport>prompt</ErrorReport>
19+
<WarningLevel>4</WarningLevel>
20+
<ConsolePause>false</ConsolePause>
21+
</PropertyGroup>
22+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
23+
<DebugType>full</DebugType>
24+
<Optimize>true</Optimize>
25+
<OutputPath>bin\Release</OutputPath>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
<ConsolePause>false</ConsolePause>
29+
</PropertyGroup>
30+
<ItemGroup>
31+
<Reference Include="System" />
32+
<Reference Include="System.Core" />
33+
</ItemGroup>
34+
<ItemGroup>
35+
<Compile Include="Properties\AssemblyInfo.cs" />
36+
<Compile Include="ArgumentParser.cs" />
37+
<Compile Include="Attributes\Switch.cs" />
38+
<Compile Include="Attributes\Argument.cs" />
39+
<Compile Include="Attributes\CastAs.cs" />
40+
<Compile Include="Attributes\CastingType.cs" />
41+
</ItemGroup>
42+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
43+
<ItemGroup>
44+
<Folder Include="Attributes\" />
45+
</ItemGroup>
46+
</Project>

0 commit comments

Comments
 (0)