adamabdelhamed / PowerArgs

The ultimate .NET Standard command line argument parser
MIT License
568 stars 56 forks source link

String args with 'null' as default throw unhandled exception #151

Closed bc3tech closed 2 years ago

bc3tech commented 3 years ago

v3.6.0

Consider following args class

        [TabCompletion]
        public class ProgramArgs
        {
            [HelpHook, ArgShortcut("?"), ArgShortcut("h"), ArgDescription("Shows help")]
            public bool Help { get; set; }

            [ArgShortcut("in"), ArgDescription(@"The directory containing the files to process"), ArgDefaultValue(@"")]
            public string InputDirectory { get; set; }
        }

Running the containing program without specifying the argument will result in

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at PowerArgs.DefaultValueAttribute.BeforePopulateProperty(HookContext Context)
   at PowerArgs.CommandLineArgument.<>c__DisplayClass76_0.<RunBeforePopulateProperty>b__1(ArgHook h)
   at PowerArgs.CommandLineArgument.RunArgumentHook(HookContext context, Func`2 orderby, Action`1 hookAction)
   at PowerArgs.CommandLineArgument.RunBeforePopulateProperty(HookContext context)
   at PowerArgs.CommandLineArgument.Populate(HookContext context)
   at PowerArgs.CommandLineArgument.PopulateArguments(List`1 arguments, HookContext context)
   at PowerArgs.Args.ParseInternal(CommandLineArgumentsDefinition definition, String[] input)
   at PowerArgs.Args.<>c__DisplayClass13_0.<ParseAction>b__0()
   at PowerArgs.Args.Execute[T](Func`1 argsProcessingCode)
   at PowerArgs.Args.ParseAction(CommandLineArgumentsDefinition definition, String[] args)
   at PowerArgs.Args.ParseAction(Type t, String[] args)
   at PowerArgs.Args.Parse(Type t, String[] args)
   at PowerArgs.Args.Parse[T](String[] args)
   at Program.Main(String[] args)

The app shouldn't except out; it should set null as the value on the argument and let the code process accordingly.

(Found while debugging #150 )

adamabdelhamed commented 2 years ago

I ran the program below without providing an option for InputDirectory and got this output: InputDirectory was ''

So I think it works. Re-open if you can provide a repro.

using PowerArgs;
public class Program
{
    public static void Main(string[] args)
    {
        var parsed = Args.Parse<ProgramArgs>(args);
        Console.WriteLine($"InputDirectory was '{parsed.InputDirectory}'");
    }
}

[TabCompletion]
public class ProgramArgs
{
    [HelpHook, ArgShortcut("?"), ArgShortcut("h"), ArgDescription("Shows help")]
    public bool Help { get; set; }

    [ArgShortcut("in"), ArgDescription(@"The directory containing the files to process"), ArgDefaultValue(@"")]
    public string InputDirectory { get; set; }
}