Merge branch 'master' of github.com:Araq/Nimrod

This commit is contained in:
Araq 2012-11-19 00:17:20 +01:00
commit 3738cee76e
2 changed files with 66 additions and 82 deletions

View file

@ -6,7 +6,7 @@ import db_sqlite, parseutils, strutils, times
type type
TTodo* = object of TObject TTodo* = object
## A todo object holding the information serialized to the database. ## A todo object holding the information serialized to the database.
id: int64 ## Unique identifier of the object in the id: int64 ## Unique identifier of the object in the
## database, use the getId() accessor to read it. ## database, use the getId() accessor to read it.
@ -17,7 +17,7 @@ type
## outside of this module, use the ## outside of this module, use the
## getModificationDate accessor. ## getModificationDate accessor.
TPagedParams* = object of TObject TPagedParams* = object
## Contains parameters for a query, initialize default values with ## Contains parameters for a query, initialize default values with
## initDefaults(). ## initDefaults().
pageSize*: int64 ## Lines per returned query page, -1 for pageSize*: int64 ## Lines per returned query page, -1 for

View file

@ -42,7 +42,7 @@ type
cmdGenerate # Add random rows to the database, for testing. cmdGenerate # Add random rows to the database, for testing.
cmdList # User wants to list contents. cmdList # User wants to list contents.
TParamConfig = object of TObject TParamConfig = object
# Structure containing the parsed options from the commandline. # Structure containing the parsed options from the commandline.
command: TCommand # Store the type of operation command: TCommand # Store the type of operation
addPriority: int # Only valid with cmdAdd, stores priority. addPriority: int # Only valid with cmdAdd, stores priority.
@ -59,6 +59,48 @@ proc initDefaults(params: var TParamConfig) =
params.addText = @[] params.addText = @[]
proc abort(message: string, value: int) =
# Simple wrapper to abort also displaying the help to the user.
stdout.write(USAGE)
quit(message, value)
template parseTodoIdAndSetCommand(newCommand: TCommand): stmt =
## Helper to parse a big todo identifier into todoId and set command.
try:
let numChars = val.parseBiggestInt(newId)
if numChars < 1: raise newException(EInvalidValue, "Empty string?")
result.command = newCommand
result.todoId = newId
except EOverflow:
raise newException(EInvalidValue, "Value $1 too big" % val)
template verifySingleCommand(actions: stmt): stmt =
## Helper to make sure only one command has been specified so far.
if specifiedCommand:
abort("Only one command can be specified at a time! (extra:$1)" % [key], 2)
else:
actions
specifiedCommand = true
proc parsePlusMinus(val: string, debugText: string): bool =
## Helper to process a plus or minus character from the commandline.
##
## Pass the string to parse and the type of parameter for debug errors.
## The processed parameter will be returned as true for a '+' and false for a
## '-'. The proc aborts with a debug message if the passed parameter doesn't
## contain one of those values.
case val
of "+":
return true
of "-":
return false
else:
abort("$1 parameter should be + or - but was '$2'." % [debugText, val], 4)
proc parseCmdLine(): TParamConfig = proc parseCmdLine(): TParamConfig =
## Parses the commandline. ## Parses the commandline.
## ##
@ -84,127 +126,69 @@ proc parseCmdLine(): TParamConfig =
if specifiedCommand and cmdAdd == result.command: if specifiedCommand and cmdAdd == result.command:
result.addText.add(key) result.addText.add(key)
else: else:
stdout.write(USAGE) abort("Argument ($1) detected without add command." % [key], 1)
quit("Argument ($1) detected without add command." % [key], 1)
of cmdLongOption, cmdShortOption: of cmdLongOption, cmdShortOption:
case normalize(key) case normalize(key)
of "help", "h": of "help", "h":
stdout.write(USAGE) stdout.write(USAGE)
quit(0) quit(0)
of "a": of "a":
if specifiedCommand: verifySingleCommand:
stdout.write(USAGE)
quit("Only one command can be specified at a time! ($1)" % [val], 2)
else:
result.command = cmdAdd result.command = cmdAdd
result.addPriority = val.parseInt result.addPriority = val.parseInt
specifiedCommand = true
of "c": of "c":
if specifiedCommand: verifySingleCommand:
stdout.write(USAGE) parseTodoIdAndSetCommand(cmdCheck)
quit("Only one command can be specified at a time! ($1)" % [val], 2)
else:
result.command = cmdCheck
let numChars = string(val).parseBiggestInt(newId)
if numChars < 1: raise newException(EInvalidValue, "Empty string?")
result.todoId = newId
specifiedCommand = true
of "u": of "u":
if specifiedCommand: verifySingleCommand:
stdout.write(USAGE) parseTodoIdAndSetCommand cmdUncheck
quit("Only one command can be specified at a time! ($1)" % [val], 2)
else:
result.command = cmdUncheck
let numChars = val.parseBiggestInt(newId)
if numChars < 1: raise newException(EInvalidValue, "Empty string?")
result.todoId = newId
specifiedCommand = true
of "d": of "d":
if specifiedCommand: verifySingleCommand:
stdout.write(USAGE)
quit("Only one command can be specified at a time! ($1)" % [val], 2)
else:
if "all" == val: if "all" == val:
result.command = cmdNuke result.command = cmdNuke
else: else:
result.command = cmdDelete parseTodoIdAndSetCommand cmdDelete
let numChars = val.parseBiggestInt(newId)
if numChars < 1:
raise newException(EInvalidValue, "Empty string?")
result.todoId = newId
specifiedCommand = true
of "g": of "g":
if specifiedCommand: verifySingleCommand:
stdout.write(USAGE)
quit("Only one command can be specified at a time! ($1)" % [val], 2)
else:
if val.len > 0: if val.len > 0:
stdout.write(USAGE) abort("Unexpected value '$1' for switch l." % [val], 3)
quit("Unexpected value '$1' for switch l." % [val], 3)
result.command = cmdGenerate result.command = cmdGenerate
specifiedCommand = true
of "l": of "l":
if specifiedCommand: verifySingleCommand:
stdout.write(USAGE)
quit("Only one command can be specified at a time! ($1)" % [val], 2)
else:
if val.len > 0: if val.len > 0:
stdout.write(USAGE) abort("Unexpected value '$1' for switch l." % [val], 3)
quit("Unexpected value '$1' for switch l." % [val], 3)
result.command = cmdList result.command = cmdList
specifiedCommand = true
of "p": of "p":
usesListParams = true usesListParams = true
case val result.listParams.priorityAscending = parsePlusMinus(val, "Priority")
of "+":
result.listParams.priorityAscending = true
of "-":
result.listParams.priorityAscending = false
else:
stdout.write(USAGE)
quit("Priority parameter ($1) should be + or |." % [val], 4)
of "m": of "m":
usesListParams = true usesListParams = true
case val result.listParams.dateAscending = parsePlusMinus(val, "Date")
of "+":
result.listParams.dateAscending = true
of "-":
result.listParams.dateAscending = false
else:
stdout.write(USAGE)
quit("Date parameter ($1) should be + or |." % [val], 4)
of "t": of "t":
usesListParams = true usesListParams = true
if val.len > 0: if val.len > 0:
stdout.write(USAGE) abort("Unexpected value '$1' for switch t." % [val], 5)
quit("Unexpected value '$1' for switch t." % [val], 5)
result.listParams.showChecked = true result.listParams.showChecked = true
of "z": of "z":
usesListParams = true usesListParams = true
if val.len > 0: if val.len > 0:
stdout.write(USAGE) abort("Unexpected value '$1' for switch z." % [val], 5)
quit("Unexpected value '$1' for switch z." % [val], 5)
result.listParams.showUnchecked = false result.listParams.showUnchecked = false
else: else:
stdout.write(USAGE) abort("Unexpected option '$1'." % [key], 6)
quit("Unexpected option '$1'." % [key], 6)
of cmdEnd: of cmdEnd:
break break
except EInvalidValue: except EInvalidValue:
stdout.write(USAGE) abort("Invalid integer value '$1' for parameter '$2'." % [val, key], 7)
quit("Invalid int value '$1' for parameter '$2'." % [val, key], 7)
if not specifiedCommand: if not specifiedCommand:
stdout.write(USAGE) abort("Didn't specify any command.", 8)
quit("Didn't specify any command.", 8)
if cmdAdd == result.command and result.addText.len < 1: if cmdAdd == result.command and result.addText.len < 1:
stdout.write(USAGE) abort("Used the add command, but provided no text/description.", 9)
quit("Used the add command, but provided no text/description.", 9)
if usesListParams and cmdList != result.command: if usesListParams and cmdList != result.command:
stdout.write(USAGE) abort("Used list options, but didn't specify the list command.", 10)
quit("Used list options, but didn't specify the list command.", 10)
proc generateDatabaseRows(conn: TDbConn) = proc generateDatabaseRows(conn: TDbConn) =