Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active October 30, 2020 09:35
Show Gist options
  • Save Xliff/554c0017ecba46388ce0bc3ff173a183 to your computer and use it in GitHub Desktop.
Save Xliff/554c0017ecba46388ce0bc3ff173a183 to your computer and use it in GitHub Desktop.
Adding default values to your USAGE in Raku

Here's a little trick that will allow you to add your default values to the usage message in Raku. Unfortunately, this has to be done by example.

sub USAGE {
  my $u = $*USAGE;

  for &MAIN.signature.params -> $p {
    given $p.name {
      when '$port' {
        if $p.default -> $d {
          $u .= subst(
            'port to bind to',
            "port to bind to [default = { $d() }]"
          );
        }
      }
    }
  }

  $u.say;
}

sub MAIN (
  Str $root-directory,       #= Root directory for server
  Int :p(:$port) = 8080      #= Local port to bind to
) {
 ...
}

The advantage of this method is that if I change the value '8080' in MAIN'S signature, the USAGE message will update, as well. It's not a complete solution as yet, and a better way would be to do this at generation time, but it will work until someone improves on it.

Have a great day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment