Skip to content
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

git style commands #82

Merged
merged 23 commits into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c91af7a
Issue #29 - Add help option to FCLP interface so it can be called man…
siywilliams Feb 9, 2015
5af2c93
Merge branch 'develop'
siywilliams Feb 9, 2015
ad107bb
added command infrastructure to the fclp. The syntax is there but is …
siywilliams Feb 27, 2015
6a1238e
a rough implementation of commands. needs a lot of refactoring but th…
siywilliams Mar 18, 2015
51a6ac4
use constructor overload instead of default parameter
siywilliams Mar 18, 2015
0d0ccfe
ignore commands in validation tests for now
siywilliams Mar 18, 2015
75c89f4
setup correct string comparion for tests
siywilliams Mar 18, 2015
45875b2
- rename callback to OnSuccess
siywilliams Mar 19, 2015
ec1e19a
Merge branch 'develop'
siywilliams Jun 10, 2015
245f928
Merge branch 'develop'
siywilliams Jul 21, 2015
8331798
Merge branch 'develop'
siywilliams Jul 21, 2015
6a9b9ab
Merge branch 'develop'
siywilliams Jul 21, 2015
946df5c
Merge branch 'master' into commands
siywilliams Sep 19, 2015
05efcdc
add abililty to assign orphan arguments to a (primary) option, for ex…
siywilliams Feb 15, 2016
d586030
only manipulate results when not running with commands
siywilliams Feb 15, 2016
8019c57
- fix issue with validation of new options
siywilliams Feb 17, 2016
8e899d8
fix issue with nullable bool not registering "my.exe -bool" as TRUE o…
siywilliams Apr 5, 2016
f95a609
Merge branch 'develop' into commands
siywilliams Apr 17, 2017
fbb292d
Merge branch 'develop' into commands
siywilliams Apr 17, 2017
98e1667
use new UseForOrphanArguments() to remove need for -f in example args
siywilliams Apr 17, 2017
200344d
Merge branch 'commands' of https://github.com/fclp/fluent-command-lin…
siywilliams Apr 17, 2017
0f6ba15
-b0 and -b1 short params are no longer valid, removing the tests.
siywilliams Nov 25, 2017
5a5f9d9
If option is nullable, then let the parser itself decide if an empty …
siywilliams Nov 25, 2017
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
6 changes: 6 additions & 0 deletions FluentCommandLineParser.Examples/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>

<supportedRuntime version="v2.0.50727" sku="Client"/></startup>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.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>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{47A9E194-B92E-4929-A7A8-396BCD46A844}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Fclp.Examples</RootNamespace>
<AssemblyName>FluentCommandLineParser.Examples</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FluentCommandLineParser\FluentCommandLineParser.csproj">
<Project>{74cdfa61-81d8-40f2-b536-949baba15d3e}</Project>
<Name>FluentCommandLineParser</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.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>
74 changes: 74 additions & 0 deletions FluentCommandLineParser.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;

