Merge pull request #249 from gradha/pr_improve_example_style
Improve examples' style
This commit is contained in:
commit
e4211230e8
4 changed files with 69 additions and 80 deletions
|
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -39,22 +37,22 @@ proc parseCmdLine(): TParamConfig =
|
||||||
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
|
||||||
key = p.key
|
key = p.key
|
||||||
val = p.val
|
val = p.val
|
||||||
|
|
||||||
case p.kind
|
case p.kind
|
||||||
of cmdArgument:
|
of cmdArgument:
|
||||||
stdout.write(USAGE)
|
stdout.write USAGE
|
||||||
quit("Erroneous argument detected: " & key, 1)
|
quit "Erroneous argument detected: " & key, 1
|
||||||
of cmdLongOption, cmdShortOption:
|
of cmdLongOption, cmdShortOption:
|
||||||
case normalize(key)
|
case key.normalize
|
||||||
of "help", "h":
|
of "help", "h":
|
||||||
stdout.write(USAGE)
|
stdout.write USAGE
|
||||||
quit(0)
|
quit 0
|
||||||
of "a":
|
of "a":
|
||||||
result.paramA = val.parseInt
|
result.paramA = val.parseInt
|
||||||
hasA = true
|
hasA = true
|
||||||
|
|
@ -62,18 +60,18 @@ proc parseCmdLine(): TParamConfig =
|
||||||
result.paramB = val.parseInt
|
result.paramB = val.parseInt
|
||||||
hasB = true
|
hasB = true
|
||||||
else:
|
else:
|
||||||
stdout.write(USAGE)
|
stdout.write USAGE
|
||||||
quit("Unexpected option: " & key, 2)
|
quit "Unexpected option: " & key, 2
|
||||||
of cmdEnd: break
|
of cmdEnd: break
|
||||||
except EInvalidValue:
|
except EInvalidValue:
|
||||||
stdout.write(USAGE)
|
stdout.write USAGE
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
proc parseUserInput(question: string): int =
|
proc parseUserInput(question: string): int =
|
||||||
|
|
@ -82,29 +80,30 @@ proc parseUserInput(question: string): int =
|
||||||
## If the user input is an empty line quit() is called. Returns the value
|
## If the user input is an empty line quit() is called. Returns the value
|
||||||
## parsed as an integer.
|
## parsed as an integer.
|
||||||
while true:
|
while true:
|
||||||
echo(question)
|
echo question
|
||||||
let input = stdin.readLine
|
let input = stdin.readLine
|
||||||
try:
|
try:
|
||||||
result = input.parseInt
|
result = input.parseInt
|
||||||
break
|
break
|
||||||
except EInvalidValue:
|
except EInvalidValue:
|
||||||
if input.len < 1: quit("Blank line detected, quitting.", 0)
|
if input.len < 1: quit "Blank line detected, quitting.", 0
|
||||||
echo("Sorry, `$1' doesn't seem to be a valid integer" % input)
|
echo "Sorry, `$1' doesn't seem to be a valid integer" % input
|
||||||
|
|
||||||
proc interactiveMode() =
|
proc interactiveMode() =
|
||||||
## Asks the user for two integer values, adds them and exits.
|
## Asks the user for two integer values, adds them and exits.
|
||||||
let paramA = parseUserInput("Enter the first parameter (blank to exit):")
|
let
|
||||||
let paramB = parseUserInput("Enter the second parameter (blank to exit):")
|
paramA = parseUserInput("Enter the first parameter (blank to exit):")
|
||||||
echo("Calculating... $1 + $2 = $3" % [$paramA, $paramB,
|
paramB = parseUserInput("Enter the second parameter (blank to exit):")
|
||||||
$backend.myAdd(paramA, paramB)])
|
echo "Calculating... $1 + $2 = $3" % [$paramA, $paramB,
|
||||||
|
$backend.myAdd(paramA, paramB)]
|
||||||
|
|
||||||
|
|
||||||
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:
|
||||||
echo("Entering interactive addition mode")
|
echo "Entering interactive addition mode"
|
||||||
interactiveMode()
|
interactiveMode()
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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) =
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -40,19 +34,19 @@ 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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue