mirror of
https://github.com/amnr/getopty
synced 2026-01-14 13:31:43 +00:00
No description
| src | ||
| tests/parsing | ||
| .gitignore | ||
| getopty.nimble | ||
| LICENSE-MIT.txt | ||
| LICENSE-NCSA.txt | ||
| Makefile | ||
| README.md | ||
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 argumentlongopts— list of long options to be recognizedlongopts_with_arg— list of long options that have required argument
Iterator returns OptArg of a kind:
OPT_SHORT— short optionOPT_LONG— long optionOPT_ARGS— command line arguments (always the lastOptArgreturned)OPT_ERROR— parse error
Errors returned:
OPTERR_EXTRA_ARG— option doesn't allow an argumentOPTERR_INVALID— invalid (unknown) optionOPTERR_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).