Skip to content

Commit

Permalink
Fix issues with ArgumentParser
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Nov 22, 2017
1 parent 1f637ec commit 891073b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
49 changes: 31 additions & 18 deletions src/Unosquare.Swan/Components/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ public bool ParseArguments<T>(IEnumerable<string> args, T instance)

if (instance == null)
throw new ArgumentNullException(nameof(instance));

var properties = GetTypeProperties(typeof(T)).ToArray();
var verbName = string.Empty;
if (properties.Any(x => x.GetCustomAttributes(typeof(VerbOptionAttribute),false).Count() > 0))

if (properties.Any(x => x.GetCustomAttributes(typeof(VerbOptionAttribute), false).Any()))
{
var selectedVerb = properties.FirstOrDefault(x => x.GetCustomAttribute<VerbOptionAttribute>().Name.Equals(args.ToArray()[0]));
var selectedVerb = properties.FirstOrDefault(x =>
x.GetCustomAttribute<VerbOptionAttribute>().Name.Equals(args.ToArray()[0]));
if (selectedVerb == null)
{
"No verb was specified".WriteLine();
Expand All @@ -82,7 +84,7 @@ public bool ParseArguments<T>(IEnumerable<string> args, T instance)
var propertyInstance = Activator.CreateInstance(selectedVerb.PropertyType);
instance.GetType().GetProperty(verbName).SetValue(instance, propertyInstance);
}

properties = GetTypeProperties(selectedVerb.PropertyType).ToArray();
}

Expand All @@ -107,7 +109,7 @@ public bool ParseArguments<T>(IEnumerable<string> args, T instance)
propertyName = string.Empty;
continue;
}

if (SetPropertyValue(targetProperty, arg, instance))
updatedList.Add(targetProperty);

Expand All @@ -130,11 +132,12 @@ public bool ParseArguments<T>(IEnumerable<string> args, T instance)
if (SetPropertyValue(targetProperty, true.ToString(), instance))
updatedList.Add(targetProperty);
}
else
else
{
var property = instance.GetType().GetProperty(verbName);
instance.GetType().GetProperty(verbName);
if (SetPropertyValue(targetProperty, true.ToString(),property.GetValue(instance,null)))

if (SetPropertyValue(targetProperty, true.ToString(), property.GetValue(instance, null)))
updatedList.Add(targetProperty);
}

Expand All @@ -153,6 +156,7 @@ public bool ParseArguments<T>(IEnumerable<string> args, T instance)

if (defaultValue == null)
continue;

if (string.IsNullOrEmpty(verbName))
{
SetPropertyValue(targetProperty, defaultValue.ToString(), instance);
Expand All @@ -172,9 +176,21 @@ public bool ParseArguments<T>(IEnumerable<string> args, T instance)
if (optionAttr == null || optionAttr.Required == false)
continue;

if (targetProperty.GetValue(instance) == null)
if (string.IsNullOrWhiteSpace(verbName))
{
requiredList.Add(optionAttr.LongName ?? optionAttr.ShortName);
if (targetProperty.GetValue(instance) == null)
{
requiredList.Add(optionAttr.LongName ?? optionAttr.ShortName);
}
}
else
{
var property = instance.GetType().GetProperty(verbName);

if (targetProperty.GetValue(property.GetValue(instance)) == null)
{
requiredList.Add(optionAttr.LongName ?? optionAttr.ShortName);
}
}
}

Expand All @@ -188,10 +204,10 @@ public bool ParseArguments<T>(IEnumerable<string> args, T instance)
WriteUsage(properties);

if (unknownList.Any())
$"Unknown arguments: {string.Join(", ", unknownList)}".WriteLine();
$"Unknown arguments: {string.Join(", ", unknownList)}".WriteLine(ConsoleColor.Red);

if (requiredList.Any())
$"Required arguments: {string.Join(", ", requiredList)}".WriteLine();
$"Required arguments: {string.Join(", ", requiredList)}".WriteLine(ConsoleColor.Red);

return false;
}
Expand Down Expand Up @@ -263,14 +279,11 @@ private bool SetPropertyValue<T>(PropertyInfo targetProperty, string propertyVal
return true;
}

if (targetProperty.PropertyType.TryParseBasicType(propertyValueString,
out var propertyValue))
{
targetProperty.SetValue(result, propertyValue);
return true;
}
if (!targetProperty.PropertyType.TryParseBasicType(propertyValueString, out var propertyValue))
return false;

return false;
targetProperty.SetValue(result, propertyValue);
return true;
}

private PropertyInfo TryGetProperty(IEnumerable<PropertyInfo> properties, string propertyName)
Expand Down
2 changes: 1 addition & 1 deletion src/Unosquare.Swan/Unosquare.Swan.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageId>Unosquare.Swan</PackageId>
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
<DebugType>Full</DebugType>
<Version>0.19.0</Version>
<Version>0.20.0</Version>
<Authors>Unosquare</Authors>
<PackageIconUrl>https://github.com/unosquare/swan/raw/master/swan-logo-32.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/unosquare/swan</PackageProjectUrl>
Expand Down

0 comments on commit 891073b

Please sign in to comment.