Skip to content

Commit eeb3ee4

Browse files
committed
Change FormatWithQuotesIfString to accept any string with spaces (PR feedback)
Feedback via issue comment: #626 (comment)
1 parent 8fbcc33 commit eeb3ee4

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/CommandLine/UnParserExtensions.cs

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

33
using System;
44
using System.Collections;
5-
using System.IO;
65
using System.Linq;
76
using System.Text;
87
using CommandLine.Core;
@@ -205,14 +204,16 @@ private static string FormatValue(Specification spec, object value)
205204

206205
private static object FormatWithQuotesIfString(object value)
207206
{
208-
if (value is DateTime || value is DateTimeOffset || value is FileInfo || value is DirectoryInfo) return $"\"{value}\"";
207+
string s = value.ToString();
208+
if (!string.IsNullOrEmpty(s) && !s.Contains("\"") && s.Contains(" "))
209+
return $"\"{s}\"";
210+
209211
Func<string, string> doubQt = v
210212
=> v.Contains("\"") ? v.Replace("\"", "\\\"") : v;
211213

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

218219
private static char SeperatorOrSpace(this Specification spec)

0 commit comments

Comments
 (0)