Skip to content

Commit

Permalink
2.1.0 fix (#6)
Browse files Browse the repository at this point in the history
* Fixes on 2.1.0

* Minor changes
  • Loading branch information
mariomastrodicasa authored Oct 7, 2021
1 parent 7a4581b commit 9ecd7fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
33 changes: 22 additions & 11 deletions src/CLIParser/CLIParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ public static void Add(this IEnumerable<IArgumentMetadata> metadatas)
Add(item);
}
}

/// <summary>
/// Return the value from <see cref="IArgumentMetadataParsed"/> or the default value
/// </summary>
/// <typeparam name="T">The <see cref="Type"/> to use to retrieve value</typeparam>
/// <param name="arg">The <see cref="IArgumentMetadataParsed"/> to use to get value</param>
/// <returns>The value from <see cref="IArgumentMetadataParsed"/> or <see cref="IArgumentMetadata.Default"/> if value was not set</returns>
public static T Get<T>(this IArgumentMetadataParsed arg)
{
if (arg == null) throw new ArgumentNullException("arg cannot be null.");
if (!typeof(T).IsAssignableFrom(arg.DataType)) throw new ArgumentException(string.Format("{0} is incomplatible wirh {1}.", typeof(T), arg.DataType));
if (arg.Value != null)
{
return (T)arg.Value;
}
return (T)arg.Default;
}
}

/// <summary>
Expand Down Expand Up @@ -315,20 +332,14 @@ public T Get<T>(IEnumerable<IArgumentMetadataParsed> args, string name)
}

/// <summary>
/// Return the <see cref="IArgumentMetadataParsed"/> at <paramref name="name"/>
/// Return the value from <see cref="IArgumentMetadataParsed"/>
/// </summary>
/// <param name="args">An ensemble of <see cref="IArgumentMetadataParsed"/> to parse</param>
/// <param name="name">The argument name, or short name, to get</param>
/// <returns>The selected <see cref="IArgumentMetadataParsed"/></returns>
/// <typeparam name="T">The <see cref="Type"/> to use to retrieve value</typeparam>
/// <param name="arg">The <see cref="IArgumentMetadataParsed"/> to use to get value</param>
/// <returns>The value from <see cref="IArgumentMetadataParsed"/></returns>
public T Get<T>(IArgumentMetadataParsed arg)
{
if (arg == null) throw new ArgumentNullException("arg cannot be null.");
if (!typeof(T).IsAssignableFrom(arg.DataType)) throw new ArgumentException(string.Format("{0} is incomplatible wirh {1}.", typeof(T), arg.DataType));
if (arg.Value != null)
{
return (T)arg.Value;
}
return (T)arg.Default;
return arg.Get<T>();
}

/// <summary>
Expand Down
9 changes: 7 additions & 2 deletions tests/CLIParserTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ static void Main(string[] args)
Name = "multivalue",
Type = ArgumentType.Double,
IsMultiValue = true,
Default = new string[] {"a", "b"}
Default = new string[] { "a", "b" }
});
parser.Add(new ArgumentMetadata<string>(parser)
{
Name = "myval",
Type = ArgumentType.Single,
});

var result = parser.Parse(args);
var result = parser.Parse(args);

var fileInfo = parser.FromFile(result);

Expand All @@ -91,6 +91,11 @@ static void Main(string[] args)

foreach (var item in parser.Exists(noFile))
{
if (item.Name == "enum")
{
Console.WriteLine("Testing method extension: {0} is {1}", item.Name, item.Get<MyValues>());
}

if (!item.IsMultiValue)
{
Console.WriteLine("{0} is {1}", item.Name, item.Value);
Expand Down

0 comments on commit 9ecd7fe

Please sign in to comment.