add when isMainModule

This commit is contained in:
Christine Dodrill 2016-01-28 14:00:42 -08:00
parent 0ff50fcbe4
commit 4bec75a3b6
1 changed files with 45 additions and 44 deletions

View File

@ -38,54 +38,55 @@ proc writeVersion() =
echo gitlog echo gitlog
quit 1 quit 1
var when isMainModule:
totpRoot: string = getHomeDir() / "life" / "crypto" / "totp" var
command: string totpRoot: string = getHomeDir() / "life" / "crypto" / "totp"
args: seq[string] command: string
args: seq[string]
for kind, key, val in getopt(): for kind, key, val in getopt():
case kind case kind
of cmdArgument: of cmdArgument:
if command == nil: if command == nil:
command = key command = key
else:
args = args & key
of cmdLongOption, cmdShortOption:
case key
of "help", "h":
case command
of "code":
code.help()
of "newSecret":
newSecret.help()
of "ls":
ls.help()
else: else:
writeHelp() args = args & key
of "version", "v": writeVersion() of cmdLongOption, cmdShortOption:
of "root", "r": case key
totpRoot = val of "help", "h":
else: discard case command
of "code":
code.help()
of "newSecret":
newSecret.help()
of "ls":
ls.help()
else:
writeHelp()
of cmdEnd: assert(false) # cannot happen of "version", "v": writeVersion()
of "root", "r":
totpRoot = val
else: discard
if command == nil: of cmdEnd: assert(false) # cannot happen
writeHelp()
else: discard
if not existsDir(totpRoot): if command == nil:
totpRoot.createDir writeHelp()
echo "Created totp root " & totpRoot else: discard
case command if not existsDir(totpRoot):
of "code": totpRoot.createDir
code(totpRoot, args) echo "Created totp root " & totpRoot
of "newSecret":
newSecret(totpRoot, args) case command
of "ls": of "code":
ls(totpRoot, args) code(totpRoot, args)
else: of "newSecret":
echo "Command " & command & " not found." newSecret(totpRoot, args)
writeHelp() of "ls":
ls(totpRoot, args)
else:
echo "Command " & command & " not found."
writeHelp()