1

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.

denver
  • 2,863
  • 2
  • 31
  • 45
  • http://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property – Yaugen Vlasau Nov 04 '13 at 21:52
  • @YaugenVlasau That question shows how I can retrieve the value assigned to an attribute, but not how I can change it. I tried modifying the value of the retrieved attribute, but it had no effect. – denver Nov 04 '13 at 22:14
  • I know it has been quite a while since you posted this, but I am trying to do the exact same thing. Did you come up with any solutions? – Matt Sanders Dec 07 '15 at 16:25

2 Answers2

3

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.

0

I would create the properties as Application Settings, then allow the user to override them with command line arguments and calculate the resulting values at runtime. This way you have compiled default values, possibilities for the user to override with custom defaults in the config file and a way to set one-time overrides at startup.

Mattias Åslund
  • 3,877
  • 2
  • 18
  • 17
  • Yes, that is what I am trying to do (what I tried to explain in the question). The question is how to do it given command line parsing (and hence the default value for the run) is being done using the CommandLine Parser Library. – denver Nov 05 '13 at 04:23
  • I tried this approach, but the value from the Property.Settings.Default is not a constant expression and can not be used as an attribute value. – Matt Sanders Dec 07 '15 at 16:38