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
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; }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With