Optionsdemo.dpr 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 : TOptionsContainer;
  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. Options := TOptionsContainer.Create('.\options.conf',TJsonOptionsSerializer.Create,True);
  75. Options.OnFileModified := procedure
  76. begin
  77. cout('Detected config file modification!',etWarning);
  78. end;
  79. Options.AddSection<TLoggingOptions>('Logging').ConfigureOptions(procedure(aOptions : TLoggingOptions)
  80. begin
  81. aOptions.Path := 'C:\';
  82. aOptions.Config.Verbose := True;
  83. aOptions.Config.Environment := 'PRO';
  84. end
  85. ).ValidateOptions;
  86. Options.AddSection<TGlobalOptions>('GlobalOptions').ConfigureOptions(procedure(aOptions : TGlobalOptions)
  87. begin
  88. aOptions.StartMinimized := True;
  89. aOptions.Servers := ['ServerOne','ServerTwo','ServerThree','ServerFour'];
  90. end
  91. ).ValidateOptions;
  92. Options.AddSection(TUIOptions).ConfigureOptions<TUIOptions>(procedure(aOptions : TUIOptions)
  93. begin
  94. aOptions.ForeColor := 77;
  95. aOptions.BackgroundColor := 100;
  96. end
  97. ).ValidateOptions;
  98. Options.Load;
  99. LoggingOptions := Options.GetSection<TLoggingOptions>;
  100. UIOptions := Options.GetSectionInterface<TUIOptions>.Value;
  101. GlobalOptions := Options.GetSectionInterface<TGlobalOptions>.Value;
  102. coutFmt('Logging.Path = %s',[LoggingOptions.Path],etInfo);
  103. coutFmt('Logging.Config.Environment = %s',[LoggingOptions.Config.Environment],etInfo);
  104. coutFmt('UIOptions.BackgroundColor = %d',[UIOptions.BackgroundColor],etInfo);
  105. coutFmt('GlobalOptions.StarMinimized = %s',[BoolToStr(GlobalOptions.StartMinimized,True)],etInfo);
  106. LoggingOptions.Path := 'D:\OtherTest';
  107. UIOptions.BackgroundColor := 120;
  108. Options.Save;
  109. //get instance of options section
  110. LoggingOptions := Options.GetSection<TLoggingOptions>;
  111. //LoggingOptions := Options.GetSection<TLoggingOptions>.Value;
  112. //gets value from IOptions<TUIOptions> interface (depency injection)
  113. UIOptions := Options.GetSectionInterface<TUIOptions>.Value;
  114. coutFmt('Logging.Path = %s',[LoggingOptions.Path],etInfo);
  115. coutFmt('UIOptions.BackgroundColor = %d',[UIOptions.BackgroundColor],etInfo);
  116. Readln;
  117. except
  118. on E: Exception do
  119. coutFmt('%s:%s',[E.ClassName,E.Message],etError);
  120. end;
  121. end.