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

Fix unparsing FileInfo and DirectoryInfo #627

Merged
merged 2 commits into from
Jun 16, 2020
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
12 changes: 7 additions & 5 deletions src/CommandLine/UnParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,16 @@ private static string FormatValue(Specification spec, object value)

private static object FormatWithQuotesIfString(object value)
{
if (value is DateTime || value is DateTimeOffset) return $"\"{value}\"";
string s = value.ToString();
if (!string.IsNullOrEmpty(s) && !s.Contains("\"") && s.Contains(" "))
return $"\"{s}\"";

Func<string, string> doubQt = v
=> v.Contains("\"") ? v.Replace("\"", "\\\"") : v;

return (value as string)
.ToMaybe()
.MapValueOrDefault(v => v.Contains(' ') || v.Contains("\"")
? "\"".JoinTo(doubQt(v), "\"") : v, value);
return s.ToMaybe()
.MapValueOrDefault(v => v.Contains(' ') || v.Contains("\"")
? "\"".JoinTo(doubQt(v), "\"") : v, value);
}

private static char SeperatorOrSpace(this Specification spec)
Expand Down
18 changes: 18 additions & 0 deletions tests/CommandLine.Tests/Fakes/Options_With_FileDirectoryInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System.IO;

namespace CommandLine.Tests.Fakes
{
public class Options_With_FileDirectoryInfo
{
[Option('s', "stringPath")]
public string StringPath { get; set; }

[Option('f', "filePath")]
public FileInfo FilePath { get; set; }

[Option('d', "directoryPath")]
public DirectoryInfo DirectoryPath { get; set; }
}
}
19 changes: 19 additions & 0 deletions tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using FluentAssertions;
Expand All @@ -23,6 +24,15 @@ public static void UnParsing_instance_returns_command_line(Simple_Options option
.Should().BeEquivalentTo(result);
}

[Theory]
[MemberData(nameof(UnParseFileDirectoryData))]
public static void UnParsing_instance_returns_command_line_for_file_directory_paths(Options_With_FileDirectoryInfo options, string result)
{
new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo(result);
}

[Theory]
[MemberData(nameof(UnParseDataVerbs))]
public static void UnParsing_instance_returns_command_line_for_verbs(Add_Verb verb, string result)
Expand Down Expand Up @@ -298,6 +308,15 @@ public static IEnumerable<object[]> UnParseData
}
}

public static IEnumerable<object[]> UnParseFileDirectoryData
{
get
{
yield return new object[] { new Options_With_FileDirectoryInfo(), "" };
yield return new object[] { new Options_With_FileDirectoryInfo { FilePath = new FileInfo(@"C:\my path\with spaces\file with spaces.txt"), DirectoryPath = new DirectoryInfo(@"C:\my path\with spaces\"), StringPath = @"C:\my path\with spaces\file with spaces.txt" }, @"--directoryPath ""C:\my path\with spaces\"" --filePath ""C:\my path\with spaces\file with spaces.txt"" --stringPath ""C:\my path\with spaces\file with spaces.txt""" };
}
}


public static IEnumerable<object[]> UnParseDataVerbs
{
Expand Down