Improves the style of some examples.

* Compacts all imports in a single line.
* Cleans whitespace before colons.
* Uses cmd abbreviation for command line enums.
* Replaces if/elif/else chain with case.
This commit is contained in:
Grzegorz Adam Hankiewicz 2012-11-13 20:57:36 +01:00
commit 2faf34b643
4 changed files with 46 additions and 58 deletions

View file

@ -1,8 +1,6 @@
# Implements a command line interface against the backend. # Implements a command line interface against the backend.
import backend import backend, parseopt, strutils
import parseopt
import strutils
const const
USAGE = """nimcalculator - Nimrod cross platform calculator USAGE = """nimcalculator - Nimrod cross platform calculator
@ -19,12 +17,12 @@ If no options are used, an interactive mode is entered.
""" """
type type
TAction = enum # The possible types of operation TCommand = enum # The possible types of operation
rtParams, # Two valid parameters were provided cmdParams, # Two valid parameters were provided
rtInteractive # No parameters were provided, run interactive mode cmdInteractive # No parameters were provided, run interactive mode
TParamConfig = object of TObject TParamConfig = object of TObject
action: TAction # store the type of operation action: TCommand # store the type of operation
paramA, paramB: int # possibly store the valid parameters paramA, paramB: int # possibly store the valid parameters
@ -37,9 +35,9 @@ proc parseCmdLine(): TParamConfig =
hasA = false hasA = false
hasB = false hasB = false
p = initOptParser() p = initOptParser()
key, val : TaintedString key, val: TaintedString
result.action = rtInteractive # By default presume interactive mode. result.action = cmdInteractive # By default presume interactive mode.
try: try:
while true: while true:
next(p) next(p)
@ -70,7 +68,7 @@ proc parseCmdLine(): TParamConfig =
quit("Invalid value " & val & " for parameter " & key, 3) quit("Invalid value " & val & " for parameter " & key, 3)
if hasA and hasB: if hasA and hasB:
result.action = rtParams result.action = cmdParams
elif hasA or hasB: elif hasA or hasB:
stdout.write(USAGE) stdout.write(USAGE)
quit("Error: provide both A and B to operate in param mode", 4) quit("Error: provide both A and B to operate in param mode", 4)
@ -102,7 +100,7 @@ proc interactiveMode() =
when isMainModule: when isMainModule:
## Main entry point. ## Main entry point.
let opt = parseCmdLine() let opt = parseCmdLine()
if rtParams == opt.action: if cmdParams == opt.action:
echo("Param mode: $1 + $2 = $3" % [$opt.paramA, $opt.paramB, echo("Param mode: $1 + $2 = $3" % [$opt.paramA, $opt.paramB,
$backend.myAdd(opt.paramA, opt.paramB)]) $backend.myAdd(opt.paramA, opt.paramB)])
else: else:

View file

@ -2,10 +2,7 @@
# #
# Most procs dealing with a TDbConn object may raise an EDb exception. # Most procs dealing with a TDbConn object may raise an EDb exception.
import db_sqlite import db_sqlite, parseutils, strutils, times
import parseutils
import strutils
import times
type type

View file

@ -1,9 +1,6 @@
# Tests the backend code. # Tests the backend code.
import backend import backend, db_sqlite, strutils, times
import db_sqlite
import strutils
import times
proc showPagedResults(conn: TDbConn; params: TPagedParams) = proc showPagedResults(conn: TDbConn; params: TPagedParams) =
@ -58,7 +55,7 @@ proc dumTest() =
discard clonedTodo.update(conn) discard clonedTodo.update(conn)
assert(clonedTodo.text == todo.text, "Should be equal") assert(clonedTodo.text == todo.text, "Should be equal")
var params : TPagedParams var params: TPagedParams
params.initDefaults params.initDefaults
conn.showPagedResults(params) conn.showPagedResults(params)
conn.deleteTodo(todo.getId) conn.deleteTodo(todo.getId)

View file

