Guest User

Untitled

a guest
Jul 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. BEGIN @*ARGS = < -w -a -b >;
  2.  
  3. class MatchContext {
  4. has $.name;
  5. has $.available is rw;
  6. }
  7.  
  8. class Option {
  9. has $.name;
  10. has $.supply is rw;
  11.  
  12. method callback() {
  13. $!supply.tap(
  14. -> $v {
  15. given $v {
  16. if .available {
  17. if .name eq $!name {
  18. .available = False;
  19. }
  20. }
  21. }
  22. }
  23. );
  24. }
  25. }
  26.  
  27. sub getopt(@args is copy = @*ARGS) {
  28. my $p = Supplier.new;
  29. my @options = [
  30. Option.new(name => "a", supply => $p.Supply),
  31. Option.new(name => "b", supply => $p.Supply),
  32. ];
  33.  
  34. @options[0].callback();
  35. @options[1].callback();
  36.  
  37. sub parse(Supply $source --> Supply) {
  38. supply {
  39. whenever $source {
  40. if .starts-with('-') || .starts-with('--') {
  41. note "In Parser: Emit the option {.Str}";
  42. $p.emit(
  43. my $mc = MatchContext.new(
  44. name => .substr(1),
  45. :available
  46. ));
  47. emit "The {.Str} matched ? " ~ ($mc.available ?? 'FAILED' !! 'OK');
  48. }
  49. }
  50. }
  51. }
  52.  
  53. my $parse = parse(Supply.from-list(@args));
  54.  
  55. react {
  56. whenever $parse -> $msg {
  57. say "In MAIN: GOT MESSAGE: ", $msg;
  58. }
  59. }
  60.  
  61. note "done the supplier";
  62. $p.done;
  63. }
  64.  
  65. &getopt();
Add Comment
Please, Sign In to add comment