about summary refs log tree commit diff stats
path: root/lib/Octans/CLI.rakumod
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-01-20 16:38:24 +0530
committerAndinus <andinus@nand.sh>2021-01-20 16:38:24 +0530
commit02cd965f42d93e9951917d7a99d1d7e711f448d3 (patch)
treecc21e30fd16eb72c876910a5cfaf481071558c9e /lib/Octans/CLI.rakumod
parent5200e692a94dd6cc8ce98eef49e01ebe0c2a158b (diff)
downloadoctans-02cd965f42d93e9951917d7a99d1d7e711f448d3.tar.gz
Read from file if passed, modify USAGE
Previouly, the only way of passing the puzzle was to enter a url. Now
octans is able to read from files too. If the file exist & it's
readable then octans will read the puzzle from there.
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";
+}