All news

Published

Argparse package now available

The new argparse package makes it easy to build command-line applications with Jik. It supports positional arguments, long and short options, option values, and flags.

Import it with use "pkg/argparse", define the arguments your program accepts, and parse any argument vector.

use "pkg/argparse"

func main(args):
    ap := argparse::new(_)
    argparse::add_positional(ap, "input", "Input file.")

    output := argparse::add_option(ap, "--output", "-o", "Output file.")
    argparse::add_option_positional(output, "file", "Output path.")

    argparse::add_option(ap, "--verbose", "-v", "Show extra output.")
    
    args := ["in.txt", "--output", "out.txt", "-v"]
    result := must argparse::parse(ap, args, _)

    input := result.positionals["input"]?
    output_path := result.options["--output"]?["file"]?
    if result.options["--verbose"] is Some:
        println("Reading ", input)
        println("Writing to ", output_path)
    end
end