Skip to content

Repository files navigation

pflags

pflags is a drop-in replacement for Odin's core:flags, with support for conventional short (-f) and long (--flag) options.

Comparison against core:flags

pflags has exactly the same public API as Odin's core:flags. Only the .Unix parsing style differs:

  • Long flags use --flag strictly; -flag is never interpreted as a long flag.
  • Short flags can be declared with args:"short=f" and used as -f.
  • Short-option values may be attached (-oapp.out) or separate (-o app.out).
  • Boolean short flags can be bundled, so -abc is the same as -a -b -c.

Installation

Clone the library into your project's lib directory:

git clone --branch v0.1.0 --depth 1 \
  https://github.com/prnvbn/pflags.git lib/pflags

to add it as a submodule:

git submodule add https://github.com/prnvbn/pflags.git lib/pflags
git -C lib/pflags checkout v0.1.0

Usage

package main

import "core:fmt"
import "core:os"
import "lib:pflags"

Options :: struct {
	name:    string `args:"short=n" usage:"Name to greet."`,
	verbose: bool   `args:"short=v" usage:"Use a more enthusiastic greeting."`,
}

main :: proc() {
	options := Options{name = "world"}
	pflags.parse_or_exit(&options, os.args, .Unix)

	if options.verbose {
		fmt.printfln("Hello, %s!!!", options.name)
	} else {
		fmt.printfln("Hello, %s!", options.name)
	}
}

Map the lib collection when running or building your program:

odin run . -collection:lib=./lib -- --name Odin -v

Examples

See the examples directory for runnable programs using pflags.

Upstream

I am hoping this change is accepted into core:flags. This PR is proposing the same behaviour in the core pkg: odin-lang/Odin#7001

About

Drop-in replacement for Odin's core:flags, implementing POSIX/GNU-style -f/--flags

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages