Skip to content

added portable library and attempt to handle free arguments #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -151,12 +152,6 @@
<Compile Include="Mspec\Subjects.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FluentCommandLineParser\FluentCommandLineParser.csproj">
<Project>{74CDFA61-81D8-40F2-B536-949BABA15D3E}</Project>
<Name>FluentCommandLineParser</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Internals\Parsing\OptionParsers\" />
</ItemGroup>
Expand All @@ -167,7 +162,14 @@
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FluentCommandLineParser\FluentCommandLineParserPortable.csproj">
<Project>{0fbc96d2-68f6-4a40-9182-37265883e5bd}</Project>
<Name>FluentCommandLineParserPortable</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class with_a_parser_engine_that_is_custom : FluentCommandLineParserTestCo

mockedEngine
.Setup(x => x.Parse(args))
.Returns(new List<ParsedOption>())
.Returns(new ParserEngineResult(new List<ParsedOption>(), new string[0]))
.Verifiable();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class with_options_that_are_specified_in_the_args : FluentCommandLineParserTestC
args = CreateArgsFromKvp(parserEngineResult);

var parserEngineMock = new Mock<ICommandLineParserEngine>();
parserEngineMock.Setup(x => x.Parse(args)).Returns(parserEngineResult);
parserEngineMock.Setup(x => x.Parse(args)).Returns(new ParserEngineResult(parserEngineResult, new string[0]));
sut.ParserEngine = parserEngineMock.Object;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class with_options_that_have_not_been_setup : FluentCommandLineParserTestContext
args = CreateArgsFromKvp(additionalOptions);

var mockEngine = new Mock<ICommandLineParserEngine>();
mockEngine.Setup(x => x.Parse(args)).Returns(additionalOptions);
mockEngine.Setup(x => x.Parse(args)).Returns(new ParserEngineResult(additionalOptions,new string[0]));
sut.ParserEngine = mockEngine.Object;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#endregion

using Fclp.Internals.Parsing;
using FluentCommandLineParser.Internals.Parsing;
using Machine.Specifications;

namespace Fclp.Tests.Internals
Expand All @@ -38,7 +39,7 @@ abstract class CommandLineOptionGrouperTestContext : TestContextBase<CommandLine

abstract class GroupByOptionTestContext : CommandLineOptionGrouperTestContext
{
protected static string[][] actualResult;
protected static GroupArgumentsByOptionResult actualResult;
protected static string[] args;

Because of = () =>
Expand All @@ -59,16 +60,16 @@ class when_double_dashes_are_used_to_terminate_option_parsing : GroupByOptionTes
};

It should_return_three_sets = () =>
actualResult.Length.ShouldEqual(3);
actualResult.MatchedArgumentsOptionGroups.Length.ShouldEqual(3);

It should_group_the_A_elements = () =>
actualResult[0].ShouldContainOnly("-A", "1", "2", "3");
actualResult.MatchedArgumentsOptionGroups[0].ShouldContainOnly("-A", "1", "2", "3");

It should_group_the_B_elements = () =>
actualResult[1].ShouldContainOnly("-B", "a", "b", "c");
actualResult.MatchedArgumentsOptionGroups[1].ShouldContainOnly("-B", "a", "b", "c");

It should_group_the_C_elements = () =>
actualResult[2].ShouldContainOnly("-C", "--", "-a", "-1", "-b");
actualResult.MatchedArgumentsOptionGroups[2].ShouldContainOnly("-C", "--", "-a", "-1", "-b");
}

class when_double_dashes_are_used_immediately_to_terminate_option_parsing : GroupByOptionTestContext
Expand All @@ -82,10 +83,10 @@ class when_double_dashes_are_used_immediately_to_terminate_option_parsing : Grou
};

It should_return_a_single_set = () =>
actualResult.Length.ShouldEqual(1);
actualResult.MatchedArgumentsOptionGroups.Length.ShouldEqual(1);

It should_group_the_A_elements = () =>
actualResult[0].ShouldContainOnly("--", "-A", "1", "2", "3", "-B", "a", "b", "c", "-C", "-a", "-1", "-b");
actualResult.MatchedArgumentsOptionGroups[0].ShouldContainOnly("--", "-A", "1", "2", "3", "-B", "a", "b", "c", "-C", "-a", "-1", "-b");
}

