Skip to content

Commit

Permalink
Add command-line argument support via racket (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnelson authored Jun 17, 2024
1 parent c2f0b83 commit a7cdf63
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
2 changes: 1 addition & 1 deletion forge/examples/sudoku_opt_viz/sudoku_unrolled.frg
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun get_grid[s: BoardState, subgrid: Int]: set Int {
s.board[rows][cols]
}

pred solution[s: PuzzleState] {
pred solution[s: BoardState] {
-- ** Rows and Columns **
-- don't use #... = 9 here; instead something like:
all r: values | s.board[r][Int] = values
Expand Down
2 changes: 1 addition & 1 deletion forge/examples/sudoku_opt_viz/sudoku_with_inst.frg
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun values: set Int {
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
}

pred solution[s: PuzzleState] {
pred solution[s: BoardState] {
-- ** Rows and Columns **
-- don't use #... = 9 here; instead something like:
all r: values | s.board[r][Int] = values
Expand Down
2 changes: 1 addition & 1 deletion forge/examples/sudoku_opt_viz/sudoku_with_inst_2.frg
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun get_grid[s: BoardState, row_offset, col_offset: Int]: set Int {
s.board[rows][cols]
}

pred solution[s: PuzzleState] {
pred solution[s: BoardState] {
-- ** Rows and Columns **
-- don't use #... = 9 here; instead something like:
all r: Helper.values | s.board[r][Int] = Helper.values
Expand Down
5 changes: 3 additions & 2 deletions forge/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ for testFile in $testFiles; do
current=`date "+%X"`
echo -e "\nRunning $testFile ($current)"

#start=`date +%s`
racket $testFile > /dev/null
#start=`date +%s`
# Use permanent (-O) option flag to always disable Sterling
racket $testFile -O run_sterling \'off > /dev/null
#end=`date +%s`
#echo -e "Testfile took $((end-start)) seconds."
testExitCode=$?
Expand Down
81 changes: 79 additions & 2 deletions forge/sigs.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@
(struct-copy State state
[inst-map new-state-inst-map]))

; this is not managed by Forge's "rolling state"; it should only be set by the command-line.
(define option-overrides (box '()))

(define (set-option! option value #:original-path [original-path #f])
(cond [(or (equal? option 'verbosity)
(equal? option 'verbose))
(cond [(member option (unbox option-overrides))
(printf "Option ~a was given when Forge started with --override option; ignoring assignment to ~a.~n"
option value)]
[(or (equal? option 'verbosity) (equal? option 'verbose))
(set-verbosity value)]
[else
(update-state! (state-set-option curr-state option value #:original-path original-path))]))
Expand Down Expand Up @@ -1133,3 +1138,75 @@
(provide output-all-test-failures
report-test-failure
reset-test-failures!)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Support for providing options at the command line
; sigs-structs.rkt provides an `option-types` value that
; we can use to cast inputs to the appropriate type.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; The lowercase -o will set an initial option value, but will be OVERRIDDEN
; by file-level option statements. E.g., this at the command line (note the
; need to escape the backquote):
; racket ring_of_lights.frg -o run_sterling \'off -o verbose 1
; * will NOT run sterling (because the example doesn't give a value for that option)
; * WILL give verbose 5 output (because the example gives a value of 5 for that option)

; In contrast, the uppercase -O will set the initial option value and disallow it changing.
; E.g.,
; racket ring_of_lights.frg -o run_sterling \'off -O verbose 1
; will give verbose 1 output.

(define (string->option-type name value)
(define type-pred (hash-ref option-types name #f))
(cond
; Arrived as a number
[(string->number value) (string->number value)]
; Arrived as a single-quote prefixed symbol
[(equal? (string-ref value 0) #\') (string->symbol (substring value 1))]
; Otherwise, try to infer from the type predicate for the option
[(equal? type-pred symbol?) (string->symbol value)]
[(equal? type-pred exact-nonnegative-integer?) (string->number value)]
[(equal? type-pred exact-positive-integer?) (string->number value)]
[(equal? type-pred exact-integer?) (string->number value)]
; This covers too much at the moment (some of the option-types values are /more complex predicates/)
[else value]))

(require racket/cmdline)

;; BEWARE: this appears to interact with `raco` when installing the Forge package. E.g.,
;; printing out `remaining-args` will actually print something when installing Forge, but
;; with arguments from `raco`:
;; cl result: (pkg install ./forge ./froglet)
;; This could technically cause a conflict with any existing `raco` arguments.
;; -o and -O are not used by `raco pkg install` as of June 14, 2024.

(define remaining-args (command-line
; Default:
;#:program (find-system-path 'run-file)
; Default:
;#:argv (current-command-line-arguments)
#:usage-help
"When running Forge from the command line, use the -o or --option flag to send options."
"Format: -o <option name> <option value>."
"If the upper-case -O is used, the option cannot be rewritten by the model file."

#:multi
[("-o" "--option") OPTION-NAME OPTION-VALUE
"Option set"
(begin
(printf "Setting ~a = ~a~n" (string->symbol OPTION-NAME) OPTION-VALUE)
(set-option! (string->symbol OPTION-NAME)
(string->option-type OPTION-NAME OPTION-VALUE)))]
[("-O" "--override") OPTION-NAME OPTION-VALUE
"Option set and override"
(begin
(printf "Setting and overriding ~a = ~a~n" (string->symbol OPTION-NAME) OPTION-VALUE)
(set-option! (string->symbol OPTION-NAME)
(string->option-type OPTION-NAME OPTION-VALUE))
; Don't allow the Forge file to reset this option.
(set-box! option-overrides (cons (string->symbol OPTION-NAME) (unbox option-overrides))))]

#:args remaining-args remaining-args))

0 comments on commit a7cdf63

Please sign in to comment.