getopty
POSIX compliant command line parser
Summary
| Latest Version | 1.0.0 |
|---|---|
| License | MIT or NCSA |
| CI Status | Failing |
| Downloads | 0 |
| Last Indexed | 2026-07-21 05:26 |
Tags
Authors
- Amun
Installation
nimble install getopty
choosenim install getopty
git clone https://github.com/amnr/getopty
OS Compatibility
| Platform | Linux | macOS | Windows | FreeBSD | OpenBSD | NetBSD | Android | iOS | WASM | Embedded |
|---|---|---|---|---|---|---|---|---|---|---|
| getopty | ✓ | ✓ | ✓ | - | - | - | - | - | - | - |
Source
| Repository | https://github.com/amnr/getopty |
|---|---|
| Homepage | https://github.com/amnr/getopty |
| Registry Source | nimble_official |
README
getopty
This module provides the standard POSIX compliant command line parser.
Supported Syntax
- Short options:
-a,-bcd,-e 5 - Long options:
--foo,--bar=baz - Arguments: everything that does not start with a
-or everything after standalone--.
Command Line Examples:
-a- short optiona-abcd- short optionsa,b,dandd-abcd(when-boption requires an argument) - short optiona, short optionbwith value ofcd--foo- long optionfoo--foo=bar- long optionfoowith value ofbar-a b -c --d e- short optionsaandc, long optiond, argumentsbande-a b -- -c --d e- short optiona, argumentsb,-c,--d,e
Install
nimble install https://github.com/amnr/getopty/
Usage
getopts is the iterator for iterating over command line options and arguments.
iterator getopts*(shortopts: string,
longopts: openArray[string] = [],
longopts_with_arg: openArray[string] = []): OptArg
Arguments:
- shortopts — the short options (one character) to be recognized; each character may be
followed by one colon to indicate the option has required argument
- longopts — list of long options to be recognized
- longopts_with_arg — list of long options that have required argument
Iterator returns OptArg of a kind:
- OPT_SHORT — short option
- OPT_LONG — long option
- OPT_ARGS — command line arguments (always the last OptArg returned)
- OPT_ERROR — parse error
Errors returned:
- OPTERR_EXTRA_ARG — option doesn't allow an argument
- OPTERR_INVALID — invalid (unknown) option
- OPTERR_REQ_ARG — option requires an argument
Example
import std/strformat
proc main() =
for opt in getopts "ab:cd":
case opt.kind
of OPT_SHORT, OPT_LONG:
if opt.name == "b":
echo fmt"Option '{opt.name}' with value ", opt.value
else:
echo fmt"Option '{opt.name}'"
of OPT_ARGS:
echo "Arguments: ", opt.args
of OPT_ERROR:
echo program_name(), ": ", $opt
quit QuitFailure
when isMainModule:
main()
Author
License
getopty is released under either:
Pick the one you prefer (or both).