Skip to content

Commit 26a5933

Browse files
committed
Introduced ValueOptionAttribute (see issue #23).
1 parent 215c445 commit 26a5933

23 files changed

+455
-287
lines changed

CommandLine.sln.DotSettings.user

+3-1
Large diffs are not rendered by default.

README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Command Line Parser Library 1.9.4.139 beta for CLR.
1+
Command Line Parser Library 1.9.4.201 beta for CLR.
22
===
33
The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks defining switches, options and verb commands. It allows you to display an help screen with an high degree of customization and a simple way to report syntax errors to the end user. Everything that is boring and repetitive to be programmed stands up on library shoulders, letting developers concentrate on core logic.
44
__The search for the command line parser for your application is over, with this library you got a solid parsing API constantly updated since 2005.__
@@ -11,6 +11,7 @@ Compatibility:
1111

1212
News:
1313
---
14+
- Introduced ``ValueOptionAttribute`` enhancement of issue #23.
1415
- ``CommandLineParser`` refactored (also using new ``ParserContext`` type).
1516
- ``ReflectionUtil`` now caches data using ``ReflectionCache``.
1617
- Added ``strings`` task to Rakefile used to regenerate ``SR.strings.cs``.
@@ -48,14 +49,7 @@ To start:
4849

4950
Public API:
5051
---
51-
- Version 1.9.4.91: Breaking, short name of an option must be defined as character (``System.Char``). Non breaking, added support for verbs.
52-
- Version 1.9.4.99: Breaking, removed dependency from ``CommandLineOptionsBase``, introduced [ParseStateAttribute](https://github.com/gsscoder/commandline/blob/master/src/sample/Program.cs).
53-
- Version 1.9.4.107: Non breaking, implemented [strict parsing](https://github.com/gsscoder/commandline/blob/master/src/tests/Parser/StrictFixture.cs) (see issue #32).
54-
- Version 1.9.4.109: Non breaking, pull request #44.
55-
- Version 1.9.4.111: Non breaking, ``CommandLineParserSettings``, ``CommandLineParser`` implements ``IDisposable``.
56-
- Version 1.9.4.113: Non breaking, added ``CommandLineParser::WasVerbOptionInvoked`` helper method.
57-
- Version 1.9.4.123: Breaking, ``HandleParsingErrorsDelegate`` renamed to ``ParsingErrorsHandler``, ``MultiLineTextAttribute`` renamed to ``MultilineTextAttribute``. Non breaking, refactoring (see ChangeLog).
58-
- Version 1.9.4.127: 'Partially' non breaking, ``OptionAttribute`` is now sealed. ``OptionArrayAttribute`` and ``OptionListAttribute`` derives from ``BaseOptionAttribute`` (update your custom types too).
52+
Latest changes are recorded from Version 1.9.4.91, please refer to [this document](https://github.com/gsscoder/commandline/blob/master/doc/PublicAPI.md).
5953

6054
Verb Commands:
6155
---

Rakefile.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PRODUCT = "Command Line Parser Library"
22
DESCRIPTION = "Command Line Parser Library allows CLR applications to define a syntax for parsing command line arguments."
33
INF_VERSION = "1.9"
4-
VERSION = INF_VERSION + ".4.139"
4+
VERSION = INF_VERSION + ".4.201"
55
COPYRIGHT = "Copyright (c) 2005 - 2013 Giacomo Stelluti Scala"
66
LICENSE_URL = "https://raw.github.com/gsscoder/commandline/master/doc/LICENSE"
77
PROJECT_URL = "https://github.com/gsscoder/commandline"

doc/ChangeLog

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2013-01-25 Giacomo Stelluti Scala <[email protected]>
2+
3+
* Implemented issue #23 (https://github.com/gsscoder/commandline/issues/33) that
4+
introduces ValueOptionAttribute.
5+
* Internal Target type renamed to ValueMapper.
6+
* OptionInfo property writing primitives refactored in PropertyWriter type.
7+
* Added ValueOptionAttributeParsingFixture and new mock types.
8+
* Version incremented to 1.9.4.201 beta.
9+
110
2013-01-24 Giacomo Stelluti Scala <[email protected]>
211

312
* Sources in Utils dir moved to CommandLine.Utils namespace.

doc/PublicAPI.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Public API Changes:
2+
---
3+
- Version 1.9.4.91: Breaking, short name of an option must be defined as character (``System.Char``). Non breaking, added support for verbs.
4+
- Version 1.9.4.99: Breaking, removed dependency from ``CommandLineOptionsBase``, introduced [ParseStateAttribute](https://github.com/gsscoder/commandline/blob/master/src/sample/Program.cs).
5+
- Version 1.9.4.107: Non breaking, implemented [strict parsing](https://github.com/gsscoder/commandline/blob/master/src/tests/Parser/StrictFixture.cs) (see issue #32).
6+
- Version 1.9.4.109: Non breaking, pull request #44.
7+
- Version 1.9.4.111: Non breaking, ``CommandLineParserSettings``, ``CommandLineParser`` implements ``IDisposable``.
8+
- Version 1.9.4.113: Non breaking, added ``CommandLineParser::WasVerbOptionInvoked`` helper method.
9+
- Version 1.9.4.123: Breaking, ``HandleParsingErrorsDelegate`` renamed to ``ParsingErrorsHandler``, ``MultiLineTextAttribute`` renamed to ``MultilineTextAttribute``. Non breaking, refactoring (see ChangeLog).
10+
- Version 1.9.4.127: 'Partially' non breaking, ``OptionAttribute`` is now sealed. ``OptionArrayAttribute`` and ``OptionListAttribute`` derives from ``BaseOptionAttribute`` (update your custom types too).
11+
- Version 1.9.4.201: Non breaking, introduced ``ValueOptionAttribute`` enhancement of issue #23.

doc/README

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Command Line Parser Library
33
Project Author/Coordinator: Giacomo Stelluti Scala
44
Main Contributor(s): Steven Evans, Kevin Moore, Dan Nemec (nemec), Alexander Fast (mizipzor)
55
--------------------------------------------------------------------------------------------
6-
Version 1.9.4.139 beta (*1)
7-
Latest Update: 24-01-2013
6+
Version 1.9.4.201 beta (*1)
7+
Latest Update: 25-01-2013
88

99
Git home:
1010
https://github.com/gsscoder/commandline

doc/config/NAnt.exe.config.part

-167
This file was deleted.

src/CommonAssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System.Runtime.InteropServices;
55
[assembly: AssemblyProduct("Command Line Parser Library")]
66
[assembly: AssemblyCopyright("Copyright (c) 2005 - 2013 Giacomo Stelluti Scala")]
7-
[assembly: AssemblyVersion("1.9.4.139")]
8-
[assembly: AssemblyFileVersion("1.9.4.139")]
7+
[assembly: AssemblyVersion("1.9.4.201")]
8+
[assembly: AssemblyFileVersion("1.9.4.201")]
99

1010
[assembly: AssemblyInformationalVersion("1.9")]
1111
[assembly: NeutralResourcesLanguage("en-US")]

src/demo/CommandLine.Demo.csproj

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@
3434
<Reference Include="System" />
3535
</ItemGroup>
3636
<ItemGroup>
37+
<Compile Include="..\libcmdline\Attributes\ValueOptionAttribute.cs">
38+
<Link>Library\Attributes\ValueOptionAttribute.cs</Link>
39+
</Compile>
3740
<Compile Include="..\libcmdline\Internal\PresentParserState.cs">
3841
<Link>Library\Internal\PresentParserState.cs</Link>
3942
</Compile>
40-
<Compile Include="..\libcmdline\Internal\Target.cs">
41-
<Link>Library\Internal\Target.cs</Link>
43+
<Compile Include="..\libcmdline\Internal\PropertyWriter.cs">
44+
<Link>Library\Internal\PropertyWriter.cs</Link>
45+
</Compile>
46+
<Compile Include="..\libcmdline\Internal\ValueMapper.cs">
47+
<Link>Library\Internal\ValueMapper.cs</Link>
4248
</Compile>
4349
<Compile Include="..\libcmdline\ParserContext.cs">
4450
<Link>Library\ParserContext.cs</Link>

src/libcmdline/Attributes/ValueListAttribute.cs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace CommandLine
4242
/// Must be applied to a field compatible with an <see cref="System.Collections.Generic.IList&lt;T&gt;"/> interface
4343
/// of <see cref="System.String"/> instances.
4444
/// </summary>
45+
/// <remarks>To map individual values use instead <see cref="CommandLine.ValueOptionAttribute"/>.</remarks>
4546
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
4647
public sealed class ValueListAttribute : Attribute
4748
{
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#region License
22
//
3-
// Command Line Library: TargetWrapper.cs
3+
// Command Line Library: ValueOptionAttribute.cs
44
//
55
// Author:
66
// Giacomo Stelluti Scala ([email protected])
@@ -27,39 +27,21 @@
2727
//
2828
#endregion
2929
#region Using Directives
30+
using System;
3031
using System.Collections.Generic;
32+
using System.Text;
3133
#endregion
3234

33-
namespace CommandLine.Internal
35+
namespace CommandLine
3436
{
35-
sealed class Target
37+
/// <summary>
38+
/// Maps a single unnamed option to the target property. Values will be mapped in order of declaration.
39+
/// This attribute takes precedence over <see cref="CommandLine.ValueListAttribute"/> with which
40+
/// can coexist.
41+
/// </summary>
42+
/// <remarks>It can handle only scalar values. Do not apply to arrays or lists.</remarks>
43+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
44+
public sealed class ValueOptionAttribute : Attribute
3645
{
37-
private Target() {}
38-
39-
public Target(object target)
40-
{
41-
_target = target;
42-
_valueListAttribute = ValueListAttribute.GetAttribute(_target);
43-
if (IsValueListDefined)
44-
{
45-
_valueList = ValueListAttribute.GetReference(_target);
46-
}
47-
}
48-
49-
public bool IsValueListDefined { get { return _valueListAttribute != null; } }
50-
51-
public bool AddValueItemIfAllowed(string item)
52-
{
53-
if (_valueListAttribute.MaximumElements == 0 || _valueList.Count == _valueListAttribute.MaximumElements)
54-
{
55-
return false;
56-
}
57-
_valueList.Add(item);
58-
return true;
59-
}
60-
61-
private readonly object _target;
62-
private readonly IList<string> _valueList;
63-
private readonly ValueListAttribute _valueListAttribute;
6446
}
6547
}

src/libcmdline/CommandLine.csproj

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
<Compile Include="Attributes\OptionListAttribute.cs" />
8080
<Compile Include="Attributes\ParserStateAttribute.cs" />
8181
<Compile Include="Attributes\ValueListAttribute.cs" />
82+
<Compile Include="Attributes\ValueOptionAttribute.cs" />
83+
<Compile Include="Internal\PropertyWriter.cs" />
8284
<Compile Include="ParserContext.cs" />
8385
<Compile Include="Utils\Assumes.cs" />
8486
<Compile Include="Internal\SR.strings.cs">
@@ -89,7 +91,7 @@
8991
<Compile Include="Utils\StringExtensions.cs" />
9092
<Compile Include="Utils\ReflectionUtil.cs" />
9193
<Compile Include="Utils\Pair.cs" />
92-
<Compile Include="Internal\Target.cs" />
94+
<Compile Include="Internal\ValueMapper.cs" />
9395
<Compile Include="Internal\LongOptionParser.cs" />
9496
<Compile Include="Internal\OptionGroupParser.cs" />
9597
<Compile Include="Internal\OptionInfo.cs" />

src/libcmdline/CommandLineParser.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ private bool DoParseArgumentsDispatcher(ParserContext context)
154154

155155
private bool DoParseArgumentsCore(ParserContext context)
156156
{
157-
bool hadError = false;
157+
var hadError = false;
158158
var optionMap = OptionMap.Create(context.Target, _settings);
159159
optionMap.SetDefaults();
160-
var target = new Target(context.Target);
160+
var valueMapper = context.Target.CreateValueMapper();
161161

162-
IArgumentEnumerator arguments = new StringArrayEnumerator(context.Arguments);
162+
var arguments = new StringArrayEnumerator(context.Arguments);
163163
while (arguments.MoveNext())
164164
{
165165
var argument = arguments.Current;
@@ -183,9 +183,9 @@ private bool DoParseArgumentsCore(ParserContext context)
183183
arguments.MoveNext();
184184
}
185185
}
186-
else if (target.IsValueListDefined)
186+
else if (valueMapper.CanReceiveValues)
187187
{
188-
if (!target.AddValueItemIfAllowed(argument))
188+
if (!valueMapper.MapValueItem(argument))
189189
{
190190
hadError = true;
191191
}

0 commit comments

Comments
 (0)