Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set command line option to false

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
like image 994
mason Avatar asked Apr 02 '14 14:04

mason


2 Answers

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; }
like image 185
Raphaël Althaus Avatar answered Oct 13 '22 02:10

Raphaël Althaus


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.

like image 24
mason Avatar answered Oct 13 '22 02:10

mason