Skip to content

Commit d855b9a

Browse files
committed
Enabling implicit name also in OptionListAttribute.
1 parent f78f1d4 commit d855b9a

File tree

7 files changed

+200
-140
lines changed

7 files changed

+200
-140
lines changed

CommandLine.sln.DotSettings.user

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

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Resources for newcomers:
107107
Latest Changes:
108108
---
109109
- Promoted to stable.
110+
- Implicit name is now available on ``OptionAttribute`` and ``OptionListAttribute``.
110111

111112
Contacts:
112113
---

doc/ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2013-02-27 Giacomo Stelluti Scala <[email protected]>
2+
3+
* Implicit name is now available on OptionAttribute and OptionListAttribute
4+
(added tests).
5+
16
2013-02-26 Giacomo Stelluti Scala <[email protected]>
27

38
* Reduced mutability of ParserSettings, instance can be used once.

src/libcmdline/Attributes/OptionListAttribute.cs

+17-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,21 @@ namespace CommandLine
3434
/// of <see cref="System.String"/> instances.
3535
/// </summary>
3636
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
37-
public sealed class OptionListAttribute : BaseOptionAttribute
38-
{
37+
public sealed class OptionListAttribute : BaseOptionAttribute
38+
{
39+
private const char DefaultSeparator = ':';
40+
41+
/// <summary>
42+
/// Initializes a new instance of the <see cref="CommandLine.OptionListAttribute"/> class.
43+
/// The default long name will be inferred from target property.
44+
/// </summary>
45+
public OptionListAttribute()
46+
{
47+
AutoLongName = true;
48+
49+
Separator = DefaultSeparator;
50+
}
51+
3952
/// <summary>
4053
/// Initializes a new instance of the <see cref="CommandLine.OptionListAttribute"/> class.
4154
/// </summary>
@@ -61,8 +74,8 @@ public OptionListAttribute(string longName)
6174
/// <param name="longName">The long name of the option or null if not used.</param>
6275
public OptionListAttribute(char shortName, string longName)
6376
: base(shortName, longName)
64-
{
65-
Separator = ':';
77+
{
78+
Separator = DefaultSeparator;
6679
}
6780

6881
/// <summary>

src/tests/CommandLine.Tests.csproj

+135-134
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,136 @@
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-
<ProductVersion>10.0.0</ProductVersion>
7-
<SchemaVersion>2.0</SchemaVersion>
8-
<ProjectGuid>{86E1AC34-ED2D-4E42-8B95-65208FEA36C2}</ProjectGuid>
9-
<OutputType>Library</OutputType>
10-
<RootNamespace>CommandLine.Tests</RootNamespace>
11-
<AssemblyName>CommandLine.Tests</AssemblyName>
12-
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
13-
<RestorePackages>true</RestorePackages>
14-
</PropertyGroup>
15-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16-
<DebugSymbols>true</DebugSymbols>
17-
<DebugType>full</DebugType>
18-
<Optimize>false</Optimize>
19-
<OutputPath>bin\Debug</OutputPath>
20-
<DefineConstants>TRACE;DEBUG</DefineConstants>
21-
<ErrorReport>prompt</ErrorReport>
22-
<WarningLevel>4</WarningLevel>
23-
<ConsolePause>false</ConsolePause>
24-
</PropertyGroup>
25-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26-
<DebugType>none</DebugType>
27-
<Optimize>false</Optimize>
28-
<OutputPath>bin\Release</OutputPath>
29-
<ErrorReport>prompt</ErrorReport>
30-
<WarningLevel>4</WarningLevel>
31-
<ConsolePause>false</ConsolePause>
32-
<DefineConstants>UNIT_TESTS</DefineConstants>
33-
</PropertyGroup>
34-
<PropertyGroup>
35-
<SignAssembly>true</SignAssembly>
36-
</PropertyGroup>
37-
<PropertyGroup>
38-
<AssemblyOriginatorKeyFile>..\..\CommandLine.snk</AssemblyOriginatorKeyFile>
39-
</PropertyGroup>
40-
<ItemGroup>
41-
<Reference Include="FluentAssertions">
42-
<HintPath>..\..\packages\FluentAssertions.2.0.0.1\lib\net40\FluentAssertions.dll</HintPath>
43-
</Reference>
44-
<Reference Include="System" />
45-
<Reference Include="System.Xml" />
46-
<Reference Include="System.Xml.Linq" />
47-
<Reference Include="xunit">
48-
<HintPath>..\..\packages\xunit.1.9.1\lib\net20\xunit.dll</HintPath>
49-
</Reference>
50-
</ItemGroup>
51-
<ItemGroup>
52-
<Compile Include="..\SharedAssemblyInfo.cs">
53-
<Link>Properties\SharedAssemblyInfo.cs</Link>
54-
</Compile>
55-
<Compile Include="Fakes\OptionsWithImplicitLongName.cs" />
56-
<Compile Include="Fakes\OptionsWithTwoArrays.cs" />
57-
<Compile Include="Unit\Attributes\AttributesFixture.cs" />
58-
<Compile Include="Unit\Attributes\HelpOptionAttributeFixture.cs" />
59-
<Compile Include="Unit\Attributes\OptionArrayAttributeFixture.cs" />
60-
<Compile Include="Unit\Attributes\OptionAttributeFixture.cs" />
61-
<Compile Include="Unit\Attributes\ValueListAttributeFixture.cs" />
62-
<Compile Include="Unit\Attributes\ValueOptionAttributeFixture.cs" />
63-
<Compile Include="Unit\Infrastructure\ArgumentParserFixture.cs" />
64-
<Compile Include="Unit\Infrastructure\EnumeratorsFixture.cs" />
65-
<Compile Include="Unit\Infrastructure\OptionMapFixture.cs" />
66-
<Compile Include="Fakes\CommandLineOptionsBase.cs" />
67-
<Compile Include="Fakes\OptionsWithValueOptionExplicitIndex.cs" />
68-
<Compile Include="Fakes\OptionsWithValueOptionImplicitIndex.cs" />
69-
<Compile Include="Fakes\SimpleOptionsWithValueOptionAndValueList.cs" />
70-
<Compile Include="Fakes\SimpleOptionsWithValueOption.cs" />
71-
<Compile Include="Fakes\SimpleOptionsForStrict.cs" />
72-
<Compile Include="Fakes\OptionsWithVerbsHelp.cs" />
73-
<Compile Include="Fakes\OptionsWithVerbs.cs" />
74-
<Compile Include="Unit\Parser\ParserFixture.cs" />
75-
<Compile Include="Unit\Parser\ParserSettingsFixture.cs" />
76-
<Compile Include="Unit\Parser\MutuallyExclusiveParsingFixture.cs" />
77-
<Compile Include="Unit\Parser\NullableTypesParsingFixture.cs" />
78-
<Compile Include="Unit\Parser\OptionArrayAttributeParsingFixture.cs" />
79-
<Compile Include="Unit\Parser\ParserStateFixture.cs" />
80-
<Compile Include="Unit\Parser\SingletonFixture.cs" />
81-
<Compile Include="Unit\Parser\StrictFixture.cs" />
82-
<Compile Include="Unit\Parser\UnknownArgumentsFixture.cs" />
83-
<Compile Include="Unit\Parser\ValueListAttributeParsingFixture.cs" />
84-
<Compile Include="Unit\Parser\ValueOptionAttributeParsingFixture.cs" />
85-
<Compile Include="Unit\Parser\VerbsFixture.cs" />
86-
<Compile Include="Properties\AssemblyInfo.cs" />
87-
<Compile Include="Unit\BaseFixture.cs" />
88-
<Compile Include="Unit\ParserBaseFixture.cs" />
89-
<Compile Include="Unit\DebugStringUtil.cs" />
90-
<Compile Include="Fakes\BooleanSetOptions.cs" />
91-
<Compile Include="Fakes\ComplexOptions.cs" />
92-
<Compile Include="Fakes\MixedCaseOptions.cs" />
93-
<Compile Include="Fakes\NullableTypesOptions.cs" />
94-
<Compile Include="Fakes\NumberSetOptions.cs" />
95-
<Compile Include="Fakes\OptionsBase.cs" />
96-
<Compile Include="Fakes\OptionsWithDefaultSet.cs" />
97-
<Compile Include="Fakes\OptionsWithMultipleSet.cs" />
98-
<Compile Include="Fakes\OptionsWithMultipleSetAndOneOption.cs" />
99-
<Compile Include="Fakes\OptionsWithValueListMaximumThree.cs" />
100-
<Compile Include="Fakes\OptionsWithValueListMaximumZero.cs" />
101-
<Compile Include="Fakes\SimpleOptions.cs" />
102-
<Compile Include="Fakes\SimpleOptionsWithArray.cs" />
103-
<Compile Include="Fakes\SimpleOptionsWithArrayAndValueList.cs" />
104-
<Compile Include="Fakes\SimpleOptionsWithBadOptionArray.cs" />
105-
<Compile Include="Fakes\SimpleOptionsWithEnum.cs" />
106-
<Compile Include="Fakes\SimpleOptionsWithHelpOption.cs" />
107-
<Compile Include="Fakes\SimpleOptionsWithOptionList.cs" />
108-
<Compile Include="Fakes\SimpleOptionsWithValueList.cs" />
109-
<Compile Include="Fakes\SimpleOptionsWithDefaults.cs" />
110-
<Compile Include="Fakes\SimpleOptionsWithBadDefaults.cs" />
111-
<Compile Include="Fakes\SimpleOptionsWithDefaultArray.cs" />
112-
<Compile Include="Fakes\OptionsForErrorsScenario.cs" />
113-
<Compile Include="Fakes\SimpleOptionsForAutoBuid.cs" />
114-
<Compile Include="Fakes\OptionsForPlugInScenario.cs" />
115-
<Compile Include="Fakes\OptionsWithUIntArray.cs" />
116-
<Compile Include="Unit\Text\CopyrightInfoFixture.cs" />
117-
<Compile Include="Unit\Text\HeadingInfoFixture.cs" />
118-
<Compile Include="Unit\Text\HelpTextFixture.cs" />
119-
<Compile Include="Unit\Text\MultiLineTextAttributeFixture.cs" />
120-
<Compile Include="Unit\Text\VerbsHelpTextFixture.cs" />
121-
<Compile Include="Unit\Infrastructure\ReflectionHelperFixture.cs" />
122-
</ItemGroup>
123-
<ItemGroup>
124-
<None Include="packages.config" />
125-
</ItemGroup>
126-
<ItemGroup>
127-
<ProjectReference Include="..\libcmdline\CommandLine.csproj">
128-
<Project>{5DEA2811-2FFA-4959-830B-CAD3ACACABEB}</Project>
129-
<Name>CommandLine</Name>
130-
</ProjectReference>
131-
</ItemGroup>
132-
<ItemGroup />
133-
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
134-
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
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+
<ProductVersion>10.0.0</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{86E1AC34-ED2D-4E42-8B95-65208FEA36C2}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<RootNamespace>CommandLine.Tests</RootNamespace>
11+
<AssemblyName>CommandLine.Tests</AssemblyName>
12+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
13+
<RestorePackages>true</RestorePackages>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug</OutputPath>
20+
<DefineConstants>TRACE;DEBUG</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
<ConsolePause>false</ConsolePause>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>none</DebugType>
27+
<Optimize>false</Optimize>
28+
<OutputPath>bin\Release</OutputPath>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
<ConsolePause>false</ConsolePause>
32+
<DefineConstants>UNIT_TESTS</DefineConstants>
33+
</PropertyGroup>
34+
<PropertyGroup>
35+
<SignAssembly>true</SignAssembly>
36+
</PropertyGroup>
37+
<PropertyGroup>
38+
<AssemblyOriginatorKeyFile>..\..\CommandLine.snk</AssemblyOriginatorKeyFile>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<Reference Include="FluentAssertions">
42+
<HintPath>..\..\packages\FluentAssertions.2.0.0.1\lib\net40\FluentAssertions.dll</HintPath>
43+
</Reference>
44+
<Reference Include="System" />
45+
<Reference Include="System.Xml" />
46+
<Reference Include="System.Xml.Linq" />
47+
<Reference Include="xunit">
48+
<HintPath>..\..\packages\xunit.1.9.1\lib\net20\xunit.dll</HintPath>
49+
</Reference>
50+
</ItemGroup>
51+
<ItemGroup>
52+
<Compile Include="..\SharedAssemblyInfo.cs">
53+
<Link>Properties\SharedAssemblyInfo.cs</Link>
54+
</Compile>
55+
<Compile Include="Fakes\OptionsWithImplicitLongName.cs" />
56+
<Compile Include="Fakes\OptionsWithTwoArrays.cs" />
57+
<Compile Include="Unit\Attributes\AttributesFixture.cs" />
58+
<Compile Include="Unit\Attributes\HelpOptionAttributeFixture.cs" />
59+
<Compile Include="Unit\Attributes\OptionArrayAttributeFixture.cs" />
60+
<Compile Include="Unit\Attributes\OptionAttributeFixture.cs" />
61+
<Compile Include="Unit\Attributes\OptionListAttributeFixture.cs" />
62+
<Compile Include="Unit\Attributes\ValueListAttributeFixture.cs" />
63+
<Compile Include="Unit\Attributes\ValueOptionAttributeFixture.cs" />
64+
<Compile Include="Unit\Infrastructure\ArgumentParserFixture.cs" />
65+
<Compile Include="Unit\Infrastructure\EnumeratorsFixture.cs" />
66+
<Compile Include="Unit\Infrastructure\OptionMapFixture.cs" />
67+
<Compile Include="Fakes\CommandLineOptionsBase.cs" />
68+
<Compile Include="Fakes\OptionsWithValueOptionExplicitIndex.cs" />
69+
<Compile Include="Fakes\OptionsWithValueOptionImplicitIndex.cs" />
70+
<Compile Include="Fakes\SimpleOptionsWithValueOptionAndValueList.cs" />
71+
<Compile Include="Fakes\SimpleOptionsWithValueOption.cs" />
72+
<Compile Include="Fakes\SimpleOptionsForStrict.cs" />
73+
<Compile Include="Fakes\OptionsWithVerbsHelp.cs" />
74+
<Compile Include="Fakes\OptionsWithVerbs.cs" />
75+
<Compile Include="Unit\Parser\ParserFixture.cs" />
76+
<Compile Include="Unit\Parser\ParserSettingsFixture.cs" />
77+
<Compile Include="Unit\Parser\MutuallyExclusiveParsingFixture.cs" />
78+
<Compile Include="Unit\Parser\NullableTypesParsingFixture.cs" />
79+
<Compile Include="Unit\Parser\OptionArrayAttributeParsingFixture.cs" />
80+
<Compile Include="Unit\Parser\ParserStateFixture.cs" />
81+
<Compile Include="Unit\Parser\SingletonFixture.cs" />
82+
<Compile Include="Unit\Parser\StrictFixture.cs" />
83+
<Compile Include="Unit\Parser\UnknownArgumentsFixture.cs" />
84+
<Compile Include="Unit\Parser\ValueListAttributeParsingFixture.cs" />
85+
<Compile Include="Unit\Parser\ValueOptionAttributeParsingFixture.cs" />
86+
<Compile Include="Unit\Parser\VerbsFixture.cs" />
87+
<Compile Include="Properties\AssemblyInfo.cs" />
88+
<Compile Include="Unit\BaseFixture.cs" />
89+
<Compile Include="Unit\ParserBaseFixture.cs" />
90+
<Compile Include="Unit\DebugStringUtil.cs" />
91+
<Compile Include="Fakes\BooleanSetOptions.cs" />
92+
<Compile Include="Fakes\ComplexOptions.cs" />
93+
<Compile Include="Fakes\MixedCaseOptions.cs" />
94+
<Compile Include="Fakes\NullableTypesOptions.cs" />
95+
<Compile Include="Fakes\NumberSetOptions.cs" />
96+
<Compile Include="Fakes\OptionsBase.cs" />
97+
<Compile Include="Fakes\OptionsWithDefaultSet.cs" />
98+
<Compile Include="Fakes\OptionsWithMultipleSet.cs" />
99+
<Compile Include="Fakes\OptionsWithMultipleSetAndOneOption.cs" />
100+
<Compile Include="Fakes\OptionsWithValueListMaximumThree.cs" />
101+
<Compile Include="Fakes\OptionsWithValueListMaximumZero.cs" />
102+
<Compile Include="Fakes\SimpleOptions.cs" />
103+
<Compile Include="Fakes\SimpleOptionsWithArray.cs" />
104+
<Compile Include="Fakes\SimpleOptionsWithArrayAndValueList.cs" />
105+
<Compile Include="Fakes\SimpleOptionsWithBadOptionArray.cs" />
106+
<Compile Include="Fakes\SimpleOptionsWithEnum.cs" />
107+
<Compile Include="Fakes\SimpleOptionsWithHelpOption.cs" />
108+
<Compile Include="Fakes\SimpleOptionsWithOptionList.cs" />
109+
<Compile Include="Fakes\SimpleOptionsWithValueList.cs" />
110+
<Compile Include="Fakes\SimpleOptionsWithDefaults.cs" />
111+
<Compile Include="Fakes\SimpleOptionsWithBadDefaults.cs" />
112+
<Compile Include="Fakes\SimpleOptionsWithDefaultArray.cs" />
113+
<Compile Include="Fakes\OptionsForErrorsScenario.cs" />
114+
<Compile Include="Fakes\SimpleOptionsForAutoBuid.cs" />
115+
<Compile Include="Fakes\OptionsForPlugInScenario.cs" />
116+
<Compile Include="Fakes\OptionsWithUIntArray.cs" />
117+
<Compile Include="Unit\Text\CopyrightInfoFixture.cs" />
118+
<Compile Include="Unit\Text\HeadingInfoFixture.cs" />
119+
<Compile Include="Unit\Text\HelpTextFixture.cs" />
120+
<Compile Include="Unit\Text\MultiLineTextAttributeFixture.cs" />
121+
<Compile Include="Unit\Text\VerbsHelpTextFixture.cs" />
122+
<Compile Include="Unit\Infrastructure\ReflectionHelperFixture.cs" />
123+
</ItemGroup>
124+
<ItemGroup>
125+
<None Include="packages.config" />
126+
</ItemGroup>
127+
<ItemGroup>
128+
<ProjectReference Include="..\libcmdline\CommandLine.csproj">
129+
<Project>{5DEA2811-2FFA-4959-830B-CAD3ACACABEB}</Project>
130+
<Name>CommandLine</Name>
131+
</ProjectReference>
132+
</ItemGroup>
133+
<ItemGroup />
134+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
135+
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
135136
</Project>

src/tests/Fakes/OptionsWithImplicitLongName.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace CommandLine.Tests.Fakes
1+
using System.Collections.Generic;
2+
3+
namespace CommandLine.Tests.Fakes
24
{
35
class OptionsWithImplicitLongName
46
{
@@ -13,5 +15,8 @@ class OptionsWithImplicitLongName
1315

1416
[OptionArray]
1517
public int[] Offsets { get; set; }
18+
19+
[OptionList]
20+
public IList<string> Segments { get; set; }
1621
}
1722
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using CommandLine.Tests.Fakes;
6+
7+
using FluentAssertions;
8+
9+
using Xunit;
10+
11+
namespace CommandLine.Tests.Unit.Attributes
12+
{
13+
public class OptionListAttributeFixture
14+
{
15+
16+
[Fact]
17+
public void Should_use_property_name_as_long_name_if_omitted()
18+
{
19+
// Given
20+
var options = new OptionsWithImplicitLongName();
21+
var parser = new CommandLine.Parser();
22+
var arguments = new[] {
23+
"--segments", "header.txt:body.txt:footer.txt"
24+
};
25+
26+
// When
27+
var result = parser.ParseArguments(arguments, options);
28+
29+
// Than
30+
result.Should().Be(true);
31+
options.Segments.Should().HaveCount(c => c == 3);
32+
options.Segments.Should().ContainInOrder(new[] { "header.txt", "body.txt", "footer.txt" });
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)