Permalink
Browse files

list 형태의 Property 파싱 가능

Parser 및 ParserAttribute 새로 추가
전체적으로 System.Drawing.Design.UITypeEditor형태와 비슷한 구조로 설계
  • Loading branch information...
1 parent 8dfae21 commit 5fc2354e5b22176feab7fd6e800ad6fd727fe314 @s2quake s2quake committed Oct 5, 2011
Showing with 714 additions and 123 deletions.
  1. +1 −1 LICENSE
  2. +4 −2 README.md
  3. BIN bin/CommandLineParser.dll
  4. BIN bin/SampleApplication.exe
  5. +40 −0 lib/CommandLineParser/BooleanParser.cs
  6. +3 −3 lib/CommandLineParser/CommandLineParser.UsagePrinter.cs
  7. +10 −2 lib/CommandLineParser/CommandLineParser.cs
  8. +5 −0 lib/CommandLineParser/CommandLineParser.csproj
  9. +1 −1 lib/CommandLineParser/IOptions.cs
  10. +6 −4 lib/CommandLineParser/InternalUsageProvider.cs
  11. +96 −0 lib/CommandLineParser/ListParser.cs
  12. +1 −1 lib/CommandLineParser/MissingSwitchException.cs
  13. +55 −0 lib/CommandLineParser/Parser.cs
  14. +49 −0 lib/CommandLineParser/ParserAttribute.cs
  15. +1 −1 lib/CommandLineParser/ParsingOptions.cs
  16. +3 −2 lib/CommandLineParser/Properties/AssemblyInfo.cs
  17. +1 −11 lib/CommandLineParser/Resource.Designer.cs
  18. +9 −1 lib/CommandLineParser/SwitchAttribute.cs
  19. +1 −1 lib/CommandLineParser/SwitchAttributeCollection.cs
  20. +37 −24 lib/CommandLineParser/SwitchDescriptor.cs
  21. +2 −58 lib/CommandLineParser/SwitchDescriptorCollection.cs
  22. +129 −0 lib/CommandLineParser/SwitchDescriptorContext.cs
  23. +1 −1 lib/CommandLineParser/SwitchException.cs
  24. +1 −1 lib/CommandLineParser/UsageProvider.cs
  25. +133 −0 lib/CommandLineParserTest/CommandLineBuilder.cs
  26. +1 −0 lib/CommandLineParserTest/CommandLineParserTest.csproj
  27. +1 −1 lib/CommandLineParserTest/Properties/AssemblyInfo.cs
  28. +51 −3 lib/CommandLineParserTest/Test.cs
  29. +44 −2 lib/CommandLineParserTest/TestOptions.cs
  30. +27 −2 lib/SampleApplication/Program.cs
  31. +1 −1 lib/SampleApplication/Properties/AssemblyInfo.cs
