Skip to content

Commit

Permalink
Update to Nimrod 0.9.4: use parseopt2 and typeDesc!
Browse files Browse the repository at this point in the history
  • Loading branch information
fenekku committed May 11, 2014
1 parent abf7126 commit 514437e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if times != 0:
if debug:
echo("Debugging enabled")
```

**On the command line**
Expand Down Expand Up @@ -137,5 +137,4 @@ Design
TODO and Contribution
---------------------

- Move to Nimrod 0.9.4
- Test out and see what needs to be added.
4 changes: 2 additions & 2 deletions commandeer.babel
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[Package]
name = "commandeer"
version = "0.1.2"
version = "0.2.0"
author = "Guillaume Viger"
description = "A small command line parsing DSL (domain specific language)"
license = "MIT"

InstallFiles = "commandeer.nim"

[Deps]
Requires: "nimrod >= 0.9.2"
Requires: "nimrod >= 0.9.4"
48 changes: 24 additions & 24 deletions commandeer.nim
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@

import parseopt
import parseopt2
import strutils
import tables


var
arguments = newSeq[ string ]()
argumentList = newSeq[ string ]()
shortOptions = initTable[string, string](32)
longOptions = initTable[string, string](32)
argumentIndex = 0
error : ref E_Base

## String conversion
proc convert(s : string, ofType : char): char =
proc convert(s : string, t : char): char =
result = s[0]
proc convert(s : string, ofType : int): int =
proc convert(s : string, t : int): int =
result = parseInt(s)
proc convert(s : string, ofType : float): float =
proc convert(s : string, t : float): float =
result = parseFloat(s)
proc convert(s : string, ofType : bool): bool =
proc convert(s : string, t : bool): bool =
## will accept "yes", "true" as true values
if s == "":
## the only way we get an empty string here is because of a key
Expand All @@ -27,41 +27,41 @@ proc convert(s : string, ofType : bool): bool =
result = true
else:
result = parseBool(s)
proc convert(s : string, ofType : string): string =
proc convert(s : string, t : string): string =
result = s.strip


template argument*(identifier : expr, ofType : expr): stmt {.immediate.} =
bind arguments
template argument*(identifier : expr, t : typeDesc): stmt {.immediate.} =
bind argumentList
bind argumentIndex
bind convert
bind error

var identifier : ofType
var identifier : t

if arguments.len <= argumentIndex:
error = newException(E_Base, "Not enough command-line arguments")
if argumentList.len <= argumentIndex:
error = newException(E_Base, "Not enough command-line argumentList")
else:
var typeVar : ofType
var typeVar : t
try:
identifier = convert(arguments[argumentIndex], typeVar)
identifier = convert(argumentList[argumentIndex], typeVar)
inc(argumentIndex)
except EInvalidValue:
error = getCurrentException()


template option*(identifier : expr, ofType : expr, longName : string,
template option*(identifier : expr, t : typeDesc, longName : string,
shortName : string): stmt {.immediate.} =
bind shortOptions
bind longOptions
bind convert
bind error
bind tables

var identifier : ofType
var identifier : t

block:
var typeVar : ofType
var typeVar : t
if tables.hasKey(longOptions, longName):
try:
identifier = convert(tables.mget(longOptions, longName), typeVar)
Expand All @@ -86,20 +86,20 @@ template exitoption*(longName, shortName, msg : string): stmt =


template commandLine*(statements : stmt): stmt {.immediate.} =
bind arguments
bind argumentList
bind shortOptions
bind longOptions
bind error
bind parseopt
bind parseopt2
bind tables

for kind, key, value in parseopt.getopt():
for kind, key, value in parseopt2.getopt():
case kind
of parseopt.cmdArgument:
arguments.add(key)
of parseopt.cmdLongOption:
of parseopt2.cmdArgument:
argumentList.add(key)
of parseopt2.cmdLongOption:
tables.add(longOptions, key, value)
of parseopt.cmdShortOption:
of parseopt2.cmdShortOption:
tables.add(shortOptions, key, value)
else:
discard
Expand Down

0 comments on commit 514437e

Please sign in to comment.