Skip to content

Instantly share code, notes, and snippets.

@nervecenter
Last active October 25, 2025 01:56
Show Gist options
  • Select an option

  • Save nervecenter/9fc10e3ef521cbc296ac8b4c45e6635d to your computer and use it in GitHub Desktop.

Select an option

Save nervecenter/9fc10e3ef521cbc296ac8b4c45e6635d to your computer and use it in GitHub Desktop.
A simple set of CLI argument parsing procedures using std/parseopt that covers the majority of basic cases for parsing CLI arguments.
# by Chris Collazo
# Released to the public domain
import std/parseopt
# `short` is optional
proc get_option*(oparser: var OptParser, name: string, default: string, short: string = ""): string =
for kind, key, val in oparser.getopt():
if kind == cmdLongOption and key == name:
return val
elif short != "" and kind == cmdShortOption and key == short:
return val
return default
# For options which are only short
proc get_option_short*(oparser: var OptParser, short: string, default: string): string =
for kind, key, val in oparser.getopt():
if kind == cmdShortOption and key == short:
return val
return default
# `short` is optional
proc get_flag*(oparser: var OptParser, name: string, short: string = ""): bool =
for kind, key, val in oparser.getopt():
if kind == cmdLongOption and key == name:
return true
elif short != "" and kind == cmdShortOption and key == short:
return true
return false
# For flags which are only short
proc get_flag_short*(oparser: var OptParser, short: string): bool =
for kind, key, val in oparser.getopt():
if kind == cmdShortOption and key == short:
return true
return false
# For positional args
proc get_arg_at_index*(oparser: var OptParser, index: int): string =
var i = 0
for kind, key, val in oparser.getopt():
if i == index and kind == cmdArgument:
return key
i += 1
return ""
# First argument, which is often a command, is at position 0
proc command_was*(oparser: var OptParser, cmd: string): bool =
oparser.get_arg_at_index(0) == cmd
# Second argument, which is sometimes a subcommand, is at position 1
proc subcommand_was*(oparser: var OptParser, cmd: string): bool =
oparser.get_arg_at_index(1) == cmd
when isMainModule:
import std/strformat
import std/strutils
var cli = init_opt_parser()
echo &"""Command was `foo`?: {cli.command_was("foo")}"""
echo &"""Subcommand was `bar`?: {cli.subcommand_was("bar")}"""
echo &"""Option `switch` was: {cli.get_option("switch", "none", short = "s")}"""
echo &"""Option `quality` was: {cli.get_option("quality", "100", short = "q").parse_int()}"""
echo &"""Flag `convert` was: {cli.get_flag("convert", short = "c")}"""
echo &"""Option `t` was: {cli.get_option_short("t", "none")}"""
echo &"""Flag a was: {cli.get_flag_short("a")}"""
echo &"""Flag e was: {cli.get_flag_short("e")}"""
echo &"""Flag i was: {cli.get_flag_short("i")}"""
echo &"""Flag o was: {cli.get_flag_short("o")}"""
echo &"""Flag u was: {cli.get_flag_short("u")}"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment