Optionsdemo.dpr 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. TLogConfig = class
  13. private
  14. fVerbose : Boolean;
  15. fTimePrecission : Boolean;
  16. fEnvironment : string;
  17. published
  18. property Verbose : Boolean read fVerbose write fVerbose;
  19. property TimePrecission : Boolean read fTimePrecission write fTimePrecission;
  20. [Required, StringLength(5)]
  21. property Environment : string read fEnvironment write fEnvironment;
  22. end;
  23. TLoggingOptions = class(TOptions)
  24. private
  25. fPath : string;
  26. fConfig : TLogConfig;
  27. public
  28. constructor Create;
  29. destructor Destroy; override;
  30. published
  31. [Required, StringLength(255,'Path too long')]
  32. property Path : string read fPath write fPath;
  33. [Required]
  34. property Config : TLogConfig read fConfig write fConfig;
  35. end;
  36. TGlobalOptions = class(TOptions)
  37. private
  38. fStartMinimized : Boolean;
  39. fServers : TArray<string>;
  40. fLevel : Double;
  41. published
  42. property StartMinimized : Boolean read fStartMinimized write fStartMinimized;
  43. property Servers : TArray<string> read fServers write fServers;
  44. [Range(0.0,5.2)]
  45. property Level : Double read fLevel write fLevel;
  46. end;
  47. TUIOptions = class(TOptions)
  48. private
  49. fForeColor : Integer;
  50. fBackColor : Integer;
  51. published
  52. [Range(0, 255)]
  53. property ForeColor : Integer read fForeColor write fForeColor;
  54. [Range(0, 255,'Out of range')]
  55. property BackgroundColor : Integer read fBackColor write fBackColor;
  56. end;
  57. var
  58. Options : TFileOptionsContainer;
  59. LoggingOptions : TLoggingOptions;
  60. GlobalOptions : TGlobalOptions;
  61. UIOptions : TUIOptions;
  62. { TLoggingOptions }
  63. constructor TLoggingOptions.Create;
  64. begin
  65. fConfig := TLogConfig.Create;
  66. end;
  67. destructor TLoggingOptions.Destroy;
  68. begin
  69. fConfig.Free;
  70. inherited;
  71. end;
  72. begin
  73. try
  74. ReportMemoryLeaksOnShutdown := True;
  75. Options := TFileOptionsContainer.Create(TJsonOptionsSerializer.Create('.\options.conf'),True);
  76. Options.OnFileModified := procedure
  77. begin
  78. cout('Detected config file modification!',etWarning);
  79. end;
  80. Options.AddSection<TLoggingOptions>('Logging').ConfigureOptions(procedure(aOptions : TLoggingOptions)
  81. begin
  82. aOptions.Path := 'C:\';
  83. aOptions.Config.Verbose := True;
  84. aOptions.Config.Environment := 'PRO';
  85. end
  86. ).ValidateOptions;
  87. Options.AddSection<TGlobalOptions>('GlobalOptions').ConfigureOptions(procedure(aOptions : TGlobalOptions)
  88. begin
  89. aOptions.StartMinimized := True;
  90. aOptions.Servers := ['ServerOne','ServerTwo','ServerThree','ServerFour'];
  91. end
  92. ).ValidateOptions;
  93. Options.AddSection(TUIOptions).ConfigureOptions<TUIOptions>(procedure(aOptions : TUIOptions)
  94. begin
  95. aOptions.ForeColor := 77;
  96. aOptions.BackgroundColor := 100;
  97. end
  98. ).ValidateOptions;
  99. Options.Load;
  100. LoggingOptions := Options.GetSection<TLoggingOptions>;
  101. UIOptions := Options.GetSectionInterface<TUIOptions>.Value;
  102. GlobalOptions := Options.GetSectionInterface<TGlobalOptions>.Value;
  103. coutFmt('Logging.Path = %s',[LoggingOptions.Path],etInfo);
  104. coutFmt('Logging.Config.Environment = %s',[LoggingOptions.Config.Environment],etInfo);
  105. coutFmt('UIOptions.BackgroundColor = %d',[UIOptions.BackgroundColor],etInfo);
  106. coutFmt('GlobalOptions.StarMinimized = %s',[BoolToStr(GlobalOptions.StartMinimized,True)],etInfo);
  107. LoggingOptions.Path := 'D:\OtherTest';
  108. UIOptions.BackgroundColor := 120;
  109. Options.Save;
  110. //get instance of options section
  111. LoggingOptions := Options.GetSection<TLoggingOptions>;
  112. //LoggingOptions := Options.GetSection<TLoggingOptions>.Value;
  113. //gets value from IOptions<TUIOptions> interface (depency injection)
  114. UIOptions := Options.GetSectionInterface<TUIOptions>.Value;
  115. coutFmt('Logging.Path = %s',[LoggingOptions.Path],etInfo);
  116. coutFmt('UIOptions.BackgroundColor = %d',[UIOptions.BackgroundColor],etInfo);
  117. Readln;
  118. Options.Free;
  119. except
  120. on E: Exception do
  121. coutFmt('%s:%s',[E.ClassName,E.Message],etError);
  122. end;
  123. end.