class when_there_are_only_arguments_and_no_options : GroupByOptionTestContext
Expand All @@ -94,10 +95,10 @@ class when_there_are_only_arguments_and_no_options : GroupByOptionTestContext
args = new[] { "0", "1", "2", "3" };

It should_return_a_single_set = () =>
actualResult.Length.ShouldEqual(1);
actualResult.MatchedArgumentsOptionGroups.Length.ShouldEqual(1);

It should_group_all_the_provided_elements = () =>
actualResult[0].ShouldContainOnly("0", "1", "2", "3");
actualResult.MatchedArgumentsOptionGroups[0].ShouldContainOnly("0", "1", "2", "3");
}

class when_there_is_a_double_dash_but_no_options_before_it : GroupByOptionTestContext
Expand All @@ -106,10 +107,10 @@ class when_there_is_a_double_dash_but_no_options_before_it : GroupByOptionTestCo
args = new[] { "--", "-A", "1", "2", "3" };

It should_return_a_single_set = () =>
actualResult.Length.ShouldEqual(1);
actualResult.MatchedArgumentsOptionGroups.Length.ShouldEqual(1);

It should_group_all_the_provided_elements = () =>
actualResult[0].ShouldContainOnly("--", "-A", "1", "2", "3");
actualResult.MatchedArgumentsOptionGroups[0].ShouldContainOnly("--", "-A", "1", "2", "3");
}

class when_there_is_a_double_dash_at_the_end : GroupByOptionTestContext
Expand All @@ -123,16 +124,16 @@ class when_there_is_a_double_dash_at_the_end : GroupByOptionTestContext
};

It should_return_three_sets = () =>
actualResult.Length.ShouldEqual(3);
actualResult.MatchedArgumentsOptionGroups.Length.ShouldEqual(3);

It should_group_the_A_elements = () =>
actualResult[0].ShouldContainOnly("-A", "1", "2", "3");
actualResult.MatchedArgumentsOptionGroups[0].ShouldContainOnly("-A", "1", "2", "3");

It should_group_the_B_elements = () =>
actualResult[1].ShouldContainOnly("-B", "a", "b", "c");
actualResult.MatchedArgumentsOptionGroups[1].ShouldContainOnly("-B", "a", "b", "c");

It should_group_the_C_elements = () =>
actualResult[2].ShouldContainOnly("-C", "a1", "b1", "c1", "--");
actualResult.MatchedArgumentsOptionGroups[2].ShouldContainOnly("-C", "a1", "b1", "c1", "--");
}

class when_options_are_repeated : GroupByOptionTestContext
Expand All @@ -141,13 +142,13 @@ class when_options_are_repeated : GroupByOptionTestContext
args = new[] { "-A", "1", "2", "3", "-A", "4", "5", "6" };

It should_return_two_sets = () =>
actualResult.Length.ShouldEqual(2);
actualResult.MatchedArgumentsOptionGroups.Length.ShouldEqual(2);

It should_group_the_first_elements = () =>
actualResult[0].ShouldContainOnly("-A", "1", "2", "3");
actualResult.MatchedArgumentsOptionGroups[0].ShouldContainOnly("-A", "1", "2", "3");

It should_group_the_second_repeated_elements = () =>
actualResult[1].ShouldContainOnly("-A", "4", "5", "6");
actualResult.MatchedArgumentsOptionGroups[1].ShouldContainOnly("-A", "4", "5", "6");
}

class when_the_args_is_empty : GroupByOptionTestContext
Expand All @@ -159,7 +160,7 @@ class when_the_args_is_empty : GroupByOptionTestContext
error.ShouldBeNull();

