Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First draft of USAGE helper function

  • Loading branch information...
commit 1f08be30100113917bb0c63d47c748257191057c 1 parent e160b24
Patrick Abi Salloum authored
Showing with 50 additions and 0 deletions.
  1. +50 −0 usage.p6
50 usage.p6
View
@@ -0,0 +1,50 @@
+use v6;
+use Test;
+plan *;
+
+sub USAGE ($sub=&MAIN) {
+ my $sig = $sub.signature;
+ my @arguments;
+ for $sig.params -> $param {
+ my $argument;
+ if ($param.named) {
+ $argument = "--"
+ ~ $param.name.substr(1)
+ ~ ($param.type ~~ Bool ?? '' !! "=value-of-{$param.name.substr(1)}")
+ ;
+ } else {
+ $argument = $param.name.substr(1);
+ }
+ $argument = "[$argument]" if $param.optional;
+ @arguments.push($argument);
+ }
+ return "Usage\n" ~ $*PROGRAM_NAME ~ ' ' ~ @arguments.join(' ');
+
+}
+
+
+my $common = "Usage\n$*PROGRAM_NAME";
+
+sub MAIN($first,$second) {...}
+is( USAGE() , "$common first second" , 'By default we introspect MAIN');
+
+my $main ;
+$main = sub ($first?) {...}
+is( USAGE($main) , "$common [first]" , 'Optional');
+
+$main = sub (:$named) {...}
+is( USAGE($main) , "$common [--named=value-of-named]" , 'named optional');
+
+$main = sub (:$named!) {...}
+is( USAGE($main) , "$common --named=value-of-named" , 'named mandatory');
+
+$main = sub (Bool :$named) {...}
+is( USAGE($main) , "$common [--named]" , 'Bool optional');
+
+$main = sub (Bool :$named!) {...}
+is( USAGE($main) , "$common --named" , 'Bool mandatory');
+
+$main = sub ($first, *@rest, Bool :$verbose, :$outfile) {...}
+is( USAGE($main) , "$common first rest [--verbose] [--outfile=value-of-outfile]" , 'Mix of params');
+
+done_testing();
Please sign in to comment.
Something went wrong with that request. Please try again.