namespace Fclp.Examples
{
class CommandProgram
{
// Contains all arguments for the add command
class AddArgs
{
public bool Verbose { get; set; }
public bool IgnoreErrors { get; set; }
public List<string> Files { get; set; }
public List<string> Files2 { get; set; }
}

// Contains all arguments for the remove command
class RemoveArgs
{
public bool Verbose { get; set; }
public List<string> Files { get; set; }
}

static void Main(string[] args)
{
var fclp = new FluentCommandLineParser();

// use new SetupCommand method to initialise a command
var addCmd = fclp.SetupCommand<AddArgs>("add")
.OnSuccess(Add); // executed when the add command is used

// the standard fclp framework, except against the created command rather than the fclp itself
addCmd.Setup(addArgs => addArgs.Verbose)
.As('v', "verbose")
.SetDefault(false)
.WithDescription("Be verbose");

addCmd.Setup(addArgs => addArgs.IgnoreErrors)
.As("ignore-errors")
.SetDefault(false)
.WithDescription("If some files could not be added, do not abort");

addCmd.Setup(addArgs => addArgs.Files)
.As('f', "files")
.WithDescription("Files to be tracked")
.UseForOrphanArguments();

// add the remove command
var remCmd = fclp.SetupCommand<RemoveArgs>("rem")
.OnSuccess(Remove); // executed when the remove command is used

remCmd.Setup(removeArgs => removeArgs.Verbose)
.As('v', "verbose")
.SetDefault(false)
.WithDescription("Be verbose");

remCmd.Setup(removeArgs => removeArgs.Files)
.As('f', "files")
.WithDescription("Files to be untracked")
.UseForOrphanArguments();

fclp.Parse(args);
}

static void Add(AddArgs args)
{
// add was executed
}

static void Remove(RemoveArgs args)
{
// remove was executed
}
}
}
36 changes: 36 additions & 0 deletions FluentCommandLineParser.Examples/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FluentCommandLineParser.Examples")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FluentCommandLineParser.Examples")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e5d3cab1-0e75-42ba-b39a-516de3b732a6")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
11 changes: 11 additions & 0 deletions FluentCommandLineParser.Tests/Commands/AddArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace Fclp.Tests.Commands
{
class AddArgs
{
public bool Verbose { get; set; }
public bool IgnoreErrors { get; set; }
public List<string> Files { get; set; }
}
}
133 changes: 133 additions & 0 deletions FluentCommandLineParser.Tests/Commands/CommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using Fclp.Tests.Extensions;
using NUnit.Framework;

namespace Fclp.Tests.Commands
{
[TestFixture]
public class CommandTests
{
static Fclp.FluentCommandLineParser SetupParser(Action<AddArgs> addCallback, Action<RemoveArgs> removeCallback)
{
var fclp = new Fclp.FluentCommandLineParser();

// use new SetupCommand method to initialise a command
var addCmd = fclp.SetupCommand<AddArgs>("add")
.OnSuccess(addCallback); // executed when the add command is used

// the standard fclp framework, except against the created command rather than the fclp itself
addCmd.Setup(addArgs => addArgs.Verbose)
.As('v', "verbose")
.SetDefault(false)
.WithDescription("Be verbose");

addCmd.Setup(addArgs => addArgs.IgnoreErrors)
.As("ignore-errors")
.SetDefault(false)
.WithDescription("If some files could not be added, do not abort");

addCmd.Setup(addArgs => addArgs.Files)
.As('f', "files")
.WithDescription("Files to be tracked")
.UseForOrphanArguments();

// add the remove command
var remCmd = fclp.SetupCommand<RemoveArgs>("rem")
.OnSuccess(removeCallback); // executed when the remove command is used

remCmd.Setup(removeArgs => removeArgs.Verbose)
.As('v', "verbose")
.SetDefault(false)
.WithDescription("Be verbose");

remCmd.Setup(removeArgs => removeArgs.Files)
.As('f', "files")
.WithDescription("Files to be untracked")
.UseForOrphanArguments();

return fclp;
}

[Test]
public void Add_Command_Usage()
{
const string args = "add --files a.txt b.txt c.txt --verbose --ignore-errors";

AddArgs addArgs = null;
RemoveArgs removeArgs = null;

var fclp = SetupParser(x => addArgs = x, x => removeArgs = x);

var result = fclp.Parse(args.AsCmdArgs());

Assert.IsFalse(result.HasErrors);
Assert.IsNull(removeArgs);
Assert.IsNotNull(addArgs);
Assert.IsTrue(addArgs.Verbose);
Assert.IsTrue(addArgs.IgnoreErrors);
Assert.That(addArgs.Files, Is.EquivalentTo(new[] { "a.txt", "b.txt", "c.txt" }));
}

[Test]
public void Add_Command_With_Orphan()
{
const string args = "add a.txt b.txt c.txt --verbose --ignore-errors";

AddArgs addArgs = null;
RemoveArgs removeArgs = null;

var fclp = SetupParser(x => addArgs = x, x => removeArgs = x);

var result = fclp.Parse(args.AsCmdArgs());

Assert.IsFalse(result.HasErrors);
Assert.IsNull(removeArgs);
Assert.IsNotNull(addArgs);
Assert.IsTrue(addArgs.Verbose);
Assert.IsTrue(addArgs.IgnoreErrors);
Assert.IsNotNull(addArgs.Files);
Assert.That(addArgs.Files, Is.EquivalentTo(new[] { "a.txt", "b.txt", "c.txt" }));
}

[Test]
public void Add_Command_With_Only_Orphan()
{
const string args = "add a.txt b.txt c.txt";

AddArgs addArgs = null;
RemoveArgs removeArgs = null;

var fclp = SetupParser(x => addArgs = x, x => removeArgs = x);

var result = fclp.Parse(args.AsCmdArgs());

Assert.IsFalse(result.HasErrors);
Assert.IsNull(removeArgs);
Assert.IsNotNull(addArgs);
Assert.IsFalse(addArgs.Verbose);
Assert.IsFalse(addArgs.IgnoreErrors);
Assert.IsNotNull(addArgs.Files);
Assert.That(addArgs.Files, Is.EquivalentTo(new[] { "a.txt", "b.txt", "c.txt" }));
}

[Test]
public void Remove_Command_Usage()
{
const string args = "rem --files a.txt b.txt c.txt --verbose";

AddArgs addArgs = null;
RemoveArgs removeArgs = null;

var fclp = SetupParser(x => addArgs = x, x => removeArgs = x);

var result = fclp.Parse(args.AsCmdArgs());

Assert.IsFalse(result.HasErrors);
Assert.IsNull(addArgs);
Assert.IsNotNull(removeArgs);
Assert.IsTrue(removeArgs.Verbose);
Assert.IsNotNull(removeArgs.Files);
Assert.That(removeArgs.Files, Is.EquivalentTo(new[] { "a.txt", "b.txt", "c.txt" }));
}
}
}
10 changes: 10 additions & 0 deletions FluentCommandLineParser.Tests/Commands/RemoveArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Fclp.Tests.Commands
{
class RemoveArgs
{
public bool Verbose { get; set; }
public List<string> Files { get; set; }
}
}
20 changes: 20 additions & 0 deletions FluentCommandLineParser.Tests/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Linq;
using Fclp.Internals.Extensions;

