Skip to content

Commit 8fbcc33

Browse files
committed
Fix unparsing FileInfo and DirectoryInfo
This treats FileInfo and DirectoryInfo as special as e.g. DateTime. Unparsing a FileInfo or DirectoryInfo works now with a path that contains spaces. Fixes #626
1 parent 24e2be2 commit 8fbcc33

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/CommandLine/UnParserExtensions.cs

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

33
using System;
44
using System.Collections;
5+
using System.IO;
56
using System.Linq;
67
using System.Text;
78
using CommandLine.Core;
@@ -204,7 +205,7 @@ private static string FormatValue(Specification spec, object value)
204205

205206
private static object FormatWithQuotesIfString(object value)
206207
{
207-
if (value is DateTime || value is DateTimeOffset) return $"\"{value}\"";
208+
if (value is DateTime || value is DateTimeOffset || value is FileInfo || value is DirectoryInfo) return $"\"{value}\"";
208209
Func<string, string> doubQt = v
209210
=> v.Contains("\"") ? v.Replace("\"", "\\\"") : v;
210211

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)