@ -1,12 +1,6 @@
# Implements a command line interface against the backend. # Implements a command line interface against the backend.
import backend import backend, db_sqlite, os, parseopt, parseutils, strutils, times
import db_sqlite
import os
import parseopt
import parseutils
import strutils
import times
const const
USAGE = """nimtodo - Nimrod cross platform todo manager USAGE = """nimtodo - Nimrod cross platform todo manager
@ -39,20 +33,20 @@ Examples:
""" """
type type
TCommand = enum # The possible types of commands TCommand = enum # The possible types of commands
commandAdd # The user wants to add a new todo entry. cmdAdd # The user wants to add a new todo entry.
commandCheck # User wants to check a todo entry. cmdCheck # User wants to check a todo entry.
commandUncheck # User wants to uncheck a todo entry. cmdUncheck # User wants to uncheck a todo entry.
commandDelete # User wants to delete a single todo entry. cmdDelete # User wants to delete a single todo entry.
commandNuke # User wants to purge all database entries. cmdNuke # User wants to purge all database entries.
commandGenerate # Add random rows to the database, for testing. cmdGenerate # Add random rows to the database, for testing.
commandList # User wants to list contents. cmdList # User wants to list contents.
TParamConfig = object of TObject TParamConfig = object of TObject
# 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 commandAdd, stores priority. addPriority: int # Only valid with cmdAdd, stores priority.
addText: seq[string] # Only valid with commandAdd, stores todo text. addText: seq[string] # Only valid with cmdAdd, stores todo text.
todoId: int64 # The todo id for operations like check or delete. todoId: int64 # The todo id for operations like check or delete.
listParams: TPagedParams # Uses the backend structure directly for params. listParams: TPagedParams # Uses the backend structure directly for params.
@ -87,7 +81,7 @@ proc parseCmdLine(): TParamConfig =
case p.kind case p.kind
of cmdArgument: of cmdArgument:
if specifiedCommand and commandAdd == result.command: if specifiedCommand and cmdAdd == result.command:
result.addText.add(key) result.addText.add(key)
else: else:
stdout.write(USAGE) stdout.write(USAGE)
@ -102,7 +96,7 @@ proc parseCmdLine(): TParamConfig =
stdout.write(USAGE) stdout.write(USAGE)
quit("Only one command can be specified at a time! ($1)" % [val], 2) quit("Only one command can be specified at a time! ($1)" % [val], 2)
else: else:
result.command = commandAdd result.command = cmdAdd
result.addPriority = val.parseInt result.addPriority = val.parseInt
specifiedCommand = true specifiedCommand = true
of "c": of "c":
@ -110,7 +104,7 @@ proc parseCmdLine(): TParamConfig =
stdout.write(USAGE) stdout.write(USAGE)
quit("Only one command can be specified at a time! ($1)" % [val], 2) quit("Only one command can be specified at a time! ($1)" % [val], 2)
else: else:
result.command = commandCheck result.command = cmdCheck
let numChars = string(val).parseBiggestInt(newId) let numChars = string(val).parseBiggestInt(newId)
if numChars < 1: raise newException(EInvalidValue, "Empty string?") if numChars < 1: raise newException(EInvalidValue, "Empty string?")
result.todoId = newId result.todoId = newId
@ -120,7 +114,7 @@ proc parseCmdLine(): TParamConfig =
stdout.write(USAGE) stdout.write(USAGE)
quit("Only one command can be specified at a time! ($1)" % [val], 2) quit("Only one command can be specified at a time! ($1)" % [val], 2)
else: else:
result.command = commandUncheck result.command = cmdUncheck
let numChars = val.parseBiggestInt(newId) let numChars = val.parseBiggestInt(newId)
if numChars < 1: raise newException(EInvalidValue, "Empty string?") if numChars < 1: raise newException(EInvalidValue, "Empty string?")
result.todoId = newId result.todoId = newId
@ -131,9 +125,9 @@ proc parseCmdLine(): TParamConfig =
quit("Only one command can be specified at a time! ($1)" % [val], 2) quit("Only one command can be specified at a time! ($1)" % [val], 2)
else: else:
if "all" == val: if "all" == val:
result.command = commandNuke result.command = cmdNuke
else: else:
result.command = commandDelete result.command = cmdDelete
let numChars = val.parseBiggestInt(newId) let numChars = val.parseBiggestInt(newId)
if numChars < 1: if numChars < 1:
raise newException(EInvalidValue, "Empty string?") raise newException(EInvalidValue, "Empty string?")
@ -147,7 +141,7 @@ proc parseCmdLine(): TParamConfig =
if val.len > 0: if val.len > 0:
stdout.write(USAGE) stdout.write(USAGE)
quit("Unexpected value '$1' for switch l." % [val], 3) quit("Unexpected value '$1' for switch l." % [val], 3)
result.command = commandGenerate result.command = cmdGenerate
specifiedCommand = true specifiedCommand = true
of "l": of "l":
if specifiedCommand: if specifiedCommand:
@ -157,22 +151,24 @@ proc parseCmdLine(): TParamConfig =
if val.len > 0: if val.len > 0:
stdout.write(USAGE) stdout.write(USAGE)
quit("Unexpected value '$1' for switch l." % [val], 3) quit("Unexpected value '$1' for switch l." % [val], 3)
result.command = commandList result.command = cmdList
specifiedCommand = true specifiedCommand = true
of "p": of "p":
usesListParams = true usesListParams = true
if "+" == val: case val
of "+":
result.listParams.priorityAscending = true result.listParams.priorityAscending = true
elif "-" == val: of "-":
result.listParams.priorityAscending = false result.listParams.priorityAscending = false
else: else:
stdout.write(USAGE) stdout.write(USAGE)
quit("Priority parameter ($1) should be + or |." % [val], 4) quit("Priority parameter ($1) should be + or |." % [val], 4)
of "m": of "m":
usesListParams = true usesListParams = true
if "+" == val: case val
of "+":
result.listParams.dateAscending = true result.listParams.dateAscending = true
elif "-" == val: of "-":
result.listParams.dateAscending = false result.listParams.dateAscending = false
else: else:
stdout.write(USAGE) stdout.write(USAGE)
@ -202,11 +198,11 @@ proc parseCmdLine(): TParamConfig =
stdout.write(USAGE) stdout.write(USAGE)
quit("Didn't specify any command.", 8) quit("Didn't specify any command.", 8)
if commandAdd == result.command and result.addText.len < 1: if cmdAdd == result.command and result.addText.len < 1:
stdout.write(USAGE) stdout.write(USAGE)
quit("Used the add command, but provided no text/description.", 9) quit("Used the add command, but provided no text/description.", 9)
if usesListParams and commandList != result.command: if usesListParams and cmdList != result.command:
stdout.write(USAGE) stdout.write(USAGE)
quit("Used list options, but didn't specify the list command.", 10) quit("Used list options, but didn't specify the list command.", 10)
@ -336,12 +332,12 @@ when isMainModule:
let conn = openDatabase(dbPath) let conn = openDatabase(dbPath)
try: try:
case opt.command case opt.command
of commandAdd: addTodo(conn, opt.addPriority, opt.addText) of cmdAdd: addTodo(conn, opt.addPriority, opt.addText)
of commandCheck: setTodoCheck(conn, opt.todoId, true) of cmdCheck: setTodoCheck(conn, opt.todoId, true)
of commandUncheck: setTodoCheck(conn, opt.todoId, false) of cmdUncheck: setTodoCheck(conn, opt.todoId, false)
of commandDelete: deleteOneTodo(conn, opt.todoId) of cmdDelete: deleteOneTodo(conn, opt.todoId)
of commandNuke: deleteAllTodos(conn) of cmdNuke: deleteAllTodos(conn)
of commandGenerate: generateDatabaseRows(conn) of cmdGenerate: generateDatabaseRows(conn)
of commandList: listDatabaseContents(conn, opt.listParams) of cmdList: listDatabaseContents(conn, opt.listParams)
finally: finally:
conn.close conn.close