From b61bd682bce9b06e3863c3fa5d0781a43d2c7cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20S=2E=20Martins?= Date: Fri, 3 Jun 2016 21:16:44 +0100 Subject: [PATCH] Add argparsing abilities to the script. --- crossword_generator.py | 31 ++++++++++++++++++++++++++----- readme.md | 2 +- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/crossword_generator.py b/crossword_generator.py index ddef1d3..0bea06e 100755 --- a/crossword_generator.py +++ b/crossword_generator.py @@ -4,8 +4,6 @@ This script takes a list of words and creates a new latex table representing a crosswod puzzle, which is then printed to PDF, and can be printed to actual paper, if you're one of those people. - -TODO: Usage instructions """ # STL imports @@ -16,6 +14,7 @@ import tempfile import os import shutil import time +import argparse # Auxiliary Functions @@ -507,12 +506,34 @@ def write_grid(grid, screen=False, out_file="table.tex", words=[]): if __name__ == "__main__": - # TODO: argparse + # Parse arguments + parser = argparse.ArgumentParser(description='Generate a crossword puzzle.') + parser.add_argument('--word-file', type=str, + default="words.txt", + dest="word_file", + help="A file containing words, one word per line.") + parser.add_argument('--dim', type=int, + nargs="+", + default=[20, 20], + dest="dim", + help="Dimensions of the grid to build.") + parser.add_argument('-t', type=int, + default=60, + dest="timeout", + help="Maximum execution time, in seconds.") + parser.add_argument('-o', type=float, + default=0.5, + dest="target_occ", + help="Minimum desired occupancy of the final grid.") + args = parser.parse_args() + # Read words from file - words = read_word_list("words.txt") + words = read_word_list(args.word_file) # Generate grid - grid = generate_grid_new(words, [20, 20]) + dim = args.dim if len(args.dim)==2 else [args.dim[0], args.dim[0]] + print("Making a grid of dimension{}, in {} seconds with a target occupancy of {}.".format(dim, args.timeout, args.target_occ)) + grid = generate_grid_new(words, dim, timeout=args.timeout, occ_goal=args.target_occ) # Show grid print("Final grid:") diff --git a/readme.md b/readme.md index 2b84476..9890e01 100644 --- a/readme.md +++ b/readme.md @@ -28,4 +28,4 @@ On my consumer-grade machine (i7-6700HQ) the algorithm can generate a 20x20 grid Usage --- -For now (until I implement argparsing), all you have to do is run the script on a folder where a "words.txt" file with one word per line exists. I recommend using the aforementioned lists! +All you have to do is run the script on a folder where a "words.txt" file with one word per line exists. I recommend using the aforementioned lists! Run `./crossword_generator -h` to see all available options.