namespace Fclp.Tests.Extensions
{
public static class StringExtensions
{
public static string[] AsCmdArgs(this string args)
{
args = ReplaceWithDoubleQuotes(args);
return args.SplitOnWhitespace().ToArray();
}

private static string ReplaceWithDoubleQuotes(string args)
{
if (args == null) return null;
return args.Replace('\'', '"');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
<Compile Include="Integration\Lists\Int64ListInlineDataAttribute.cs" />
<Compile Include="UriTests.cs" />
<Compile Include="CommandLineOptionFormatterTests.cs" />
<Compile Include="Commands\AddArgs.cs" />
<Compile Include="Commands\CommandTests.cs" />
<Compile Include="Commands\RemoveArgs.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="FluentCommandLineParserBuilderTests.cs" />
<Compile Include="FluentCommandLineParserMSpecTests.cs" />
<Compile Include="FluentCommandLineParserTests.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class with_a_parser_engine_that_is_custom : FluentCommandLineParserTestCo
args = new string[0];

mockedEngine
.Setup(x => x.Parse(args))
.Setup(x => x.Parse(args, false))
.Returns(Create<ParserEngineResult>())
.Verifiable();
};
Expand All @@ -59,7 +59,7 @@ public class with_a_parser_engine_that_is_custom : FluentCommandLineParserTestCo
};

It should_replace_the_old_engine = () => sut.ParserEngine.ShouldBeTheSameAs(customEngine);
It should_be_used_to_parse_the_args = () => mockedEngine.Verify(x => x.Parse(args));
It should_be_used_to_parse_the_args = () => mockedEngine.Verify(x => x.Parse(args, false));
}
}
}
Loading