Skip to content

Commit

Permalink
Fix #15
Browse files Browse the repository at this point in the history
  • Loading branch information
sakya committed Aug 21, 2022
1 parent 05dfcfc commit 83412f4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
10 changes: 5 additions & 5 deletions CmdLineArgsParser/Parser.Parse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ private void SetOptionValue(OptionProperty option, object obj, string stringValu

private void SetArrayOptionValue(OptionProperty option, object obj, object value)
{
Array array = option.Property.GetValue(obj) as Array;
var array = option.Property.GetValue(obj) as Array;
if (array == null) {
array = Array.CreateInstance(option.Property.PropertyType.GetElementType(), 1);
} else {
Type elementType = array.GetType().GetElementType();
var elementType = array.GetType().GetElementType();
if (elementType != null) {
Array newArray = Array.CreateInstance(elementType, array.Length + 1);
Array.Copy(array, newArray, Math.Min(array.Length, newArray.Length));
Expand All @@ -246,7 +246,7 @@ private void SetArrayOptionValue(OptionProperty option, object obj, object value

private void SetListOptionValue(OptionProperty option, object obj, object value)
{
IList list = option.Property.GetValue(obj) as IList;
var list = option.Property.GetValue(obj) as IList;
if (list == null) {
Type genericListType = typeof(List<>).MakeGenericType(option.Property.PropertyType.GetGenericArguments().FirstOrDefault());
list = (IList)Activator.CreateInstance(genericListType);
Expand All @@ -268,7 +268,7 @@ private void CheckValidValues(OptionProperty option, object obj, object value, L
{
// Check valid values:
if (option.Option.ValidValues?.Length > 0) {
bool valueOk = false;
var valueOk = false;
foreach (var vvs in option.Option.GetValidValues()) {
var vv = GetValueFromString(option.Property.PropertyType, vvs, out _);
if (value.Equals(vv)) {
Expand All @@ -290,7 +290,7 @@ private void CheckOnlyForVerbs(OptionProperty option, object obj, List<ParserErr
if (_verbValue == null) {
errors.Add(new ParserError(option.Option.Name, $"Option '{ option.Option.Name }' can be set only for specific verbs but no verb has been specified"));
} else {
bool valueOk = false;
var valueOk = false;
foreach (var ofvs in option.Option.GetOnlyForVerbs()) {
var ofv = GetValueFromString(_verbOption.Property.PropertyType, ofvs, out _);
if (_verbValue.Equals(ofv)) {
Expand Down
18 changes: 14 additions & 4 deletions CmdLineArgsParser/Parser.ShowUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ namespace CmdLineArgsParser
{
public partial class Parser
{
/// <summary>
/// Writes information lines
/// AssemblyTitle AssemblyVersion
/// AssemblyCopyright
/// AssemblyDescription
/// </summary>
/// <param name="includeDescription">Define if the AssemblyDescription should be printed</param>
public void ShowInfo(bool includeDescription = true)
{
ShowAssemblyInformation(Assembly.GetCallingAssembly(), includeDescription);
}

/// <summary>
/// Writes usage information tow the <see cref="Console"/>
/// </summary>
Expand All @@ -24,8 +36,6 @@ public partial class Parser
ValidateOptionsType<T>();

var cAssembly = Assembly.GetCallingAssembly();
ShowAssemblyInformation(cAssembly);

var properties = GetProperties<T>();
_verbOption = properties.FirstOrDefault(p => p.Option.Verb);

Expand All @@ -52,7 +62,7 @@ public partial class Parser

#region private operations

private void ShowAssemblyInformation(Assembly assembly)
private void ShowAssemblyInformation(Assembly assembly, bool includeDescription = false)
{
bool newline = false;
if (assembly.GetCustomAttribute(typeof(AssemblyTitleAttribute)) is AssemblyTitleAttribute ta) {
Expand All @@ -70,7 +80,7 @@ private void ShowAssemblyInformation(Assembly assembly)
newline = false;
}

if (assembly.GetCustomAttribute(typeof(AssemblyDescriptionAttribute)) is AssemblyDescriptionAttribute da) {
if (includeDescription && assembly.GetCustomAttribute(typeof(AssemblyDescriptionAttribute)) is AssemblyDescriptionAttribute da) {
Console.WriteLine(da.Description);
newline = true;
}
Expand Down
1 change: 1 addition & 0 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static void Main(string[] args)
};

var parser = new Parser(new ParserSettings());
parser.ShowInfo(true);
parser.ShowUsage<Options>();

var options = parser.Parse<Options>(testArgs, out var errors);
Expand Down
2 changes: 1 addition & 1 deletion SolutionInfo.proj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<RespositoryType>git</RespositoryType>
<PackageTags>command line commandline argument option shell parser</PackageTags>
<Description>CmdLineArgsParser helps you parse command line arguments.</Description>
<Version>0.7.1.0</Version>
<Version>0.7.2.0</Version>
</PropertyGroup>
</Project>
10 changes: 5 additions & 5 deletions Tests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ protected void CheckPropertyValue(string propertyName, object res, object value)

var objValue = property?.GetValue(res);
if (property?.PropertyType.IsArray == true) {
Array? array = objValue as Array;
Array? valueArray = value as Array;
var array = objValue as Array;
var valueArray = value as Array;
if (valueArray == null)
Assert.Fail($"Wrong value for comparison (not an array)");

if (valueArray?.Length != array?.Length)
Assert.Fail($"Property {propertyName} expected value was {value}, got {objValue}");
for (int i = 0; i < array.Length; i++) {
for (var i = 0; i < array.Length; i++) {
if (!array.GetValue(i).Equals(valueArray.GetValue(i)))
Assert.Fail($"Property {propertyName} expected value was {value}, got {objValue}");
}
} else if (property?.PropertyType.IsGenericType == true && typeof(List<>).IsAssignableFrom(property.PropertyType.GetGenericTypeDefinition())) {
IList? list = objValue as IList;
IList? valueList = value as IList;
var list = objValue as IList;
var valueList = value as IList;
if (valueList == null)
Assert.Fail($"Wrong value for comparison (not an array)");

Expand Down

0 comments on commit 83412f4

Please sign in to comment.