It should_return_empty_args = () =>
actualResult.ShouldBeEmpty();
actualResult.MatchedArgumentsOptionGroups.ShouldBeEmpty();
}
}
}
13 changes: 13 additions & 0 deletions FluentCommandLineParser.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCommandLineParser", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCommandLineParser.Tests", "FluentCommandLineParser.Tests\FluentCommandLineParser.Tests.csproj", "{A2546703-0B86-4515-BE5B-FAF85B756BDC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{BD4336C1-BCBA-41CF-BBA0-49D3793E0C27}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCommandLineParserPortable", "FluentCommandLineParser\FluentCommandLineParserPortable.csproj", "{0FBC96D2-68F6-4A40-9182-37265883E5BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -19,6 +28,10 @@ Global
{A2546703-0B86-4515-BE5B-FAF85B756BDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2546703-0B86-4515-BE5B-FAF85B756BDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2546703-0B86-4515-BE5B-FAF85B756BDC}.Release|Any CPU.Build.0 = Release|Any CPU
{0FBC96D2-68F6-4A40-9182-37265883E5BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0FBC96D2-68F6-4A40-9182-37265883E5BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FBC96D2-68F6-4A40-9182-37265883E5BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FBC96D2-68F6-4A40-9182-37265883E5BD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
14 changes: 10 additions & 4 deletions FluentCommandLineParser/FluentCommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ internal IHelpCommandLineOption HelpOption
/// </exception>
public ICommandLineOptionFluent<T> Setup<T>(char shortOption, string longOption)
{
return SetupInternal<T>(shortOption.ToString(CultureInfo.InvariantCulture), longOption);
return SetupInternal<T>(shortOption.ToString(), longOption);
}

/// <summary>
Expand Down Expand Up @@ -198,7 +198,7 @@ private ICommandLineOptionFluent<T> SetupInternal<T>(string shortOption, string
/// </exception>
public ICommandLineOptionFluent<T> Setup<T>(char shortOption)
{
return SetupInternal<T>(shortOption.ToString(CultureInfo.InvariantCulture), null);
return SetupInternal<T>(shortOption.ToString(), null);
}

/// <summary>
Expand All @@ -221,9 +221,11 @@ public ICommandLineOptionFluent<T> Setup<T>(string longOption)
/// <returns>An <see cref="ICommandLineParserResult"/> representing the results of the parse operation.</returns>
public ICommandLineParserResult Parse(string[] args)
{
var parsedOptions = this.ParserEngine.Parse(args).ToList();
var parsedRes = this.ParserEngine.Parse(args);
var parsedOptions=parsedRes.ParsedOptions.ToList();

var result = new CommandLineParserResult { EmptyArgs = parsedOptions.IsNullOrEmpty() };
result.UnMatchedArgs=parsedRes.AdditionalValues;

if (this.HelpOption.ShouldShowHelp(parsedOptions, StringComparison))
{
Expand Down Expand Up @@ -252,7 +254,11 @@ public ICommandLineParserResult Parse(string[] args)

try
{
option.Bind(match);
IEnumerable<string> unmatchedArgs=option.Bind(match);
if (unmatchedArgs != null)
{
result.UnMatchedArgs = result.UnMatchedArgs.Concat(unmatchedArgs);
}
}
catch (OptionSyntaxException)
{
Expand Down
3 changes: 3 additions & 0 deletions FluentCommandLineParser/FluentCommandLineParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
</ItemGroup>
<ItemGroup>
<Compile Include="CommandLineParserErrorFormatter.cs" />
Expand Down Expand Up @@ -82,12 +83,14 @@
<Compile Include="IFluentCommandLineParser.cs" />
<Compile Include="Internals\Parsing\CommandLineParserEngineMark2.cs" />
<Compile Include="Internals\Parsing\CommandLineParserResult.cs" />
<Compile Include="Internals\Parsing\GroupArgumentsByOptionResult.cs" />
<Compile Include="Internals\Parsing\ICommandLineOptionParserFactory.cs" />
<Compile Include="Internals\Parsing\ICommandLineParserEngine.cs" />
<Compile Include="Internals\Parsing\OptionArgumentParser.cs" />
<Compile Include="Internals\Parsing\CommandLineOptionGrouper.cs" />
<Compile Include="Internals\Parsing\OptionParsers\BoolCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\CommandLineOptionParserFactory.cs" />
<Compile Include="Internals\Parsing\OptionParsers\CommandLineOptionParserResult.cs" />
<Compile Include="Internals\Parsing\OptionParsers\DateTimeCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\DoubleCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\ICommandLineOptionParser.cs" />
Expand Down
104 changes: 104 additions & 0 deletions FluentCommandLineParser/FluentCommandLineParserPortable.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0FBC96D2-68F6-4A40-9182-37265883E5BD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FluentCommandLineParserPortable</RootNamespace>
<AssemblyName>FluentCommandLineParserPortable</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Profile88</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</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>
<Compile Include="CommandLineParserErrorFormatter.cs" />
<Compile Include="FluentCommandLineBuilder.cs" />
<Compile Include="FluentCommandLineParser.cs" />
<Compile Include="ICommandLineOptionBuilderFluent.cs" />
<Compile Include="ICommandLineOptionFluent.cs" />
<Compile Include="ICommandLineOptionFormatter.cs" />
<Compile Include="ICommandLineParserError.cs" />
<Compile Include="ICommandLineParserErrorFormatter.cs" />
<Compile Include="ICommandLineParserResult.cs" />
<Compile Include="IFluentCommandLineBuilder.cs" />
<Compile Include="IFluentCommandLineParser.cs" />
<Compile Include="IHelpCommandLineOptionFluent.cs" />
<Compile Include="Internals\CommandLineOption.cs" />
<Compile Include="Internals\CommandLineOptionBuilderFluent.cs" />
<Compile Include="Internals\CommandLineOptionFactory.cs" />
<Compile Include="Internals\CommandLineOptionFormatter.cs" />
<Compile Include="Internals\EmptyHelpCommandLineOption.cs" />
<Compile Include="Internals\Errors\CommandLineParserErrorBase.cs" />
<Compile Include="Internals\Errors\ExpectedOptionNotFoundParseError.cs" />
<Compile Include="Internals\Errors\OptionSyntaxParseError.cs" />
<Compile Include="Internals\Extensions\UsefulExtension.cs" />
<Compile Include="Internals\HelpCommandLineOption.cs" />
<Compile Include="Internals\ICommandLineOption.cs" />
<Compile Include="Internals\ICommandLineOptionFactory.cs" />
<Compile Include="Internals\ICommandLineOptionResult.cs" />
<Compile Include="Internals\IHelpCommandLineOption.cs" />
<Compile Include="Internals\IHelpCommandLineOptionResult.cs" />
<Compile Include="Internals\Parsing\CommandLineOptionGrouper.cs" />
<Compile Include="Internals\Parsing\CommandLineParserEngineMark2.cs" />
<Compile Include="Internals\Parsing\CommandLineParserResult.cs" />
<Compile Include="Internals\Parsing\GroupArgumentsByOptionResult.cs" />
<Compile Include="Internals\Parsing\ICommandLineOptionParserFactory.cs" />
<Compile Include="Internals\Parsing\ICommandLineParserEngine.cs" />
<Compile Include="Internals\Parsing\OptionArgumentParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\BoolCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\CommandLineOptionParserFactory.cs" />
<Compile Include="Internals\Parsing\OptionParsers\CommandLineOptionParserResult.cs" />
<Compile Include="Internals\Parsing\OptionParsers\DateTimeCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\DoubleCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\ICommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\Int32CommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\ListCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\StringCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\ParsedOption.cs" />
<Compile Include="Internals\Parsing\ParsedOptionFactory.cs" />
<Compile Include="Internals\Parsing\ParserEngineResult.cs" />
<Compile Include="Internals\SpecialCharacters.cs" />
<Compile Include="Internals\Validators\CommandLineOptionValidator.cs" />
<Compile Include="Internals\Validators\ICommandLineOptionValidator.cs" />
<Compile Include="Internals\Validators\NoDuplicateOptionValidator.cs" />
<Compile Include="Internals\Validators\OptionNameValidator.cs" />
<Compile Include="InvalidOptionNameException.cs" />
<Compile Include="OptionAlreadyExistsException.cs" />
<Compile Include="OptionSyntaxException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnsupportedTypeException.cs" />
</ItemGroup>
<ItemGroup>
<None Include="FluentCommandLineParser.snk" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.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>
5 changes: 5 additions & 0 deletions FluentCommandLineParser/ICommandLineParserResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ public interface ICommandLineParserResult
/// Contains all the setup options that were not matched during the parse operation.
/// </summary>
IEnumerable<ICommandLineOption> UnMatchedOptions { get; }

/// <summary>
/// Contains all arguments that could not be matched to any option
/// </summary>
IEnumerable<string> UnMatchedArgs { get; }
}
}
Loading