adamabdelhamed / PowerArgs

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

Boolean args as 'flags'? #150

Closed bc3tech closed 3 years ago

bc3tech commented 3 years ago

v3.6.0

I'd love for my console app to be able to handle -whatif as an argument (and only that), but it seems as though I must do -whatif true in order for this to actually work.

My Args class is set up as follows:

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

...

            [ArgShortcut("whatif"), ArgDescription(@"Don't actually move files, just show what would happen if we were to move them"), ArgDefaultValue(false)]
            public bool NoOp { get; set; }
        }

But if I run the program with just -whatif I get:

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 FileSorter.Program.Main(String[] args)

(side note: would be great if PowerArgs would call out which arg is causing the problem :) ) (other side note: does my args class have to be public to get tab completion to work properly?)

Is there a way I can get the behavior I'm after? Thanks!

bc3tech commented 3 years ago

Turns out my error was because I had string args defined with default values of null - will file a bug.