a

argus

v0.0.1library

A builder-style command-line argument parsing library for Odin

0 stars0 forksMITupdated 5 hours ago
Open repo

argus

A builder-style command-line argument parser for Odin, in the spirit of Rust's clap. Supports subcommands and a fluent builder API, neither of which core:flags provides.

If you need a flat, quick CLI, use core:flags. Use argus when you need subcommands or prefer building the arg spec in code.

Status

v0.0.1. Scope is "core:flags parity on flat flags, plus one level of subcommands, via a builder API". See TODO for the roadmap.

Parsing

Odin-style only:

  • -name sets a bool flag.
  • -name:value sets a typed option (string, int, float).
  • The first bare token matching a registered subcommand switches parsing to that subcommand. Max depth is one level.
  • Other bare tokens fill registered positionals in order.
  • -help and -h request help.

No Unix-style --flag value, no short clustering.

Usage

package main

import cli "path/to/argus/cli"
import "core:os"

main :: proc() {
	cmd := cli.command("mytool", "does a thing")
	cli.flag(&cmd, "verbose", "verbose", "verbose output")
	cli.opt(&cmd, "output", "output", .String, "output file", default = "out.txt")

	build := cli.subcommand(&cmd, "build", "builds the project")
	cli.flag(build, "release", "release", "build in release mode")
	defer cli.command_destroy(&cmd)

	m := cli.parse_or_exit(cmd, os.args[1:], os.to_writer(os.stdout))
	defer cli.destroy(&m)

	if cli.has_subcommand(m, "build") {
		release := cli.get_bool(m.sub^, "release")
		if release {
			// build in release mode
		}
	}
}

Memory

parse creates and owns a virtual.Arena inside the returned Matches. Everything the result needs lives in that arena. Call cli.destroy(&m) once when done. The Command spec is caller-owned; free it with cli.command_destroy(&cmd).

Errors and exit codes

parse returns (Matches, Error) for callers that want to handle errors themselves. parse_or_exit prints and exits: 0 on help, 2 on any usage or validation error.

Build and test

odin test cli
odin build examples/mytool -out:/tmp/mytool

License

MIT. See LICENSE.

Package Info
Version
v0.0.1
License
MIT
Author
@vetr0s
Type
library
Forks
0
Created
6 hours ago
Updated
5 hours ago
Health
maintained
has releases
has readme
has license
Activity
last 56 weeks