Pyro

A scripting language for people who enjoy the simpler things in life.

Version 0.9.35

Examples


Simple Parser

Here's an example of a simple argument parser:

import std::args;

# Initialize a new argument parser.
var 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.
parser:parse();

# Dump the parser to stdout.
parser:print();

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

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

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 "---";
    cmd_parser:print();
    echo "---";
}

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

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

# We can register flags and options on 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.
parser:parse();

# Dump the parser to stdout.
parser:print();

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);