9

I'm using the Command Line Parser Library I obtained via NuGet in my C# Console Application, .NET Framework 4.0.

Here's my options class...

class Options
{
    [Option('p', "prompt", DefaultValue = true, HelpText = "Prompt the user before exiting the program.")]
    public bool PromptForExit { get; set; }

    [HelpOption]
    public string GetUsage()
    {
        return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
    }
}

Here's where I parse and use the options...

static void Main(string[] args)
{
    Options options = new Options();
    if (CommandLine.Parser.Default.ParseArguments(args, options))
    {
        if (options.PromptForExit)
        {
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

I've tried all sorts of commands in an effort to get it to not prompt me before exiting, but none of them work. Is anyone familiar with this library or have an idea as to how I can get the PromptForExit option to be false from the command line?

Here's what I've tried.

myprogram.exe
myprogram.exe -p false
myprogram.exe -p False
myprogram.exe -p FALSE
myprogram.exe -p 0
myprogram.exe --prompt false
myprogram.exe --prompt False
myprogram.exe --prompt FALSE
myprogram.exe --prompt 0
mason
  • 31,774
  • 10
  • 77
  • 121

3 Answers3

11

If you look at this, you'll see that

Bool options are true if they are present and false if they are not.

So just do

myprogram.exe

and PromptForExit should be false.

EDIT

In your case : negate your property

[Option('p', "noprompt", DefaultValue = false, HelpText = "Don't prompt the user before exiting the program.")]
public bool NoPromptForExit { get; set; }
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • I'll add that to the list of things I've tried. However, that doesn't appear to work when you set the `DefaultValue` to `true` in the `OptionAttribute`. – mason Apr 02 '14 at 15:07
  • 1
    For that case : https://commandline.codeplex.com/discussions/256995 : `As for your last question about how to set an option to true by default: Negate its name.` – Raphaël Althaus Apr 02 '14 at 15:10
  • See edit (it's a bit ugly, i know, and the "char" version is not that clear) – Raphaël Althaus Apr 02 '14 at 15:13
  • Wow this is dumb. - false should definitely result in false. Just fixed this in my code... yeesh – Ed S. Oct 12 '16 at 23:14
2

Apparently the library doesn't properly support Booleans with a DefaultValue of True. So I modified my program as such...

class Options
{
    [Option('p', "do-not-prompt", DefaultValue = false, HelpText = "Do not prompt the user before exiting the program.")]
    public bool DoNotPromptForExit { get; set; }

    [HelpOption]
    public string GetUsage()
    {
        return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
    }
}


static void Main(string[] args)
{
    Options options = new Options();
    if (CommandLine.Parser.Default.ParseArguments(args, options))
    {
        if (!options.DoNotPromptForExit)
        {
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

I think this is an uglier solution, so if someone comes along with a better one then I'll accept it.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Maybe you can change bool to string and in the get you can parse it safely. Say if you want to default to false, then if the text can be parsed to true, then true, else it's false (null, empty, any other scenario). If you want true to be default, same game for null,empty etc being considered as true and if the value can be parsed as false, then return false. – Drusantia May 28 '17 at 16:04
1

It's too bad the Boolean approach doesn't work as it should. I used a YesNo-enum as workaround. Hope it will help.

class Options
{
    public enum YesNo
    {
       Yes,
       No
    }

    [Option('p', "prompt", DefaultValue = YesNo.Yes, HelpText = "Prompt the user before exiting the program. (Yes/No)")]
    public YesNo PromptForExit { get; set; }
}
Michiel
  • 36
  • 2