Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set default command line option value at runtime

I am using CommandLine Parser Library to parse command line arguments within an application.

There are some options that will in most cases be the same every time a user runs the application. Usually, I use the DefaultValue attribute so that if the user does not provide a value a default one will be used.

[Option('a', "address", DefaultValue = "http://me.com", Required = false, HelpText = "Address of server.")]
public string Address{ get; set; }

The issue I am facing is that the default value is specific for a given deployment and needs to be configured after deployment. I would like the user/administrator to be able to set the default value of these options using a configuration file.

Does anyone know how to change the default value for an option at run time? Then when starting the application I can load the configuration file and set the default values accordingly.

like image 269
denver Avatar asked Sep 05 '25 22:09

denver


1 Answers

For the benefit of anyone else looking for this, I was facing the same issue today, and I realised that Options work fine with C# 6 Auto Property Initializers.

[Option]
public string MyProperty { get; set; } = Properties.Settings.Default.MySetting;

No doubt in C# 5 and earlier you could achieve the same thing with a private backing property.

like image 96
Andrew Hoddinott Avatar answered Sep 10 '25 03:09

Andrew Hoddinott