Created
January 4, 2023 23:36
-
-
Save blacknon/65db417937e28a353c3a92b86e96c5a4 to your computer and use it in GitHub Desktop.
nimでshort optionを書いているコマンドラインをパースしてseqにするサンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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