Skip to content

Commit 941e8cb

Browse files
committed
Provided of strongest implementation around IParserState mechanism.
1 parent 79f115b commit 941e8cb

8 files changed

Lines changed: 265 additions & 145 deletions

File tree

CommandLine.sln.DotSettings.user

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/libcmdline/CommandLine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@
7676
<Compile Include="Extensions\CharExtensions.cs" />
7777
<Compile Include="GlobalSuppressions.cs" />
7878
<Compile Include="Infrastructure\PopsicleSetter.cs" />
79-
<Compile Include="Parsing\PropertyWriter.cs" />
8079
<Compile Include="Infrastructure\SR.strings.cs">
8180
<AutoGen>True</AutoGen>
8281
<DesignTime>True</DesignTime>
8382
<DependentUpon>SR.tt</DependentUpon>
8483
</Compile>
84+
<Compile Include="Parsing\PropertyWriter.cs" />
8585
<Compile Include="Infrastructure\Assumes.cs" />
8686
<Compile Include="Infrastructure\ReflectionCache.cs" />
8787
<Compile Include="Extensions\StringExtensions.cs" />

src/libcmdline/Infrastructure/SR.strings

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ CommandLineParserException_IncompatibleTypes = The types are incompatible.
1313
InvalidOperationException_CopyrightInfoRequiresAssemblyCopyrightAttributeOrAssemblyCompanyAttribute = CopyrightInfo::Default requires that you define AssemblyCopyrightAttribute or AssemblyCompanyAttribute.
1414
ArgumentNullException_OnVerbDelegateCannotBeNull = Delegate executed to capture verb command instance reference cannot be null.
1515
ArgumentNullException_ParserSettingsDelegateCannotBeNull = The command line parser settings delegate cannot be null.
16-
InvalidOperationException_ParserSettingsInstanceCanBeUsedOnce = The command line parserSettings instance cannnot be used more than once.
16+
InvalidOperationException_ParserSettingsInstanceCanBeUsedOnce = The command line parserSettings instance cannnot be used more than once.
17+
InvalidOperationException_ParserStateInstanceCannotBeNotNull = ParserState instance cannot be supplied.
18+
InvalidOperationException_ParserStateInstanceBadApplied = Cannot apply ParserStateAttribute to a property that does not implement IParserState or is not accessible.

src/libcmdline/Infrastructure/SR.strings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,9 @@ internal static class SR
4343

4444
public const string InvalidOperationException_ParserSettingsInstanceCanBeUsedOnce = "The command line parserSettings instance cannnot be used more than once.";
4545

46+
public const string InvalidOperationException_ParserStateInstanceCannotBeNotNull = "ParserState instance cannot be supplied.";
47+
48+
public const string InvalidOperationException_ParserStateInstanceBadApplied = "Cannot apply ParserStateAttribute to a property that does not implement IParserState or is not accessible.";
49+
4650
}
4751
}

src/libcmdline/Infrastructure/SR.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<#@ import namespace="System.Text" #>
55
<#@ import namespace="System.Collections.Generic" #>
66
<#@ import namespace="System.IO" #>
7-
<#@ output extension=".cs" #>
7+
<#@ output extension=".strings.cs" #>
88
//------------------------------------------------------------------------------
99
// <auto-generated>
1010
// This code was generated by http://t4.codeplex.com/.

src/libcmdline/Parser.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,36 @@ private static void SetParserStateIfNeeded(object options, IEnumerable<ParsingEr
288288

289289
var property = ReflectionHelper.RetrievePropertyList<ParserStateAttribute>(options)[0].Left;
290290

291-
// Developers are entitled to provide their implementation and instance
292-
if (property.GetValue(options, null) == null)
291+
var parserState = property.GetValue(options, null);
292+
if (parserState != null)
293293
{
294-
// Otherwise the parser will the default one
295-
property.SetValue(options, new ParserState(), null);
294+
if (!(parserState is IParserState))
295+
{
296+
throw new InvalidOperationException(SR.InvalidOperationException_ParserStateInstanceBadApplied);
297+
}
298+
299+
if (!(parserState is ParserState))
300+
{
301+
throw new InvalidOperationException(SR.InvalidOperationException_ParserStateInstanceCannotBeNotNull);
302+
}
303+
}
304+
else
305+
{
306+
try
307+
{
308+
property.SetValue(options, new ParserState(), null);
309+
}
310+
catch (Exception ex)
311+
{
312+
throw new InvalidOperationException(SR.InvalidOperationException_ParserStateInstanceBadApplied, ex);
313+
}
296314
}
297315

298-
var parserState = (IParserState)property.GetValue(options, null);
316+
var state = (IParserState)property.GetValue(options, null);
299317

300318
foreach (var error in errors)
301319
{
302-
parserState.Errors.Add(error);
320+
state.Errors.Add(error);
303321
}
304322
}
305323

