Browse Source

initial commit

master
Scott Barker 7 years ago
commit
ff7f95b4b0
8 changed files with 262 additions and 0 deletions
  1. 39
    0
      .gitignore
  2. 65
    0
      OptionBase.cs
  3. 58
    0
      OptionParser.csproj
  4. 13
    0
      OptionParserAttribute.cs
  5. 11
    0
      OptionParserExtensionMethods.cs
  6. 32
    0
      OptionRequiredException.cs
  7. 8
    0
      OptionType.cs
  8. 36
    0
      Properties/AssemblyInfo.cs

+ 39
- 0
.gitignore View File

@@ -0,0 +1,39 @@
*.com
*.class
*.dll
*.exe
*.pdb
*.dll.config
*.cache
*.suo

# Include dlls if they’re in the NuGet packages directory
!/packages/*/lib/*.dll

UpgradeLog.XML
_UpgradeReport_Files/

[Bb]in/
[Oo]bj/
[Dd]ebug/
[Rr]elease/
TestResults/

*.Publish.xml

/_ReSharper.*
*.ReSharper.*

*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

.DS_Store?
ehthumbs.db
Icon?
Thumbs.db

+ 65
- 0
OptionBase.cs View File

@@ -0,0 +1,65 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
namespace OptionParser {
public abstract class OptionBase {
public OptionBase(string[] arguments)
{
Parse(arguments);
}
public void Parse(string[] arguments) {
List<string> argList = arguments.ToList<string>();
FieldInfo[] fields = GetType().GetFields();
foreach (var f in fields) {
if (f.IsDefined(typeof(OptionParserAttribute), false)) {
var attributes = (OptionParserAttribute[])f.GetCustomAttributes(typeof(OptionParserAttribute), false);
foreach (var attribute in attributes) {
if (attribute.Default != null) {
f.SetValue(this, attribute.Default);
}
bool optionSet = false;
for (int i = 0; i < argList.Count; i++) {
if (argList[i].IsOptionFor(attribute.Name, attribute.Switch)) {
int range = 2;
object val = null;
if (argList.Count > 1)
{
val = argList[i + 1];
}
if (attribute.OptionType == typeof(bool))
{
range = 1;
val = !Convert.ToBoolean(f.GetValue(this));
}
f.SetValue(this, val);
argList.RemoveRange(i, range);
optionSet = true;
break;
}
}
var desc =
(f.GetCustomAttributes(typeof (DescriptionAttribute), false).First() as DescriptionAttribute).Description;
if (attribute.Required && !optionSet)
{
throw new OptionRequiredException(message: string.Format("Option '{0}' is required.", attribute.Name),
option: attribute.Name,
description: desc
);
}
}
}
}
}
}
}

+ 58
- 0
OptionParser.csproj View File

@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{C4806DBC-ECE6-4DA5-8E75-57BB0500ABDE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OptionParser</RootNamespace>
<AssemblyName>OptionParser</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="OptionRequiredException.cs" />
<Compile Include="OptionParserAttribute.cs" />
<Compile Include="OptionBase.cs" />
<Compile Include="OptionParserExtensionMethods.cs" />
<Compile Include="OptionType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

+ 13
- 0
OptionParserAttribute.cs View File

@@ -0,0 +1,13 @@
using System;
namespace OptionParser
{
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class OptionParserAttribute : Attribute {
public string Name;
public string Switch;
public bool Required;
public object Default;
public Type OptionType;
}
}

+ 11
- 0
OptionParserExtensionMethods.cs View File

@@ -0,0 +1,11 @@
internal static class OptionParserExtensionMethods
{
internal static bool IsOptionFor(this string str, string name, string @switch)
{
if (str != null)
{
return (str.Equals("--" + name) || str.Equals("-" + @switch) || str.Equals("/" + name) || str.Equals("/" + @switch));
}
return false;
}
}

+ 32
- 0
OptionRequiredException.cs View File

@@ -0,0 +1,32 @@
using System;
using System.Runtime.Serialization;
namespace OptionParser
{
[Serializable]
public class OptionRequiredException : ArgumentException
{
public string Option { get; set; }
public string Description { get; set; }
public OptionRequiredException()
{
}
public OptionRequiredException(string message, string option = null, string description = null) : base(message)
{
Option = option;
Description = description;
}
public OptionRequiredException(string message, Exception inner) : base(message, inner)
{
}
protected OptionRequiredException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
}
}

+ 8
- 0
OptionType.cs View File

@@ -0,0 +1,8 @@
namespace OptionParser
{
public enum OptionType {
String,
Bool,
Int
}
}

+ 36
- 0
Properties/AssemblyInfo.cs View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("OptionParser")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OptionParser")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7b9dbaa6-cb51-4456-871f-2832a6137f35")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Loading…
Cancel
Save