(sigil args)
(sigil args) - Command-Line Argument Parsing
Declarative CLI definition with options, subcommands, and help generation. Uses records for clean, composable command structures.
Quick Start
(import (sigil args))
(define my-cli
(command
name: "my-tool"
description: "My awesome tool"
options: (list
(option name: 'verbose short: #\v long: "verbose"
description: "Enable verbose output")
(option name: 'output short: #\o long: "output"
value: "FILE" default: "out.txt"))
handler: (lambda (opts args)
(let ((verbose (alist-get 'verbose opts))
(output (alist-get 'output opts)))
(process-files args output verbose)))))
(run-command my-cli (cdr (command-line)))Subcommands
(define build-cmd
(command name: "build" description: "Build project"
handler: (lambda (opts args) ...)))
(define my-cli
(command name: "my-tool"
subcommands: (list build-cmd test-cmd)))Advanced Features
;; Negatable flags: --verbose or --no-verbose
(option name: 'verbose long: "verbose" negatable: #t)
;; Environment variable fallback
(option name: 'token long: "token" value: "TOKEN" env: "MY_APP_TOKEN")
;; Choice validation
(option name: 'level long: "level" value: "LEVEL"
choices: '("debug" "info" "warn" "error"))
;; Multi-value (repeatable) options
(option name: 'include short: #\I long: "include" value: "PATH" multi: #t)
;; -I foo -I bar => ((include . ("foo" "bar")))Supported Syntax
-v Short flag -vvv Repeated short flags (counted) -o value Short option with value --verbose Long flag --no-verbose Negated flag (if negatable: #t) --output=value Long option with = --output value Long option with space -- Stop option parsing
Exports
optionprocedureA command-line option definition for flags or value options.
(option (name 'verbose) (short #\v) (long "verbose")
(description "Enable verbose output"))
(option (name 'output) (short #\o) (long "output")
(value "FILE") (default "out.txt"))option?procedureTest if a value is a option struct.
option-nameprocedureSymbol used as key in parsed options alist.
option-shortprocedureSingle character for short form (e.g., #\v for -v), or #f.
option-longprocedureString for long form (e.g., "verbose" for --verbose), or #f.
option-descriptionprocedureHelp text displayed in usage output.
option-valueprocedureValue placeholder string (e.g., "FILE"), or #f for boolean flags.
option-defaultprocedureDefault value if option not provided.
option-requiredprocedureIf #t, option must be provided or parsing fails.
option-parseprocedureFunction to parse string value (e.g., string->number), or #f.
option-envprocedureEnvironment variable name to use as fallback (e.g., "MY_APP_OUTPUT").
option-choicesprocedureList of valid choices for validation (e.g., '("debug" "info" "warn")).
option-multiprocedureIf #t, option can be repeated and values accumulate into a list.
option-negatableprocedureIf #t, flag supports --no-X form to set value to #f.
%command--typevariableIf #t, flag supports --no-X form to set value to #f.
commandprocedureA CLI command or subcommand definition.
(command
(name "build")
(description "Build the project")
(options
(option (name 'config) (short #\c) (value "NAME")))
(handler (lambda (opts args) (run-build opts))))command?procedureTest if a value is a command struct.
command-nameprocedureCommand name string shown in help and used for matching.
command-descriptionprocedureHelp text describing what this command does.
command-optionsprocedureList of option records for this command.
command-handlerprocedureHandler procedure (lambda (opts args) ...) called when command runs.
command-subcommandsprocedureList of command records for nested subcommands.
%parse-result--typevariableList of command records for nested subcommands.
parse-resultprocedureThe result of parsing command-line arguments.
Check errors to determine if parsing succeeded. If empty, opts contains the parsed option values and args contains positional arguments.
parse-result?procedureTest if a value is a parse-result struct.
parse-result-optsprocedureParsed options as ((name . value) ...) alist.
parse-result-argsprocedurePositional arguments as a list of strings.
parse-result-errorsprocedureError messages as a list of strings. Empty if parsing succeeded.
parse-result-subcommandprocedureMatched subcommand command record, or #f if none.
find-subcommandprocedureFind a subcommand by name from a list of commands.
Returns the matching command record, or #f if not found.
(find-subcommand (command-subcommands cli) "build")
; => <command> record for "build" or #fparse-argsprocedureParse command-line arguments according to a command spec.
Returns a <parse-result> record with parsed options, positional arguments, any errors, and the matched subcommand (if any).
(let ((result (parse-args my-cmd '("-v" "--output" "file.txt" "arg1"))))
(parse-result-opts result) ; => ((verbose . #t) (output . "file.txt"))
(parse-result-args result)) ; => ("arg1")parse-args-loopprocedureMain parsing loop options - List of option specs to match against subcommands - List of subcommand specs argv - Remaining arguments to parse opts - Accumulated options alist args - Accumulated positional args errors - Accumulated errors stop? - #t if -- was seen (stop parsing options) subcmd - Matched subcommand (or #f)
parse-long-optionprocedureParse a long option: --name or --name=value or --no-name
parse-short-optionsprocedureParse short option(s): -v, -vvv, -o value
parse-short-charsprocedureParse individual characters in short option string
validate-choicesprocedureValidate value against choices if specified
get-env-valueprocedureGet value from environment variable if available
finalize-parseprocedureFinalize parsing: apply env fallback, defaults, check required, validate choices
generate-helpprocedureGenerate help text for a command.
Returns a formatted help string including usage, description, options, and subcommands.
(display (generate-help my-cmd))print-helpprocedurePrint help text for a command.
run-commandprocedureParse arguments and run the appropriate command handler.
Handles parsing, error display, automatic --help/-h support, subcommand dispatch, and exit status.
(run-command my-cli (cdr (command-line)))command-passthroughvariable(No description)
parse-result-passthroughvariable(No description)