jsonmerge.pp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. {
  2. This file is part of the Free Component Library
  3. Merge 2 JSON files.
  4. Copyright (c) 2021 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program jsonmerge;
  12. {$mode objfpc}{$H+}
  13. uses
  14. Classes, SysUtils, CustApp, fpJSON, jsonparser, fpjsonapply;
  15. type
  16. { TJSONMergeApplication }
  17. TJSONMergeApplication = class(TCustomApplication)
  18. private
  19. function ParseOptions: string;
  20. protected
  21. FApplier : TJSONApplier;
  22. procedure DoRun; override;
  23. public
  24. constructor Create(TheOwner: TComponent); override;
  25. destructor Destroy; override;
  26. procedure Usage(const aErrorMsg: String); virtual;
  27. end;
  28. { TJSONMergeApplication }
  29. Function TJSONMergeApplication.ParseOptions : string;
  30. begin
  31. Result:='';
  32. FApplier.SourceFileName:=GetOptionValue('s','source');
  33. FApplier.ApplyFileName:=GetOptionValue('a','apply');
  34. FApplier.DestFileName:=GetOptionValue('d','destination');
  35. FApplier.CaseInsensitive:=HasOption('i','ignorecase');
  36. FApplier.RemoveNonExisting:=HasOption('r','remove');
  37. FApplier.Formatted:=HasOption('f','format');
  38. FApplier.SourcePath:=GetOptionValue('p','path');
  39. FApplier.ApplyPath:=GetOptionValue('y','apply-path');
  40. if (FApplier.SourceFileName='') then
  41. Result:='Missing source filename'
  42. else if (FApplier.ApplyFileName='') then
  43. Result:='Missing apply filename';
  44. if (Result='') and (FApplier.DestFileName='') then
  45. FApplier.DestFileName:=FApplier.SourceFileName;
  46. end;
  47. procedure TJSONMergeApplication.DoRun;
  48. var
  49. ErrorMsg: String;
  50. begin
  51. Terminate;
  52. ErrorMsg:=CheckOptions('hs:a:d:irfp:y:', ['help','source:','apply:','destination:','ignorecase','remove','format','path:','apply-path:']);
  53. if (ErrorMsg='') and not HasOption('h','help') then
  54. ErrorMsg:=ParseOptions;
  55. if (ErrorMsg<>'') or HasOption('h','help') then
  56. begin
  57. Usage(ErrorMsg);
  58. Exit;
  59. end;
  60. FApplier.Execute;
  61. end;
  62. constructor TJSONMergeApplication.Create(TheOwner: TComponent);
  63. begin
  64. inherited Create(TheOwner);
  65. StopOnException:=True;
  66. FApplier:=TJSONApplier.Create(Self);
  67. end;
  68. destructor TJSONMergeApplication.Destroy;
  69. begin
  70. FreeAndNil(FApplier);
  71. inherited Destroy;
  72. end;
  73. procedure TJSONMergeApplication.Usage(const aErrorMsg: String);
  74. begin
  75. if (aErrorMsg<>'') then
  76. Writeln(aErrorMsg);
  77. writeln('Usage: ', ExeName, ' -h');
  78. writeln('where');
  79. writeln('-a --apply=FILE File with JSON to apply to input.');
  80. writeln('-d --destination=FILE File to write resulting JSON to (defaults to input)');
  81. writeln('-f --format Format destination JSON.');
  82. writeln('-h --help This help message.');
  83. writeln('-i --ignorecase Ignore case when looking for element names.');
  84. writeln('-p --path=PATH Start applying at element at PATH in source.');
  85. writeln('-r --remove Remove elements in source not existing in apply file.');
  86. writeln('-s --source=FILE File with JSON input.');
  87. writeln('-y --apply-path=PATH Start applying at element at PATH in apply.');
  88. ExitCode:=Ord(aErrorMsg<>'');
  89. end;
  90. var
  91. Application: TJSONMergeApplication;
  92. begin
  93. Application:=TJSONMergeApplication.Create(nil);
  94. Application.Title:='JSON merge tool';
  95. Application.Run;
  96. Application.Free;
  97. end.