demonodecmdlineoptions.pas 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. program demonodecmdlineoptions;
  2. {$mode objfpc}
  3. uses
  4. nodejsapp, JS, Classes, SysUtils, nodeJS;
  5. type
  6. { TMyApplication }
  7. TMyApplication = class(TNodeJSApplication)
  8. procedure doRun; override;
  9. private
  10. procedure Usage(Msg: string);
  11. end;
  12. procedure TMyApplication.Usage(Msg : string);
  13. begin
  14. if Msg<>'' then
  15. Writeln('Error :',Msg);
  16. Writeln('Usage:');
  17. Writeln(ExeName,' [options]');
  18. Writeln('Where options is one or more of');
  19. Writeln('-h --help this help message');
  20. Writeln('-e --echo echo option');
  21. ExitCode:=Ord(Msg<>'');
  22. end;
  23. procedure TMyApplication.doRun;
  24. Var
  25. S : String;
  26. begin
  27. S:=CheckOptions('he:',['help','echo']);
  28. if (S<>'') or HasOption('h','help') then
  29. Usage(S);
  30. if HasOption('e','echo') then
  31. Writeln(GetoptionValue('e','echo'));
  32. for S in GetNonOptions('he:',['help','echo']) do
  33. Writeln(s);
  34. Terminate;
  35. end;
  36. var
  37. Application : TMyApplication;
  38. begin
  39. Application:=TMyApplication.Create(nil);
  40. Application.Initialize;
  41. Application.Run;
  42. Application.Free;
  43. end.