Skip to content

Unclear exception when providing Default = null for an option#352

@ptylenda

Description

@ptylenda

In 2.x version of CommandLine it is not possible to specify null as default value of an option:

[Option('u', "user", Default = null, HelpText = "User to run the schedule as")] public string User{get; set} 

You will get such an exception:

Unhandled Exception: System.Reflection.CustomAttributeFormatException: 'Default' property specified was not found. ---> System.Reflection.TargetInvocationExcept ion: Exception has been thrown by the target of an invocation. ---> System.Argum entNullException: Value cannot be null. Parameter name: value at CommandLine.BaseAttribute.set_Default(Object value) --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec t[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decora tedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFi lterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecorate dTargetSecurityTransparent) --- End of inner exception stack trace --- at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decora tedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFi lterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecorate dTargetSecurityTransparent) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimePropertyInfo property, RuntimeType caType) at System.Reflection.RuntimePropertyInfo.GetCustomAttributes(Boolean inherit) at CommandLine.Core.ReflectionExtensions.<>c__0`1.<GetSpecifications>b__0_1(P ropertyInfo pi) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelec tor, Func`2 elementSelector, IEqualityComparer`1 comparer) at System.Linq.GroupedEnumerable`3.GetEnumerator() at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate ) at CommandLine.Core.SpecificationExtensions.ThrowingValidate(IEnumerable`1 sp ecifications, IEnumerable`1 guardsLookup) at CommandLine.Core.InstanceBuilder.Build[T](Maybe`1 factory, Func`3 tokenize r, IEnumerable`1 arguments, StringComparer nameComparer, CultureInfo parsingCult ure, IEnumerable`1 nonFatalErrors) at CommandLine.Parser.ParseArguments[T](IEnumerable`1 args) 

The behaviour is perfectly fine, but it is not documented and the exception does not clearly state which option property caused the exception. Second thing is that using Default = null was possible in 1.x, so while migrating 1.x -> 2.x you will run into this unexpected exception and it is not immediately clear where to look for the root cause (especially if you have a large number of option properties)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions