Skip to content

Commit

Permalink
Add ParseArg method to split commandline
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-hassan committed Jul 4, 2020
1 parent 000b156 commit cccae2d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/CommandLine/UnParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommandLine.Core;
Expand Down Expand Up @@ -102,6 +103,18 @@ public static string FormatCommandLine<T>(this Parser parser, T options)
return parser.FormatCommandLine(options, config => { });
}

/// <summary>
/// Format a command line argument string from a parsed instance in the form of string[].
/// </summary>
/// <typeparam name="T">Type of <paramref name="options"/>.</typeparam>
/// <param name="parser">Parser instance.</param>
/// <param name="options">A parsed (or manually correctly constructed instance).</param>
/// <returns>A string[] with command line arguments.</returns>
public static string[] FormatCommandLineArgs<T>(this Parser parser, T options)
{
return parser.FormatCommandLine(options, config => { }).SplitArgs();
}

/// <summary>
/// Format a command line argument string from a parsed instance.
/// </summary>
Expand Down Expand Up @@ -180,7 +193,19 @@ orderby v.Index
return builder
.ToString().TrimEnd(' ');
}

/// <summary>
/// Format a command line argument string[] from a parsed instance.
/// </summary>
/// <typeparam name="T">Type of <paramref name="options"/>.</typeparam>
/// <param name="parser">Parser instance.</param>
/// <param name="options">A parsed (or manually correctly constructed instance).</param>
/// <param name="configuration">The <see cref="Action{UnParserSettings}"/> lambda used to configure
/// aspects and behaviors of the unparsersing process.</param>
/// <returns>A string[] with command line arguments.</returns>
public static string[] FormatCommandLineArgs<T>(this Parser parser, T options, Action<UnParserSettings> configuration)
{
return FormatCommandLine<T>(parser, options, configuration).SplitArgs();
}
private static string FormatValue(Specification spec, object value)
{
var builder = new StringBuilder();
Expand Down Expand Up @@ -273,5 +298,35 @@ private static bool IsEmpty(this object value, Specification specification, bool
if (value is IEnumerable && !((IEnumerable)value).GetEnumerator().MoveNext()) return true;
return false;
}


#region splitter
/// <summary>
/// Returns a string array that contains the substrings in this instance that are delimited by space considering string between double quote.
/// </summary>
/// <param name="command">the commandline string</param>
/// <param name="keepQuote">don't remove the quote</param>
/// <returns>a string array that contains the substrings in this instance</returns>
public static string[] SplitArgs(this string command, bool keepQuote = false)
{
if (string.IsNullOrEmpty(command))
return new string[0];

var inQuote = false;
var chars = command.ToCharArray().Select(v =>
{
if (v == '"')
inQuote = !inQuote;
return !inQuote && v == ' ' ? '\n' : v;
}).ToArray();

return new string(chars).Split('\n')
.Select(x => keepQuote ? x : x.Trim('"'))
.Where(x => !string.IsNullOrWhiteSpace(x))
.ToArray();
}

#endregion

}
}
24 changes: 24 additions & 0 deletions tests/CommandLine.Tests/Unit/UnParserExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,30 @@ public static void UnParsing_instance_with_nullable_bool(bool? flag, string expe
.FormatCommandLine(options)
.Should().BeEquivalentTo(expected);
}
#region SplitArgs
[Theory]
[InlineData("--shape Circle", new[] { "--shape","Circle" })]
[InlineData(" --shape Circle ", new[] { "--shape", "Circle" })]
[InlineData("-a --shape Circle", new[] {"-a", "--shape", "Circle" })]
[InlineData("-a --shape Circle -- -x1 -x2", new[] { "-a", "--shape", "Circle","--","-x1","-x2" })]
[InlineData("--name \"name with space and quote\" -x1", new[] { "--name", "name with space and quote","-x1" })]
public static void Split_arguments(string command, string[] expectedArgs)
{
var args = command.SplitArgs();
args.Should().BeEquivalentTo(expectedArgs);
}
[Theory]
[InlineData("--shape Circle", new[] { "--shape", "Circle" })]
[InlineData(" --shape Circle ", new[] { "--shape", "Circle" })]
[InlineData("-a --shape Circle", new[] { "-a", "--shape", "Circle" })]
[InlineData("-a --shape Circle -- -x1 -x2", new[] { "-a", "--shape", "Circle", "--", "-x1", "-x2" })]
[InlineData("--name \"name with space and quote\" -x1", new[] { "--name", "\"name with space and quote\"", "-x1" })]
public static void Split_arguments_with_keep_quote(string command, string[] expectedArgs)
{
var args = command.SplitArgs(true);
args.Should().BeEquivalentTo(expectedArgs);
}
#endregion
class Option_Int_Nullable
{
[Option('v', Default = 1)]
Expand Down

0 comments on commit cccae2d

Please sign in to comment.