|
2 | 2 |
|
3 | 3 | using System;
|
4 | 4 | using System.Collections;
|
| 5 | +using System.Collections.Generic; |
5 | 6 | using System.Linq;
|
6 | 7 | using System.Text;
|
7 | 8 | using CommandLine.Core;
|
@@ -102,6 +103,18 @@ public static string FormatCommandLine<T>(this Parser parser, T options)
|
102 | 103 | return parser.FormatCommandLine(options, config => { });
|
103 | 104 | }
|
104 | 105 |
|
| 106 | + /// <summary> |
| 107 | + /// Format a command line argument string from a parsed instance in the form of string[]. |
| 108 | + /// </summary> |
| 109 | + /// <typeparam name="T">Type of <paramref name="options"/>.</typeparam> |
| 110 | + /// <param name="parser">Parser instance.</param> |
| 111 | + /// <param name="options">A parsed (or manually correctly constructed instance).</param> |
| 112 | + /// <returns>A string[] with command line arguments.</returns> |
| 113 | + public static string[] FormatCommandLineArgs<T>(this Parser parser, T options) |
| 114 | + { |
| 115 | + return parser.FormatCommandLine(options, config => { }).SplitArgs(); |
| 116 | + } |
| 117 | + |
105 | 118 | /// <summary>
|
106 | 119 | /// Format a command line argument string from a parsed instance.
|
107 | 120 | /// </summary>
|
@@ -180,7 +193,19 @@ orderby v.Index
|
180 | 193 | return builder
|
181 | 194 | .ToString().TrimEnd(' ');
|
182 | 195 | }
|
183 |
| - |
| 196 | + /// <summary> |
| 197 | + /// Format a command line argument string[] from a parsed instance. |
| 198 | + /// </summary> |
| 199 | + /// <typeparam name="T">Type of <paramref name="options"/>.</typeparam> |
| 200 | + /// <param name="parser">Parser instance.</param> |
| 201 | + /// <param name="options">A parsed (or manually correctly constructed instance).</param> |
| 202 | + /// <param name="configuration">The <see cref="Action{UnParserSettings}"/> lambda used to configure |
| 203 | + /// aspects and behaviors of the unparsersing process.</param> |
| 204 | + /// <returns>A string[] with command line arguments.</returns> |
| 205 | + public static string[] FormatCommandLineArgs<T>(this Parser parser, T options, Action<UnParserSettings> configuration) |
| 206 | + { |
| 207 | + return FormatCommandLine<T>(parser, options, configuration).SplitArgs(); |
| 208 | + } |
184 | 209 | private static string FormatValue(Specification spec, object value)
|
185 | 210 | {
|
186 | 211 | var builder = new StringBuilder();
|
@@ -273,5 +298,35 @@ private static bool IsEmpty(this object value, Specification specification, bool
|
273 | 298 | if (value is IEnumerable && !((IEnumerable)value).GetEnumerator().MoveNext()) return true;
|
274 | 299 | return false;
|
275 | 300 | }
|
| 301 | + |
| 302 | + |
| 303 | + #region splitter |
| 304 | + /// <summary> |
| 305 | + /// Returns a string array that contains the substrings in this instance that are delimited by space considering string between double quote. |
| 306 | + /// </summary> |
| 307 | + /// <param name="command">the commandline string</param> |
| 308 | + /// <param name="keepQuote">don't remove the quote</param> |
| 309 | + /// <returns>a string array that contains the substrings in this instance</returns> |
| 310 | + public static string[] SplitArgs(this string command, bool keepQuote = false) |
| 311 | + { |
| 312 | + if (string.IsNullOrEmpty(command)) |
| 313 | + return new string[0]; |
| 314 | + |
| 315 | + var inQuote = false; |
| 316 | + var chars = command.ToCharArray().Select(v => |
| 317 | + { |
| 318 | + if (v == '"') |
| 319 | + inQuote = !inQuote; |
| 320 | + return !inQuote && v == ' ' ? '\n' : v; |
| 321 | + }).ToArray(); |
| 322 | + |
| 323 | + return new string(chars).Split('\n') |
| 324 | + .Select(x => keepQuote ? x : x.Trim('"')) |
| 325 | + .Where(x => !string.IsNullOrWhiteSpace(x)) |
| 326 | + .ToArray(); |
| 327 | + } |
| 328 | + |
| 329 | + #endregion |
| 330 | + |
276 | 331 | }
|
277 | 332 | }
|
0 commit comments