pflags is a drop-in replacement for Odin's
core:flags, with
support for conventional short (-f) and long (--flag) options.
pflags has exactly the same public API as Odin's
core:flags. Only
the .Unix parsing style differs:
- Long flags use
--flagstrictly;-flagis 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
-abcis the same as-a -b -c.
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
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 -vSee the examples directory for runnable programs using pflags.
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