@@ -392,10 +410,8 @@ private bool DoParseArgumentsVerbs(string[] args, object options, ref object ver
392410

393411
var optionMap = OptionMap.Create(options, verbs, _settings);
394412

395-
// Read the verb from command line arguments
396413
if (TryParseHelpVerb(args, options, helpInfo, optionMap))
397414
{
398-
// Since user requested help, parsing is considered a fail
399415
return false;
400416
}
401417

src/tests/CommandLine.Tests.csproj

Lines changed: 134 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,135 @@
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\SingletonFixture.cs" />
80-
<Compile Include="Unit\Parser\StrictFixture.cs" />
81-
<Compile Include="Unit\Parser\UnknownArgumentsFixture.cs" />
82-
<Compile Include="Unit\Parser\ValueListAttributeParsingFixture.cs" />
83-
<Compile Include="Unit\Parser\ValueOptionAttributeParsingFixture.cs" />
84-
<Compile Include="Unit\Parser\VerbsFixture.cs" />
85-
<Compile Include="Properties\AssemblyInfo.cs" />
86-
<Compile Include="Unit\BaseFixture.cs" />
87-
<Compile Include="Unit\ParserBaseFixture.cs" />
88-
<Compile Include="Unit\DebugStringUtil.cs" />
89-
<Compile Include="Fakes\BooleanSetOptions.cs" />
90-
<Compile Include="Fakes\ComplexOptions.cs" />
91-
<Compile Include="Fakes\MixedCaseOptions.cs" />
92-
<Compile Include="Fakes\NullableTypesOptions.cs" />
93-
<Compile Include="Fakes\NumberSetOptions.cs" />
94-
<Compile Include="Fakes\OptionsBase.cs" />
95-
<Compile Include="Fakes\OptionsWithDefaultSet.cs" />
96-
<Compile Include="Fakes\OptionsWithMultipleSet.cs" />
97-
<Compile Include="Fakes\OptionsWithMultipleSetAndOneOption.cs" />
98-
<Compile Include="Fakes\OptionsWithValueListMaximumThree.cs" />
99-
<Compile Include="Fakes\OptionsWithValueListMaximumZero.cs" />
100-
<Compile Include="Fakes\SimpleOptions.cs" />
101-
<Compile Include="Fakes\SimpleOptionsWithArray.cs" />
102-
<Compile Include="Fakes\SimpleOptionsWithArrayAndValueList.cs" />
103-
<Compile Include="Fakes\SimpleOptionsWithBadOptionArray.cs" />
104-
<Compile Include="Fakes\SimpleOptionsWithEnum.cs" />
105-
<Compile Include="Fakes\SimpleOptionsWithHelpOption.cs" />
106-
<Compile Include="Fakes\SimpleOptionsWithOptionList.cs" />
107-
<Compile Include="Fakes\SimpleOptionsWithValueList.cs" />
108-
<Compile Include="Fakes\SimpleOptionsWithDefaults.cs" />
109-
<Compile Include="Fakes\SimpleOptionsWithBadDefaults.cs" />
110-
<Compile Include="Fakes\SimpleOptionsWithDefaultArray.cs" />
111-
<Compile Include="Fakes\OptionsForErrorsScenario.cs" />
112-
<Compile Include="Fakes\SimpleOptionsForAutoBuid.cs" />
113-
<Compile Include="Fakes\OptionsForPlugInScenario.cs" />
114-
<Compile Include="Fakes\OptionsWithUIntArray.cs" />
115-
<Compile Include="Unit\Text\CopyrightInfoFixture.cs" />
116-
<Compile Include="Unit\Text\HeadingInfoFixture.cs" />
117-
<Compile Include="Unit\Text\HelpTextFixture.cs" />
118-
<Compile Include="Unit\Text\MultiLineTextAttributeFixture.cs" />
119-
<Compile Include="Unit\Text\VerbsHelpTextFixture.cs" />
120-
<Compile Include="Unit\Infrastructure\ReflectionHelperFixture.cs" />
121-
</ItemGroup>
122-
<ItemGroup>
123-
<None Include="packages.config" />
124-
</ItemGroup>
125-
<ItemGroup>
126-
<ProjectReference Include="..\libcmdline\CommandLine.csproj">
127-
<Project>{5DEA2811-2FFA-4959-830B-CAD3ACACABEB}</Project>
128-
<Name>CommandLine</Name>
129-
</ProjectReference>
130-
</ItemGroup>
131-
<ItemGroup />
132-
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
133-
<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\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" />
134135
</Project>

0 commit comments

Comments
 (0)