Skip to content

Commit da42349

Browse files
committed
Merge branch 'kapsiR-issue-626' by @kapsiR into develop
2 parents 0154c98 + 6d45b9e commit da42349

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/CommandLine/UnParserExtensions.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,16 @@ private static string FormatValue(Specification spec, object value)
204204

205205
private static object FormatWithQuotesIfString(object value)
206206
{
207-
if (value is DateTime || value is DateTimeOffset) return $"\"{value}\"";
207+
string s = value.ToString();
208+
if (!string.IsNullOrEmpty(s) && !s.Contains("\"") && s.Contains(" "))
209+
return $"\"{s}\"";
210+
208211
Func<string, string> doubQt = v
209212
=> v.Contains("\"") ? v.Replace("\"", "\\\"") : v;
210213

211-
return (value as string)
212-
.ToMaybe()
213-
.MapValueOrDefault(v => v.Contains(' ') || v.Contains("\"")
214-
? "\"".JoinTo(doubQt(v), "\"") : v, value);
214+
return s.ToMaybe()
215+
.MapValueOrDefault(v => v.Contains(' ') || v.Contains("\"")
216+
? "\"".JoinTo(doubQt(v), "\"") : v, value);
215217
}
216218

217219
private static char SeperatorOrSpace(this Specification spec)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2+
3+
using System.IO;
4+
5+
namespace CommandLine.Tests.Fakes
6+
{
7+
public class Options_With_FileDirectoryInfo
8+
{
9+
[Option('s', "stringPath")]
10+
public string StringPath { get; set; }
11+
12+
[Option('f', "filePath")]
13+
public FileInfo FilePath { get; set; }
14+
15+
[Option('d', "directoryPath")]
16+
public DirectoryInfo DirectoryPath { get; set; }
17+
}
18+
}

tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67
using Xunit;
78
using FluentAssertions;
@@ -23,6 +24,15 @@ public static void UnParsing_instance_returns_command_line(Simple_Options option
2324
.Should().BeEquivalentTo(result);
2425
}
2526

27+
[Theory]
28+
[MemberData(nameof(UnParseFileDirectoryData))]
29+
public static void UnParsing_instance_returns_command_line_for_file_directory_paths(Options_With_FileDirectoryInfo options, string result)
30+
{
31+
new Parser()
32+
.FormatCommandLine(options)
33+
.Should().BeEquivalentTo(result);
34+
}
35+
2636
[Theory]
2737
[MemberData(nameof(UnParseDataVerbs))]
2838
public static void UnParsing_instance_returns_command_line_for_verbs(Add_Verb verb, string result)
@@ -298,6 +308,15 @@ public static IEnumerable<object[]> UnParseData
298308
}
299309
}
300310

311+
public static IEnumerable<object[]> UnParseFileDirectoryData
312+
{
313+
get
314+
{
315+
yield return new object[] { new Options_With_FileDirectoryInfo(), "" };
316+
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""" };
317+
}
318+
}
319+
301320

302321
public static IEnumerable<object[]> UnParseDataVerbs
303322
{

0 commit comments

Comments
 (0)