Optionsdemo.dpr 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. program Optionsdemo;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. System.SysUtils,
  6. Quick.Commons,
  7. Quick.Console,
  8. Quick.Options,
  9. Quick.Options.Serializer.Json,
  10. Quick.Options.Serializer.Yaml;
  11. type
  12. TLoggingOptions = class(TOptions)
  13. private
  14. fPath : string;
  15. published
  16. [Required, StringLength(255,'Path too long')]
  17. property Path : string read fPath write fPath;
  18. end;
  19. TGlobalOptions = class(TOptions)
  20. private
  21. fStartMinimized : Boolean;
  22. fServers : TArray<string>;
  23. fLevel : Double;
  24. published
  25. property StartMinimized : Boolean read fStartMinimized write fStartMinimized;
  26. property Servers : TArray<string> read fServers write fServers;
  27. [Range(0.0,5.2)]
  28. property Level : Double read fLevel write fLevel;
  29. end;
  30. TUIOptions = class(TOptions)
  31. private
  32. fForeColor : Integer;
  33. fBackColor : Integer;
  34. published
  35. [Range(0, 255)]
  36. property ForeColor : Integer read fForeColor write fForeColor;
  37. [Range(0, 255,'Out of range')]
  38. property BackgroundColor : Integer read fBackColor write fBackColor;
  39. end;
  40. var
  41. Options : TOptionsContainer;
  42. LoggingOptions : TLoggingOptions;
  43. GlobalOptions : TGlobalOptions;
  44. UIOptions : TUIOptions;
  45. begin
  46. try
  47. Options := TOptionsContainer.Create('.\options.conf',TJsonOptionsSerializer.Create,True);
  48. Options.OnFileModified := procedure
  49. begin
  50. cout('Detected config file modification!',etWarning);
  51. end;
  52. Options.AddSection<TLoggingOptions>('Logging').ConfigureOptions(procedure(aOptions : TLoggingOptions)
  53. begin
  54. aOptions.Path := 'C:\';
  55. end
  56. ).ValidateOptions;
  57. Options.AddSection<TGlobalOptions>('GlobalOptions').ConfigureOptions(procedure(aOptions : TGlobalOptions)
  58. begin
  59. aOptions.StartMinimized := True;
  60. aOptions.Servers := ['ServerOne','ServerTwo','ServerThree','ServerFour'];
  61. end
  62. ).ValidateOptions;
  63. Options.AddSection(TUIOptions).ConfigureOptions<TUIOptions>(procedure(aOptions : TUIOptions)
  64. begin
  65. aOptions.ForeColor := 77;
  66. aOptions.BackgroundColor := 100;
  67. end
  68. ).ValidateOptions;
  69. Options.Load;
  70. LoggingOptions := Options.GetSection<TLoggingOptions>;
  71. UIOptions := Options.GetSectionInterface<TUIOptions>.Value;
  72. GlobalOptions := Options.GetSectionInterface<TGlobalOptions>.Value;
  73. coutFmt('Logging.Path = %s',[LoggingOptions.Path],etInfo);
  74. coutFmt('UIOptions.BackgroundColor = %d',[UIOptions.BackgroundColor],etInfo);
  75. coutFmt('GlobalOptions.StarMinimized = %s',[BoolToStr(GlobalOptions.StartMinimized,True)],etInfo);
  76. LoggingOptions.Path := 'D:\OtherTest';
  77. UIOptions.BackgroundColor := 120;
  78. Options.Save;
  79. //get instance of options section
  80. LoggingOptions := Options.GetSection<TLoggingOptions>;
  81. //LoggingOptions := Options.GetSection<TLoggingOptions>.Value;
  82. //gets value from IOptions<TUIOptions> interface (depency injection)
  83. UIOptions := Options.GetSectionInterface<TUIOptions>.Value;
  84. coutFmt('Logging.Path = %s',[LoggingOptions.Path],etInfo);
  85. coutFmt('UIOptions.BackgroundColor = %d',[UIOptions.BackgroundColor],etInfo);
  86. Readln;
  87. except
  88. on E: Exception do
  89. coutFmt('%s:%s',[E.ClassName,E.Message],etError);
  90. end;
  91. end.