Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
🐛 Fix improper handling of default values (not accepting objects)
Browse files Browse the repository at this point in the history
* 🐛 Fix NRE thrown by null value sequences
* Add preprocessor to non-generic argument types
* Change signature of ValueConverter.ConvertValue
  • Loading branch information
louistakepillz committed May 25, 2015
1 parent ae05b8d commit 2ad9942
Show file tree
Hide file tree
Showing 29 changed files with 121 additions and 56 deletions.
5 changes: 3 additions & 2 deletions ArgumentParser/Arguments/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ protected Argument() { }
/// <param name="key">The unique identifier to use to represent the argument.</param>
/// <param name="description">The description of the argument.</param>
/// <param name="valueOptions">The value parsing behavior of the argument.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
protected Argument(Key key, String description = null, ValueOptions valueOptions = ValueOptions.Single, String defaultValue = null)
: base(key, description, valueOptions, defaultValue: defaultValue) { }
protected Argument(Key key, String description = null, ValueOptions valueOptions = ValueOptions.Single, PreprocessorDelegate preprocessor = null, String defaultValue = null)
: base(key, description, valueOptions, preprocessor: preprocessor, defaultValue: defaultValue) { }
}
}
49 changes: 44 additions & 5 deletions ArgumentParser/Arguments/Argument`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected Argument() { }
/// <param name="typeConverter">The type converter to use for conversion.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
protected Argument(Key key, String description = null, ValueOptions valueOptions = ValueOptions.Single, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, T defaultValue = default (T))
protected Argument(Key key, String description = null, ValueOptions valueOptions = ValueOptions.Single, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, Object defaultValue = null)
{
this.Key = key;
this.Description = description;
Expand Down Expand Up @@ -73,7 +73,7 @@ protected Argument() { }
/// <summary>
/// Gets the default value of the argument.
/// </summary>
public T DefaultValue { get; private set; }
public Object DefaultValue { get; private set; }

/// <summary>
/// Gets the value type of the argument.
Expand All @@ -93,9 +93,48 @@ protected Argument() { }
/// <summary>
/// Gets the default value of the argument.
/// </summary>
Object IArgument.DefaultValue
T IArgument<T>.DefaultValue
{
get { return this.DefaultValue; }
get
{
return this.DefaultValue is T
? (T) this.DefaultValue
: (T) ValueConverter.GetDefaultValue(this.Type, this.TypeConverter, this.DefaultValue);
}
}

/// <summary>
/// Gets the default value of the argument.
/// </summary>
/// <param name="value">The default value.</param>
/// <returns>A boolean value indicating whether the conversion succeeded.</returns>
Boolean IArgument.TryGetDefaultValue(out Object value)
{
T defaultValue;
bool returnValue = this.TryGetDefaultValue(out defaultValue);
value = defaultValue;

return returnValue;
}

/// <summary>
/// Gets the default value of the argument.
/// </summary>
/// <param name="value">The default value.</param>
/// <returns>A boolean value indicating whether the conversion succeeded.</returns>
public virtual Boolean TryGetDefaultValue(out T value)
{
try
{
value = ((IArgument<T>) this).DefaultValue;
}
catch
{
value = default (T);
return false;
}

return true;
}

/// <summary>
Expand Down Expand Up @@ -142,7 +181,7 @@ public virtual ParameterPair GetPair(IEnumerable<RawParameter> parameters, Prepr
return new ParameterPair(this, new Object[0]);
default:
var canonicalValues = parameters.ToDictionary(x => x, x => ValueConverter.GetCompositeValueParts(x, this.Preprocessor ?? preprocessor, culture));
trailingValues = canonicalValues.Select(x => x.Value.Any() ? x.Value.Skip(1) : x.Value);
trailingValues = canonicalValues.Select(x => x.Value != null && x.Value.Any() ? x.Value.Skip(1) : x.Value);
return new ParameterPair(
argument: this,
values: this.GetValues(canonicalValues.Select(x =>
Expand Down
2 changes: 1 addition & 1 deletion ArgumentParser/Arguments/FlagArgument`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected FlagArgument() { }
/// <param name="typeConverter">The type converter to use for conversion.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
protected FlagArgument(Key key, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, T defaultValue = default(T))
protected FlagArgument(Key key, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, Object defaultValue = null)
: base(key, description, valueOptions, typeConverter, preprocessor, defaultValue)
{
this.FlagOptions = flagOptions;
Expand Down
7 changes: 7 additions & 0 deletions ArgumentParser/Arguments/IArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public interface IArgument : IPairable
/// </summary>
ValueOptions ValueOptions { get; }

/// <summary>
/// Gets the default value of the argument.
/// </summary>
/// <param name="value">The default value.</param>
/// <returns>A boolean value indicating whether the conversion succeeded.</returns>
Boolean TryGetDefaultValue(out Object value);

/// <summary>
/// Converts a sequence of values to the type of the argument using the specified format.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion ArgumentParser/Arguments/IArgument`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ namespace ArgumentParser.Arguments
/// Represents an argument definition of a defined value type.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
public interface IArgument<out T> : IArgument
public interface IArgument<T> : IArgument
{
/// <summary>
/// Gets the default value of the argument.
/// </summary>
new T DefaultValue { get; }

/// <summary>
/// Gets the default value of the argument.
/// </summary>
/// <param name="value">The default value.</param>
/// <returns>A boolean value indicating whether the conversion succeeded.</returns>
Boolean TryGetDefaultValue(out T value);

/// <summary>
/// Converts a sequence of values to the type of the argument using the specified <see cref="T:System.Globalization.CultureInfo"/>.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions ArgumentParser/Arguments/POSIX/POSIXLongArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class POSIXLongArgument : Argument
/// <param name="tag">The tag that defines the argument.</param>
/// <param name="description">The description of the argument.</param>
/// <param name="valueOptions">The value parsing behavior of the argument.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public POSIXLongArgument(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, String defaultValue = null)
: base(new Key(Prefix, tag), description, valueOptions, defaultValue) { }
public POSIXLongArgument(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, PreprocessorDelegate preprocessor = null, String defaultValue = null)
: base(new Key(Prefix, tag), description, valueOptions, preprocessor, defaultValue) { }

/// <summary>
/// Gets the prefix used for arguments of the <see cref="T:ArgumentParser.Arguments.POSIX.POSIXLongArgument"/> type.
Expand Down
2 changes: 1 addition & 1 deletion ArgumentParser/Arguments/POSIX/POSIXLongArgument`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class POSIXLongArgument<T> : Argument<T>
/// <param name="typeConverter">The type converter to use for conversion.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public POSIXLongArgument(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, T defaultValue = default (T))
public POSIXLongArgument(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, Object defaultValue = null)
: base(new Key(Prefix, tag), description, valueOptions, typeConverter, preprocessor, defaultValue) { }

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions ArgumentParser/Arguments/POSIX/POSIXLongFlag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public class POSIXLongFlag : FlagArgument
/// <param name="description">The description of the argument.</param>
/// <param name="valueOptions">The value parsing behavior of the argument.</param>
/// <param name="flagOptions">The value conversion behavior.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public POSIXLongFlag(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, Int32 defaultValue = default (Int32))
: base(new Key(Prefix, tag), description, valueOptions, flagOptions, defaultValue: defaultValue) { }
public POSIXLongFlag(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, PreprocessorDelegate preprocessor = null, Int32 defaultValue = default (Int32))
: base(new Key(Prefix, tag), description, valueOptions, flagOptions, preprocessor: preprocessor, defaultValue: defaultValue) { }

/// <summary>
/// Gets the prefix used for arguments of the <see cref="T:ArgumentParser.Arguments.POSIX.POSIXLongFlag"/> type.
Expand Down
2 changes: 1 addition & 1 deletion ArgumentParser/Arguments/POSIX/POSIXLongFlag`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class POSIXLongFlag<T> : FlagArgument<T>
/// <param name="typeConverter">The type converter to use for value conversion.</param>
/// <param name="preprocessor">The delegate to use for preprocessor.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public POSIXLongFlag(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, T defaultValue = default (T))
public POSIXLongFlag(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, Object defaultValue = null)
: base(new Key(Prefix, tag), description, valueOptions, flagOptions, typeConverter, preprocessor, defaultValue) { }

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions ArgumentParser/Arguments/POSIX/POSIXShortArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class POSIXShortArgument : Argument
/// <param name="tag">The character that defines the argument.</param>
/// <param name="description">The description of the argument.</param>
/// <param name="valueOptions">The value parsing behavior of the argument.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public POSIXShortArgument(Char tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, String defaultValue = null)
: base(new Key(Prefix, tag.ToString()), description, valueOptions, defaultValue) { }
public POSIXShortArgument(Char tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, PreprocessorDelegate preprocessor = null, String defaultValue = null)
: base(new Key(Prefix, tag.ToString()), description, valueOptions, preprocessor, defaultValue) { }

/// <summary>
/// Gets the prefix used for arguments of the <see cref="T:ArgumentParser.Arguments.POSIX.POSIXShortArgument"/> type.
Expand Down
2 changes: 1 addition & 1 deletion ArgumentParser/Arguments/POSIX/POSIXShortArgument`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class POSIXShortArgument<T> : Argument<T>
/// <param name="typeConverter">The type converter to use for value conversion.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public POSIXShortArgument(Char tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, T defaultValue = default (T))
public POSIXShortArgument(Char tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, Object defaultValue = null)
: base(new Key(Prefix, tag.ToString()), description, valueOptions, typeConverter, preprocessor, defaultValue) { }

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions ArgumentParser/Arguments/POSIX/POSIXShortFlag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public class POSIXShortFlag : FlagArgument
/// <param name="description">The description of the argument.</param>
/// <param name="valueOptions">The value parsing behavior of the argument.</param>
/// <param name="flagOptions">The value conversion behavior.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public POSIXShortFlag(Char tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, Int32 defaultValue = default (Int32))
: base(new Key(Prefix, tag.ToString()), description, valueOptions, flagOptions, defaultValue: defaultValue) { }
public POSIXShortFlag(Char tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, PreprocessorDelegate preprocessor = null, Int32 defaultValue = default (Int32))
: base(new Key(Prefix, tag.ToString()), description, valueOptions, flagOptions, preprocessor: preprocessor, defaultValue: defaultValue) { }

/// <summary>
/// Gets the prefix used for arguments of the <see cref="T:ArgumentParser.Arguments.POSIX.POSIXShortFlag"/> type.
Expand Down
2 changes: 1 addition & 1 deletion ArgumentParser/Arguments/POSIX/POSIXShortFlag`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class POSIXShortFlag<T> : FlagArgument<T>
/// <param name="typeConverter">The type converter to use for value conversion.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public POSIXShortFlag(Char tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, T defaultValue = default (T))
public POSIXShortFlag(Char tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, Object defaultValue = null)
: base(new Key(Prefix, tag.ToString()), description, valueOptions, flagOptions, typeConverter, preprocessor, defaultValue) { }

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions ArgumentParser/Arguments/PowerShell/PowerShellArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class PowerShellArgument : Argument
/// <param name="tag">The tag that defines the argument.</param>
/// <param name="description">The description of the argument.</param>
/// <param name="valueOptions">The value parsing behavior of the argument.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public PowerShellArgument(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, String defaultValue = null)
: base(new Key(Prefix, tag), description, valueOptions, defaultValue) { }
public PowerShellArgument(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, PreprocessorDelegate preprocessor = null, String defaultValue = null)
: base(new Key(Prefix, tag), description, valueOptions, preprocessor, defaultValue) { }

/// <summary>
/// Gets the prefix used for arguments of the <see cref="T:ArgumentParser.Arguments.PowerShell.PowerShellArgument"/> type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class PowerShellArgument<T> : Argument<T>
/// <param name="typeConverter">The type converter to use for value conversion.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public PowerShellArgument(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, T defaultValue = default (T))
public PowerShellArgument(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, TypeConverter typeConverter = null, PreprocessorDelegate preprocessor = null, Object defaultValue = null)
: base(new Key(Prefix, tag), description, valueOptions, typeConverter, preprocessor, defaultValue) { }

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions ArgumentParser/Arguments/PowerShell/PowerShellFlag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public class PowerShellFlag : FlagArgument
/// <param name="description">The description of the argument.</param>
/// <param name="valueOptions">The value parsing behavior of the argument.</param>
/// <param name="flagOptions">The value conversion behavior.</param>
/// <param name="preprocessor">The delegate to use for preprocessing.</param>
/// <param name="defaultValue">The default value of the argument.</param>
public PowerShellFlag(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, Int32 defaultValue = default (Int32))
: base(new Key(Prefix, tag), description, valueOptions, flagOptions, defaultValue: defaultValue) { }
public PowerShellFlag(String tag, String description = null, ValueOptions valueOptions = ValueOptions.Single, FlagOptions flagOptions = FlagOptions.None, PreprocessorDelegate preprocessor = null, Int32 defaultValue = default (Int32))
: base(new Key(Prefix, tag), description, valueOptions, flagOptions, preprocessor: preprocessor, defaultValue: defaultValue) { }

/// <summary>
/// Gets the prefix used for arguments of the <see cref="T:ArgumentParser.Arguments.PowerShell.PowerShellFlag"/> type.
Expand Down
Loading

0 comments on commit 2ad9942

Please sign in to comment.