Skip to content

Args

Parse command-line arguments.

Functions

Args.get(name: string) -> string

Returns the value of a named flag. Supports --name=value and --name value formats.

wyn
host = Args.get("host")     // --host=localhost or --host localhost
port = Args.get("port")     // --port=8080 or --port 8080
print("Connecting to " + host + ":" + port)

Returns "" if the flag is not present.

Args.has(name: string) -> bool

Returns true if a flag is present.

wyn
if Args.has("verbose") {
    print("Verbose mode enabled")
}

// Also matches single-letter flags
if Args.has("v") {    // matches -v
    print("Verbose")
}

Args.positional() -> array

Returns non-flag arguments as an array.

wyn
// wyn run app.wyn -- file1.txt file2.txt --verbose
files = Args.positional()   // ["file1.txt", "file2.txt"]
for f in files {
    print("Processing: " + f)
}

Example: CLI Tool

wyn
name = Args.get("name")
count = Args.get("count")
loud = Args.has("loud")

if name == "" {
    print("Usage: greet --name <name> [--count N] [--loud]")
} else {
    n = if count != "" { count.to_int() } else { 1 }
    for i in 0..n {
        msg = "Hello, " + name + "!"
        if loud {
            print(msg.upper())
        } else {
            print(msg)
        }
    }
}

TIP

Arguments are only available when running the compiled binary directly. wyn run does not forward arguments after -- to the program.

See Also

MIT License - v1.21.0