optex.pp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. program testopt;
  2. { Program to depmonstrate the getopts function. }
  3. uses getopts;
  4. var c : char;
  5. optionindex : integer;
  6. theopts : array[1..7] of option;
  7. begin
  8. with theopts[1] do
  9. begin
  10. name:='add';
  11. has_arg:=1;
  12. flag:=nil;
  13. value:=#0;
  14. end;
  15. with theopts[2] do
  16. begin
  17. name:='append';
  18. has_arg:=0;
  19. flag:=nil;
  20. value:=#0;
  21. end;
  22. with theopts[3] do
  23. begin
  24. name:='delete';
  25. has_arg:=1;
  26. flag:=nil;
  27. value:=#0;
  28. end;
  29. with theopts[4] do
  30. begin
  31. name:='verbose';
  32. has_arg:=0;
  33. flag:=nil;
  34. value:=#0;
  35. end;
  36. with theopts[5] do
  37. begin
  38. name:='create';
  39. has_arg:=1;
  40. flag:=nil;
  41. value:='c'
  42. end;
  43. with theopts[6] do
  44. begin
  45. name:='file';
  46. has_arg:=1;
  47. flag:=nil;
  48. value:=#0;
  49. end;
  50. with theopts[7] do
  51. begin
  52. name:='';
  53. has_arg:=0;
  54. flag:=nil;
  55. end;
  56. c:=#0;
  57. repeat
  58. c:=getlongopts('abc:d:012',@theopts[1],optionindex);
  59. case c of
  60. '1','2','3','4','5','6','7','8','9' :
  61. begin
  62. writeln ('Got optind : ',c)
  63. end;
  64. #0 : begin
  65. write ('Long option : ',theopts[optionindex].name);
  66. if theopts[optionindex].has_arg>0 then
  67. writeln (' With value : ',optarg)
  68. else
  69. writeln
  70. end;
  71. 'a' : writeln ('Option a.');
  72. 'b' : writeln ('Option b.');
  73. 'c' : writeln ('Option c : ', optarg);
  74. 'd' : writeln ('Option d : ', optarg);
  75. '?',':' : writeln ('Error with opt : ',optopt);
  76. end; { case }
  77. until c=endofoptions;
  78. if optind<=paramcount then
  79. begin
  80. write ('Non options : ');
  81. while optind<=paramcount do
  82. begin
  83. write (paramstr(optind),' ');
  84. inc(optind)
  85. end;
  86. writeln
  87. end
  88. end.