flags Module
The flags module provides a simple command/flag system for Harneet programs.
- Nested commands (e.g.,
tool user add) - Per-command flags with optional short aliases
- Global flags that are available to all commands
- Interleaved command selection (commands can appear anywhere in argv)
- Ancestor-chain flag lookup (current → parent → … → global)
- Unknown flags produce errors (e.g.,
unknown flag: --foo) --terminator makes the rest positional
Quick Start
API
flags.Command(name, usage [, alias])– Define a top-level command.flags.Subcommand(parent, name, usage [, alias])– Define a nested command.flags.String(name, default, usage [, alias])– Define a string flag.flags.Bool(name, default, usage [, alias])– Define a boolean flag.flags.Int(name, default, usage [, alias])– Define an integer flag.flags.Parse(argv array)– Parse args. Commands can be interleaved with flags; the command path is resolved respecting parent relationships.flags.Selected()– Returns the selected command path as an array.flags.Args()– Returns positional arguments for the selected command.flags.GetString/Bool/Int(name)– Get parsed values. Lookup order is current command, then parent(s), then global.flags.Reset()– Reset all definitions and parsed values (handy for tests).
Dispatch (Return-Dispatch pattern)
flags.Handle(path array[string], tag any)– Register a handler tag for a command path. The tag is any value your program can switch on.flags.Run(argv array)– Parse and return(selectedPath array, args array, handlerTag any, error). Your program performs the call based onhandlerTag.
Usage Patterns
-
Define your command tree upfront, then declare flags for the current context. Global flags are declared at the global context (no current command selected).
-
Populate
argvfrom your environment if needed (e.g., a futureos.Args()); for now our examples manually createargvarrays. -
Short alias flags support
-x valueand-x=valueforms. Booleans support--bool(sets true) and--bool=false.
Examples
See examples/flags/: - nested_commands_basic.ha – Command path selection and command-local flags - alias_and_fallback.ha – Short aliases and fallback to global flags - dispatch_demo.ha – Register tags with flags.Handle, parse with flags.Run, then dispatch in user code