From 02cd965f42d93e9951917d7a99d1d7e711f448d3 Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 20 Jan 2021 16:38:24 +0530 Subject: 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. --- lib/Octans/CLI.rakumod | 57 +++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 22 deletions(-) (limited to 'lib/Octans/CLI.rakumod') 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; } multi sub MAIN ( - Str $url?, #= url for Algot's crossword + Str $path?, #= path to the crossword (file or url) Str :$dict = (%?RESOURCES // "/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 = [ - [], - [], - [], - [] - ]; - @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"; +} -- cgit 1.4.1-2-gfad0