optex.pp 1.9 KB

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