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
GetoptStatic methods:
parse(Array argv, Array specs, Array config?)Parameters:
argvis the command-line argument array,specsis an option-spec array, andconfigis optional parser configuration. Returns:Dict. Parses arguments into options and remaining positional arguments.schema(Array argv, Array schema, Array config?)Parameters:
argvis the command-line argument array,schemais an array of option schema dictionaries, andconfigis 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 exampleNumber,String,Boolean),required(Boolean),default,multiple, anddesc.Returns a dictionary:
ok(Boolean-like Number)options(Dict)argv(remaining positional args)error(String or null)errors(Array, forschema)usage(String, forschema)
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.