ARGS
Command Line Argument Parsing in Vint
Vint now provides built-in support for parsing command line arguments, making it easy to build CLI applications.
Basic Usage
Get All Arguments
// Get all command line arguments as an array let allArgs = args() print(allArgs) // ["input.txt", "--verbose", "--output", "result.txt"]
Using the CLI Module
import cli // Get all arguments (same as args()) let cliArgs = cli.args() // Get only positional arguments (non-flags) let positional = cli.getPositional() print(positional) // ["input.txt", "extra_file.txt"] // Get flags as a dictionary let flags = cli.getFlags() print(flags) // {verbose: true, output: "result.txt"} // Check if a specific flag is present if (cli.hasArg("--verbose")) { print("Verbose mode enabled") } // Get the value of a specific flag let outputFile = cli.getArgValue("--output") if (outputFile) { print("Output file:", outputFile) }
Supported Flag Formats
--flag- Boolean flags (sets to true)--flag value- Flag with value (space-separated)--flag=value- Flag with value (equals-separated)-v- Short flags (boolean)
Complete Example
// example: vint myapp.vint input.txt --output result.txt --verbose --format json import cli // Check for help if (cli.hasArg("--help")) { print("Usage: myapp <input> [options]") exit(0) } // Get input file from positional arguments let positional = cli.getPositional() let inputFile = positional[0] || "stdin" // Get options let outputFile = cli.getArgValue("--output") || "stdout" let format = cli.getArgValue("--format") || "txt" let verbose = cli.hasArg("--verbose") if (verbose) { print("Processing", inputFile, "->", outputFile, "in", format, "format") }
Available Functions
Built-in Functions
args()- Returns array of all command line arguments
CLI Module Functions
cli.args()- Same asargs()cli.getPositional()- Returns array of positional argumentscli.getFlags()- Returns dictionary of flagscli.hasArg(flag)- Checks if flag is presentcli.getArgValue(flag)- Gets value of flagcli.prompt(message)- Prompts user for inputcli.confirm(message)- Prompts for yes/no confirmationcli.execCommand(cmd)- Executes shell commandcli.cliExit(code)- Exits with status code
Notes
- Positional arguments are those that don't start with
-or-- - Flag values are automatically detected (space or equals separated)
- Boolean flags are set to
truewhen present - The
args()builtin andcli.args()return the same result - All functions handle quotes in arguments properly