Skip to content

Instantly share code, notes, and snippets.

@blacknon
Created January 4, 2023 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blacknon/65db417937e28a353c3a92b86e96c5a4 to your computer and use it in GitHub Desktop.
Save blacknon/65db417937e28a353c3a92b86e96c5a4 to your computer and use it in GitHub Desktop.
nimでshort optionを書いているコマンドラインをパースしてseqにするサンプルコード
import std/parseopt
import hashes
import sets
import sequtils
var p = initOptParser("-ab -c:123 -d xyz -e:5 --foo --bar=20 file.txt")
var cmdline: OrderedSet[string] = initOrderedSet[string]()
while true:
p.next()
case p.kind
of cmdEnd: break
of cmdShortOption, cmdLongOption:
if p.val == "":
echo "Option: ", p.key
cmdline.incl("-" & p.key)
else:
echo "Option and value: ", p.key, ", ", p.val
cmdline.incl("-" & p.key)
cmdline.incl(p.val)
of cmdArgument:
echo "Argument: ", p.key
cmdline.incl(p.key)
echo toSeq(cmdline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment