Laurent Orseau
Usage: Use define-global to define global variables (possibly in different modules) with cross-module getters and setters. globals->command-line automatically generates a command line parser for all the globals defined in modules that are transitively required, and globals-interact generates and textual interaction for reading and writing globals.
Globals can be used without a command-line parser.
"burger.rkt"
1;initial value"Number of burgers";help string for the command line
racketburger.rkt--help
racketburger.rkt--burgers10
racketburger.rkt--burgersa
racket-lglobal/examples/minimal----help
racket-lglobal/examples/example----help
Additional remarks:
A global variable defined with define-global in a module A is shared between all modules that require A.
Note that a global defined in module A that is transitively required by module B can be fully accessed in module B even if A does not export any identifier. Globals can be removed from the command line with (globals->command-line (remove *my-global*(get-globals ))).
By convention, globals’ identifiers are surrounded by *. The value of a global *my-global* can be retrieved with (*my-global*) and set with (*my-global*some-new-value).
always have a single value at any time,
are not thread safe.
Suggestions, questions or issues? File an issue.
procedure
inithelpvalid?string->valuename:symbol?init:any/cvalid?:(->any/cany/c)string->value:(->any/cany/c)
The procedure valid? is used when setting or updating the value of the global to check if the new value is valid. Note that valid? is not used on init: this can be useful to set the initial value to #f for example while only allowing certain values when set by the user.
The procedure string->value is used to convert command line arguments to values that are checked with valid? before setting the corresponding global to this value. (They could also be used for example in text-field% in GUI applications.)
more-commands is an optional list of additional command-line flags, which can be used in particular to specify short flags.
syntax
( define-global varinithelpvalid?string->value[more-commands])
syntax
( define-global:boolean idinithelpmaybe-more-commands)
syntax
( define-global:string idinithelpmaybe-more-commands)
syntax
( define-global:natural0 idinithelpmaybe-more-commands)
syntax
( define-global:natural1 idinithelpmaybe-more-commands)
syntax
( define-global:integer idinithelpmaybe-more-commands)
syntax
( define-global:real idinithelpmaybe-more-commands)
syntax
( define-global:rational idinithelpmaybe-more-commands)
syntax
( define-global:category idinitvalshelpmaybe-more-commands)
vals = (listexpr...)
procedure
( global-unsafe-set! gv)→void?
g:global?v:any?g:global?updater:(->any/cany/c)
procedure
( global-set-from-string! gstr)→void?
g:global?str:string?
procedure
( get-globals )→(listofglobal? )
procedure
( globals->assoc [globals])→(listof(cons/csymbol? any/c))
procedure
( string->boolean s)→boolean?
s:string?
'("#f""#false""false"))#t)
syntax
( with-globals ([gv]...)body...)
procedure
#:name->stringname->string#:boolean-valid?bool?#:boolean-no-prefixno-prefix#:mutex-groupsmutex-groups#:argvargv#:programprogram#:usage-helpusage]trailing-arg-name...) → anytrailing-arg-name:string?
See parse-command-line for general information. Each list of globals within mutex-groups are placed in a separate once-any group in parse-command-line . Multi flags are not supported by globals.
See global->cmd-line-rule for some of the keywords and for more information about boolean flags.
See also the note in (get-globals ).
procedure
#:name->stringname->string#:boolean-valid?bool?g:global?name->string:default-name->stringbool?:boolean?no-prefix:"--no-~a"
(define-global:boolean *abool*#t"a boolean")
(define-global:boolean *abool*#f"a boolean")
procedure
( globals-interact [globals])→void?