Pyro

A dynamically-typed, garbage-collected scripting language.

Version 0.19.2

Examples


Simple Parser

Here's an example of a simple argument parser:

import std::args;

# Initialize a new argument parser.
let parser = args::ArgParser();
parser:helptext("Usage: foo...");
parser:version("1.0");

# Register a flag.
parser:flag("flag f");

# Register a string-valued option.
parser:option("name n");

# Register an integer-valued option and set its
# default value to 0.
parser:option("count c", 0, $i64);

# Parse the command line arguments.
if let result = parser:parse(); $is_err(result) {
    $exit("error: ${result}");
}

# Dump the parser to stdout.
echo parser;

Note that we can chain the calls to helptext(), version(), flag(), and option(), e.g.

var parser = args::ArgParser()
    :helptext("Usage: foo...")
    :version("1.0")
    :flag("flag f")
    :option("name n")
    :option("count c", 0, $i64);

Command Parser

Here's an example of an argument parser that registers a command:

import std::args;

# Callback function for the 'boo' command.
def cmd_boo(cmd_name, cmd_parser) {
    echo "---";
    echo cmd_parser;
    echo "---";
}

# Initialize a new argument parser.
let parser = args::ArgParser();
parser:helptext("Usage: foo...");
parser:version("1.0");

# Register a command 'boo'.
let cmd_parser = parser:command("boo");
cmd_parser:helptext("Usage: foo boo...");

# We can register flags and options for the command.
cmd_parser:flag("flag f");
cmd_parser:option("opt o");

# If the command is found, its callback will be called.
cmd_parser:callback(cmd_boo);

# Parse the command line arguments.
if let result = parser:parse(); $is_err(result) {
    $exit("error: ${result}");
}

# Dump the parser to stdout.
echo parser;

Note that we can chain the calls to the command parser's helptext(), flag(), option(), and callback() methods, e.g.

var cmd_parser = parser:command("boo")
    :helptext("Usage: foo boo...")
    :flag("flag f")
    :option("opt o")
    :callback(cmd_boo);