about summary refs log tree commit diff stats
path: root/lib/Octans/CLI.rakumod
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Octans/CLI.rakumod')
-rw-r--r--lib/Octans/CLI.rakumod57
1 files changed, 35 insertions, 22 deletions
diff --git a/lib/Octans/CLI.rakumod b/lib/Octans/CLI.rakumod
index 24ef59f..46b1afd 100644
--- a/lib/Octans/CLI.rakumod
+++ b/lib/Octans/CLI.rakumod
@@ -1,23 +1,25 @@
 use Octans::Puzzle;
 use Octans::WordSearch;
 
-proto MAIN (|) is export {unless so @*ARGS {say $*USAGE; exit;}; {*}}
-multi sub MAIN(Bool :$version) is hidden-from-USAGE {
+# If no arguments are passed then run USAGE & exit.
+proto MAIN (|) is export {unless so @*ARGS {USAGE(); exit;}; {*}}
+
+multi sub MAIN (Bool :$version) is hidden-from-USAGE {
     say "Octans v" ~ $?DISTRIBUTION.meta<version>;
 }
 
 multi sub MAIN (
-    Str $url?, #= url for Algot's crossword
+    Str $path?, #= path to the crossword (file or url)
     Str :$dict = (%?RESOURCES<mwords/354984si.ngl> //
                   "/usr/share/dict/words").Str, #= dictionary file
     Bool :s($sample), #= run the sample puzzle
     Bool :v($verbose), #= increase verbosity
     Bool :$version, #= print version
 ) {
-    # Print usage & exit if both sample & url are not passed.
-    unless ($sample or $url) {
-        say $*USAGE;
-        exit 0;
+    # Print usage & exit if both sample & path are not passed.
+    unless ($sample or $path) {
+        USAGE();
+        exit;
     }
 
     # @dict holds the sorted dictionary. Only consider words >= 7
@@ -27,22 +29,22 @@ multi sub MAIN (
     # @puzzle holds the puzzle.
     #
     # @gray-squares holds the list of indexes of valid starting
-    # positions in the puzzle.
+    # positions in the puzzle. ($y, $x)
     my (@puzzle, @gray-squares);
 
     # Set the sample puzzle if requested.
     if $sample {
-        @puzzle = [
-            [<n a t k>],
-            [<i m e c>],
-            [<a r d e>],
-            [<t e c h>]
-        ];
-        @gray-squares = [3, 0], [2, 0]; # $y, $x
+        parse-puzzle(
+            < n a t k
+              i m e c
+              a* r d e
+              t* e c h >,
+            @puzzle, @gray-squares
+        );
     }
 
-    # Get the puzzle from $url if it's passed.
-    get-puzzle($url, @puzzle, @gray-squares) with $url;
+    # Get the puzzle from $path if it's passed.
+    get-puzzle($_, @puzzle, @gray-squares) with $path;
 
     if $verbose {
         # Don't print path if using the dictionary included with the
@@ -85,11 +87,11 @@ multi sub MAIN (
                 for ^@puzzle.elems -> $y {
                     print " " x 3;
                     for ^@puzzle[$y].elems -> $x {
-                        print " ", (@visited[$y][$x] ??
-                                    (%π’»π’Άπ“ƒπ’Έπ“Ž-π’Έπ’½π’Άπ“‡π“ˆ{@puzzle[$y][$x]}
-                                     // @puzzle[$y][$x]) !!
-                                    @puzzle[$y][$x]
-                                   );
+                        print " ", (
+                        @visited[$y][$x] ??
+                        (%π’»π’Άπ“ƒπ’Έπ“Ž-π’Έπ’½π’Άπ“‡π“ˆ{@puzzle[$y][$x]} // @puzzle[$y][$x]) !!
+                        @puzzle[$y][$x]
+                    );
                     }
                     print "\n";
                 }
@@ -97,3 +99,14 @@ multi sub MAIN (
         }
     }
 }
+
+# Modify USAGE to include input file format.
+sub USAGE {
+    say $*USAGE;
+    say "\nInput file format:
+
+    n a t k
+    i m e c
+    a* r d e
+    t* e c h";
+}