View
@@ -1,4 +1,4 @@
-Ntreev CommandLineParser for .Net
+Ntreev CommandLineParser for .Net 1.0.4295.27782
https://github.com/NtreevSoft/CommandLineParser
Released under the MIT License.
View
@@ -7,6 +7,7 @@ SourceCode:
using System;
using Ntreev.Library;
+ using System.Collections.Generic;
namespace Example
{
@@ -31,15 +32,16 @@ SourceCode:
[Switch("i")]
public int Index { get; set; }
public string Text { get; set; }
+ public List<int> Numbers { get; set; }
}
}
}
You can call like this:
- C:\> Example.exe /Toggle /Index 3 /Text "this is text"
+ C:\>Example.exe /Toggle /Index 3 /Text "this is text" /Numbers "1,2,3,4,5"
or
- C:\> Example.exe /i 3
+ C:\>Example.exe /i 3
License
View
Binary file not shown.
View
Binary file not shown.
@@ -0,0 +1,40 @@
+#region License
+//Ntreev CommandLineParser for .Net 1.0.4295.27782
+//https://github.com/NtreevSoft/CommandLineParser
+
+//Released under the MIT License.
+
+//Copyright (c) 2010 Ntreev Soft co., Ltd.
+
+//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+//documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
+//rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
+//persons to whom the Software is furnished to do so, subject to the following conditions:
+
+//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
+//Software.
+
+//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+//COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Ntreev.Library
+{
+ class BooleanParser : Parser
+ {
+ public override object Parse(SwitchDescriptor switchDescriptor, string arg, object value)
+ {
+ if (switchDescriptor.ArgType == typeof(bool) && switchDescriptor.ArgSeperator == null)
+ {
+ return true;
+ }
+ return base.Parse(switchDescriptor, arg, value);
+ }
+ }
+}
@@ -1,5 +1,5 @@
#region License
-//Ntreev CommandLineParser for .Net
+//Ntreev CommandLineParser for .Net 1.0.4295.27782
//https://github.com/NtreevSoft/CommandLineParser
//Released under the MIT License.
@@ -111,7 +111,7 @@ internal void PrintSwitchUsage(string switchName)
throw new Exception();
SwitchAttributeCollection switchAttributes = this.commandLineParser.SwitchAttributes;
- SwitchDescriptorCollection switchDescriptorCollection = SwitchDescriptorCollection.GetSwitches(instance, switchAttributes);
+ SwitchDescriptorCollection switchDescriptorCollection = SwitchDescriptorContext.GetSwitches(instance, switchAttributes);
OnPrintSwitchUsage(switchName, switchDescriptorCollection[switchName], this.commandLineParser.TextWriter);
}
@@ -122,7 +122,7 @@ internal void PrintUsage()
throw new Exception();
SwitchAttributeCollection switchAttributes = this.commandLineParser.SwitchAttributes;
- SwitchDescriptorCollection switchDescriptorCollection = SwitchDescriptorCollection.GetSwitches(instance, switchAttributes);
+ SwitchDescriptorCollection switchDescriptorCollection = SwitchDescriptorContext.GetSwitches(instance, switchAttributes);
OnPrintUsage(switchDescriptorCollection.ToArray(), this.commandLineParser.TextWriter);
}
@@ -1,5 +1,5 @@
#region License
-//Ntreev CommandLineParser for .Net
+//Ntreev CommandLineParser for .Net 1.0.4295.27782
//https://github.com/NtreevSoft/CommandLineParser
//Released under the MIT License.
@@ -142,7 +142,7 @@ public void Parse(string commandLine, object options, ParsingOptions parsingOpti
string[] switchLines, unusedArgs;
SplitSwitches(this.arguments, out switchLines);
- SwitchDescriptorCollection switchCollection = SwitchDescriptorCollection.GetSwitches(instance, switches);
+ SwitchDescriptorCollection switchCollection = SwitchDescriptorContext.GetSwitches(instance, switches);
switchCollection.AssertValidation();
switchCollection.AssertMutuallyExclusive();
@@ -261,6 +261,14 @@ public string[] UnusedArguments
get { return this.unusedArguments.ToArray(); }
}
+ /// <summary>
+ /// 사용방법을 출력하는 방법을 나타내는 인스턴를 가져옵니다.
+ /// </summary>
+ public UsagePrinter Usage
+ {
+ get { return this.usagePrinter; }
+ }
+
#endregion
#region private classes
@@ -70,15 +70,20 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="BooleanParser.cs" />
+ <Compile Include="ListParser.cs" />
+ <Compile Include="Parser.cs" />
<Compile Include="CommandLineParser.cs" />
<Compile Include="InternalUsageProvider.cs" />
<Compile Include="IOptions.cs" />
+ <Compile Include="ParserAttribute.cs" />
<Compile Include="ParsingOptions.cs" />
<Compile Include="Resource.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resource.resx</DependentUpon>
</Compile>
+ <Compile Include="SwitchDescriptorContext.cs" />
<Compile Include="UsageProvider.cs" />
<Compile Include="MissingSwitchException.cs" />
<Compile Include="CommandLineParser.UsagePrinter.cs" />
@@ -1,5 +1,5 @@
#region License
-//Ntreev CommandLineParser for .Net
+//Ntreev CommandLineParser for .Net 1.0.4295.27782
//https://github.com/NtreevSoft/CommandLineParser
//Released under the MIT License.
@@ -1,5 +1,5 @@
#region License
-//Ntreev CommandLineParser for .Net
+//Ntreev CommandLineParser for .Net 1.0.4295.27782
//https://github.com/NtreevSoft/CommandLineParser
//Released under the MIT License.
@@ -51,16 +51,18 @@ public override string Usage
Type argType = this.SwitchDescriptor.ArgType;
if (argType != typeof(bool) || argSeperator != null)
{
+ string argTypeName = this.SwitchDescriptor.ArgTypeSummary;
+
if (argSeperator == null)
{
- help += string.Format(" <{0}>", argType.Name);
+ help += string.Format(" <{0}>", argTypeName);
}
else
{
if (argSeperator != char.MinValue)
- help += string.Format("<{0}{1}>", argSeperator, argType.Name);
+ help += string.Format("<{0}{1}>", argSeperator, argTypeName);
else
- help += string.Format("<{0}>", argType.Name);
+ help += string.Format("<{0}>", argTypeName);
}
}
return help;
@@ -0,0 +1,96 @@
+#region License
+//Ntreev CommandLineParser for .Net 1.0.4295.27782
+//https://github.com/NtreevSoft/CommandLineParser
+
+//Released under the MIT License.
+
+//Copyright (c) 2010 Ntreev Soft co., Ltd.
+
+//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+//documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
+//rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
+//persons to whom the Software is furnished to do so, subject to the following conditions:
+
+//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
+//Software.
+
+//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+//COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Reflection;
+using System.ComponentModel;
+
+namespace Ntreev.Library
+{
+ public class ListParser : Parser
+ {
+ public override object Parse(SwitchDescriptor switchDescriptor, string arg, object value)
+ {
+ System.Collections.IList list;
+
+ if (value == null)
+ {
+ list = TypeDescriptor.CreateInstance(null, switchDescriptor.ArgType, null, null) as System.Collections.IList;
+ }
+ else
+ {
+ list = value as System.Collections.IList;
+ }
+
+ Type type = GetElementType(switchDescriptor.ArgType);
+ if (type == null)
+ throw new NotSupportedException();
+ string[] args = SplitArgument(arg);
+
+ try
+ {
+ foreach (string item in args)
+ {
+ string s = item.Trim();
+ if(s.Length == 0)
+ continue;
+ object element = OnItemParse(s, type);
+ list.Add(element);
+ }
+ }
+ catch (Exception e)
+ {
+ throw new SwitchException("잘못된 인수 형식입니다.", switchDescriptor.Name, e);
+ }
+
+ return list;
+ }
+
+ virtual protected string[] SplitArgument(string arg)
+ {
+ return arg.Split(new char[] { ',', });
+ }
+
+ virtual protected object OnItemParse(string arg, Type itemType)
+ {
+ TypeConverter typeConverter = TypeDescriptor.GetConverter(itemType);
+ return typeConverter.ConvertFrom(arg);
+ }
+
+ static public Type GetElementType(Type propertyType)
+ {
+ PropertyInfo[] properties = TypeDescriptor.GetReflectionType(propertyType).GetProperties(BindingFlags.Public | BindingFlags.Instance);
+
+ foreach (PropertyInfo item in properties)
+ {
+ if (item.Name.Equals("Item") || item.Name.Equals("Items"))
+ {
+ return item.PropertyType;
+ }
+ }
+
+ return null;
+ }
+ }
+}
@@ -1,5 +1,5 @@
#region License
-//Ntreev CommandLineParser for .Net
+//Ntreev CommandLineParser for .Net 1.0.4295.27782
//https://github.com/NtreevSoft/CommandLineParser
//Released under the MIT License.
@@ -0,0 +1,55 @@
+#region License
+//Ntreev CommandLineParser for .Net 1.0.4295.27782
+//https://github.com/NtreevSoft/CommandLineParser
+
+//Released under the MIT License.
+
+//Copyright (c) 2010 Ntreev Soft co., Ltd.
+
+//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+//documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
+//rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
+//persons to whom the Software is furnished to do so, subject to the following conditions:
+
+//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
+//Software.
+
+//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+//COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.ComponentModel;
+
+namespace Ntreev.Library
+{
+ public class Parser
+ {
+ public Parser()
+ {
+
+ }
+
+ virtual public object Parse(SwitchDescriptor switchDescriptor, string arg, object value)
+ {
+ TypeConverter typeConverter = switchDescriptor.Converter;
+
+ if (typeConverter.CanConvertFrom(typeof(string)) == false)
+ throw new NotSupportedException("타입컨버터에서 문자열에 의한 변환이 지원되질 않습니다.");
+
+ try
+ {
+ value = typeConverter.ConvertFrom(arg);
+ }
+ catch (Exception e)
+ {
+ throw new SwitchException("잘못된 인수 형식입니다.", switchDescriptor.Name, e);
+ }
+ return value;
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 5fc2354

Please sign in to comment.