AtlatestRepositorysigil-args
sigil-args / tree / docsargs.md
1
# Args3
> Declarative CLI definition with options, subcommands, and help generation.5
```scheme6
(import (sigil args))7
```9
## Defining Options11
Options are records describing CLI flags and value parameters.13
```scheme14
;; Boolean flag15
(option name: 'verbose short: #\v long: "verbose"16
description: "Enable verbose output")18
;; Option with value and default19
(option name: 'output short: #\o long: "output"20
value: "FILE" default: "out.txt"21
description: "Output file path")23
;; Required option with parser24
(option name: 'count long: "count"25
value: "N" parse: string->number required: #t)27
;; Environment variable fallback28
(option name: 'token long: "token" value: "TOKEN"29
env: "MY_APP_TOKEN")31
;; Restricted choices32
(option name: 'level long: "level" value: "LEVEL"33
choices: '("debug" "info" "warn"))35
;; Multi-value (repeatable)36
(option name: 'include short: #\I long: "include"37
value: "PATH" multi: #t)39
;; Negatable flag (supports --no-color)40
(option name: 'color long: "color" negatable: #t default: #t)41
```43
Option fields: `name:` (symbol, required), `short:` (char), `long:` (string), `description:` (string), `value:` (string placeholder — omit for boolean flags), `default:`, `required:` (boolean), `parse:` (string converter), `env:` (env var name), `choices:` (list of valid strings), `multi:` (boolean), `negatable:` (boolean).45
## Defining Commands47
Commands group options together with a handler or subcommands.49
```scheme50
(command51
name: "my-tool"52
description: "A useful CLI tool"53
options: (list54
(option name: 'verbose short: #\v long: "verbose")55
(option name: 'output short: #\o long: "output" value: "FILE"))56
handler: (lambda (opts args)57
(let ((verbose (alist-get 'verbose opts))58
(output (alist-get 'output opts)))59
(process-files args output verbose))))61
;; With subcommands62
(command63
name: "my-tool"64
subcommands: (list65
(command name: "build" description: "Build project"66
options: (list (option name: 'config short: #\c value: "NAME"))67
handler: (lambda (opts args) ...))68
(command name: "test" description: "Run tests"69
handler: (lambda (opts args) ...))))70
```72
## Parsing Arguments74
`parse-args` returns a `parse-result` record.76
```scheme77
(let ((result (parse-args my-cmd '("-v" "--output" "out.txt" "file1" "file2"))))78
(parse-result-opts result) ; => ((verbose . #t) (output . "out.txt"))79
(parse-result-args result) ; => ("file1" "file2")80
(parse-result-errors result) ; => () on success81
(parse-result-subcommand result)) ; => matched command or #f82
```84
Supported syntax:85
- `-v` short flag, `-vvv` repeated (counted), `-vf` combined flags86
- `-o value` or `-ovalue` short option with value87
- `--verbose` long flag, `--no-verbose` negated88
- `--output value` or `--output=value` long option with value89
- `--` stops option parsing; everything after is positional91
## Running Commands93
`run-command` parses arguments, handles errors, and dispatches to handlers. Automatically supports `--help` / `-h`.95
```scheme96
(run-command my-cli (cdr (command-line)))97
```99
- Prints help and exits 0 on `--help`100
- Prints errors and exits 1 on parse failure101
- Calls the handler with `(handler opts args)`102
- Dispatches to subcommand handler when matched104
## Help Generation106
```scheme107
;; Print help to stdout108
(print-help my-cmd)110
;; Get help as a string111
(define help-text (generate-help my-cmd))112
```114
Help output includes usage line, description, formatted options with defaults/choices/env vars, negatable syntax (`--[no-]color`), and subcommand list.116
## Common Patterns118
### Simple CLI Tool120
```scheme121
(import (sigil args))123
(define cli124
(command125
name: "greet"126
description: "Print a greeting"127
options: (list128
(option name: 'name short: #\n long: "name"129
value: "NAME" default: "World")130
(option name: 'loud short: #\l long: "loud"131
description: "Use uppercase"))132
handler: (lambda (opts args)133
(let ((greeting (format "Hello, ~a!" (alist-get 'name opts))))134
(display (if (alist-get 'loud opts)135
(string-upcase greeting)136
greeting))137
(newline)))))139
(run-command cli (cdr (command-line)))140
```142
### Subcommand-Based Tool144
```scheme145
(import (sigil args))147
(define cli148
(command149
name: "todo"150
description: "Task manager"151
subcommands: (list152
(command name: "add" description: "Add a task"153
handler: (lambda (opts args)154
(add-task (car args))))155
(command name: "list" description: "List tasks"156
options: (list157
(option name: 'all short: #\a long: "all"158
description: "Include completed"))159
handler: (lambda (opts args)160
(list-tasks all: (alist-get 'all opts)))))))162
(run-command cli (cdr (command-line)))163
```165
### Option with Env Var Fallback167
```scheme168
(option name: 'token long: "token" value: "TOKEN"169
env: "API_TOKEN" required: #t170
description: "API token (or set API_TOKEN)")171
;; --token <val> takes priority, falls back to $API_TOKEN172
```