std/getopt

Standard Library documentation

Parse command line arguments.

Module

Name
std/getopt
Area
Standard Library
Source
modules/std/getopt.zzm

NAME

std/getopt - Parse command line arguments.

SYNOPSIS

  from std/getopt import Getopt;

  function __main__ (argv) {
    let parsed := Getopt.parse(
      argv,
      [ "help|h", "verbose|v", "count|c=i", "name|n=s" ]
    );

    if ( not parsed{ok} ) {
      say( parsed{error} );
      return 2;
    }

    let opts := parsed{options};
    let rest := parsed{argv};

    if ( opts{help} ) {
      say( "usage: tool [--count N] [--name STR] args..." );
      return 0;
    }

    say( "remaining args = " + rest.length() );
    return 0;
  }

IMPLEMENTATION SUPPORT

This module is supported by all implementations of ZuzuScript.

DESCRIPTION

This module parses command-line option arrays for ZuzuScript programs.

Use Getopt.parse(argv, specs, config?) and pass the argv array that your __main__ function receives.

It intentionally does not read a host process argument global.

EXPORTS

Classes

  • Getopt

    Static methods:

    • parse(Array argv, Array specs, Array config?)

      Parameters: argv is the command-line argument array, specs is an option-spec array, and config is optional parser configuration. Returns: Dict. Parses arguments into options and remaining positional arguments.

    • schema(Array argv, Array schema, Array config?)

      Parameters: argv is the command-line argument array, schema is an array of option schema dictionaries, and config is optional parser configuration. Returns: Dict. Parses arguments using structured option metadata and produces errors and usage text.

      Schema entries use fields such as name, short, type (for example Number, String, Boolean), required (Boolean), default, multiple, and desc.

      Returns a dictionary:

      • ok (Boolean-like Number)
      • options (Dict)
      • argv (remaining positional args)
      • error (String or null)
      • errors (Array, for schema)
      • usage (String, for schema)

COPYRIGHT AND LICENCE

std/getopt is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.