Reduces parenthesis usage for clearness.

This commit is contained in:
Grzegorz Adam Hankiewicz 2012-11-13 22:08:02 +01:00
commit 14038058cf

View file

@ -40,19 +40,19 @@ proc parseCmdLine(): TParamConfig =
result.action = cmdInteractive # 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
@ -60,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 = cmdParams 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 =
@ -80,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 cmdParams == 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()