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

TimeSpan option parser #72

Merged
merged 1 commit into from
Apr 17, 2017
Merged
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
1 change: 1 addition & 0 deletions FluentCommandLineParser/FluentCommandLineParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Internals\Parsing\ICommandLineParserEngine.cs" />
<Compile Include="Internals\Parsing\OptionArgumentParser.cs" />
<Compile Include="Internals\Parsing\CommandLineOptionGrouper.cs" />
<Compile Include="Internals\Parsing\OptionParsers\TimeSpanCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\Int64CommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\NullableCommandLineOptionParser.cs" />
<Compile Include="Internals\Parsing\OptionParsers\NullableEnumCommandLineOptionParser.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ public CommandLineOptionParserFactory()
this.AddOrReplace(new Int64CommandLineOptionParser());
this.AddOrReplace(new StringCommandLineOptionParser());
this.AddOrReplace(new DateTimeCommandLineOptionParser());
this.AddOrReplace(new TimeSpanCommandLineOptionParser());
this.AddOrReplace(new DoubleCommandLineOptionParser());
this.AddOrReplace(new UriCommandLineOptionParser());
this.AddOrReplace(new ListCommandLineOptionParser<string>(this));
this.AddOrReplace(new ListCommandLineOptionParser<int>(this));
this.AddOrReplace(new ListCommandLineOptionParser<long>(this));
this.AddOrReplace(new ListCommandLineOptionParser<double>(this));
this.AddOrReplace(new ListCommandLineOptionParser<DateTime>(this));
this.AddOrReplace(new ListCommandLineOptionParser<TimeSpan>(this));
this.AddOrReplace(new ListCommandLineOptionParser<bool>(this));
this.AddOrReplace(new ListCommandLineOptionParser<Uri>(this));
this.AddOrReplace(new NullableCommandLineOptionParser<bool>(this));
this.AddOrReplace(new NullableCommandLineOptionParser<int>(this));
this.AddOrReplace(new NullableCommandLineOptionParser<long>(this));
this.AddOrReplace(new NullableCommandLineOptionParser<double>(this));
this.AddOrReplace(new NullableCommandLineOptionParser<DateTime>(this));
this.AddOrReplace(new NullableCommandLineOptionParser<TimeSpan>(this));
}

internal Dictionary<Type, object> Parsers { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#region License
// DateTimeCommandLineOptionParser.cs
// Copyright (c) 2013, Simon Williams
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provide
// d that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#endregion

using System;
using System.Globalization;

namespace Fclp.Internals.Parsing.OptionParsers
{
/// <summary>
/// Parser used to convert to <see cref="System.TimeSpan"/>.
/// </summary>
public class TimeSpanCommandLineOptionParser : ICommandLineOptionParser<TimeSpan>
{
/// <summary>
/// Parses the specified <see cref="System.String"/> into a <see cref="System.TimeSpan"/>.
/// </summary>
/// <param name="parsedOption"></param>
/// <returns></returns>
public TimeSpan Parse(ParsedOption parsedOption)
{
return TimeSpan.Parse(TrimAnyUnwantedCharacters(parsedOption.Value));
}

/// <summary>
/// Determines whether the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>.
/// </summary>
/// <param name="parsedOption"></param>
/// <returns><c>true</c> if the specified <see cref="System.String"/> can be parsed by this <see cref="ICommandLineOptionParser{T}"/>; otherwise <c>false</c>.</returns>
public bool CanParse(ParsedOption parsedOption)
{
TimeSpan dtOut;
return TimeSpan.TryParse(TrimAnyUnwantedCharacters(parsedOption.Value), out dtOut);
}

/// <summary>
/// Trim any unwanted characters such as any remaining double quotes that can come through.
/// </summary>
private static string TrimAnyUnwantedCharacters(string value)
{
return (value ?? string.Empty).Trim('"');
}
}
}