From 6244cc2e4fb7aaabd14352a21add5236220a8198 Mon Sep 17 00:00:00 2001 From: def Date: Sat, 14 Feb 2015 23:43:31 +0100 Subject: Fix cross_calculator example --- examples/cross_calculator/nim_backend/backend.nim | 5 + examples/cross_calculator/nim_commandline/nim.cfg | 4 + .../nim_commandline/nimcalculator.nim | 109 +++++++++++++++++++++ .../cross_calculator/nim_commandline/readme.txt | 10 ++ .../cross_calculator/nimrod_backend/backend.nim | 5 - .../nimrod_commandline/nimcalculator.nim | 109 --------------------- .../cross_calculator/nimrod_commandline/nimrod.cfg | 4 - .../cross_calculator/nimrod_commandline/readme.txt | 10 -- 8 files changed, 128 insertions(+), 128 deletions(-) create mode 100644 examples/cross_calculator/nim_backend/backend.nim create mode 100644 examples/cross_calculator/nim_commandline/nim.cfg create mode 100644 examples/cross_calculator/nim_commandline/nimcalculator.nim create mode 100644 examples/cross_calculator/nim_commandline/readme.txt delete mode 100644 examples/cross_calculator/nimrod_backend/backend.nim delete mode 100644 examples/cross_calculator/nimrod_commandline/nimcalculator.nim delete mode 100644 examples/cross_calculator/nimrod_commandline/nimrod.cfg delete mode 100644 examples/cross_calculator/nimrod_commandline/readme.txt diff --git a/examples/cross_calculator/nim_backend/backend.nim b/examples/cross_calculator/nim_backend/backend.nim new file mode 100644 index 000000000..ffa4311f9 --- /dev/null +++ b/examples/cross_calculator/nim_backend/backend.nim @@ -0,0 +1,5 @@ +# Backend for the different user interfaces. + +proc myAdd*(x, y: int): int {.cdecl, exportc.} = + result = x + y + diff --git a/examples/cross_calculator/nim_commandline/nim.cfg b/examples/cross_calculator/nim_commandline/nim.cfg new file mode 100644 index 000000000..41c034430 --- /dev/null +++ b/examples/cross_calculator/nim_commandline/nim.cfg @@ -0,0 +1,4 @@ +# Nimrod configuration file. +# The file is used only to add the path of the backend to the compiler options. + +path="../nim_backend" diff --git a/examples/cross_calculator/nim_commandline/nimcalculator.nim b/examples/cross_calculator/nim_commandline/nimcalculator.nim new file mode 100644 index 000000000..69d62a90c --- /dev/null +++ b/examples/cross_calculator/nim_commandline/nimcalculator.nim @@ -0,0 +1,109 @@ +# Implements a command line interface against the backend. + +import backend, parseopt, strutils + +const + USAGE = """nimcalculator - Nimrod cross platform calculator + (beta version, only integer addition is supported!) + +Usage: + nimcalculator [options] [-a=value -b=value] +Options: + -a=value sets the integer value of the a parameter + -b=value sets the integer value of the b parameter + -h, --help shows this help + +If no options are used, an interactive mode is entered. +""" + +type + TCommand = enum # The possible types of operation + cmdParams, # Two valid parameters were provided + cmdInteractive # No parameters were provided, run interactive mode + + TParamConfig = object of RootObj + action: TCommand # store the type of operation + paramA, paramB: int # possibly store the valid parameters + + +proc parseCmdLine(): TParamConfig = + ## Parses the commandline. + ## + ## Returns a TParamConfig structure filled with the proper values or directly + ## calls quit() with the appropriate error message. + var + hasA = false + hasB = false + p = initOptParser() + key, val: TaintedString + + result.action = cmdInteractive # By default presume interactive mode. + try: + while true: + next p + key = p.key + val = p.val + + case p.kind + of cmdArgument: + stdout.write USAGE + quit "Erroneous argument detected: " & key, 1 + of cmdLongOption, cmdShortOption: + case key.normalize + of "help", "h": + stdout.write USAGE + quit 0 + of "a": + result.paramA = val.parseInt + hasA = true + of "b": + result.paramB = val.parseInt + hasB = true + else: + stdout.write USAGE + quit "Unexpected option: " & key, 2 + of cmdEnd: break + except ValueError: + stdout.write USAGE + quit "Invalid value " & val & " for parameter " & key, 3 + + if hasA and hasB: + result.action = cmdParams + elif hasA or hasB: + stdout.write USAGE + quit "Error: provide both A and B to operate in param mode", 4 + + +proc parseUserInput(question: string): int = + ## Parses a line of user input, showing question to the user first. + ## + ## If the user input is an empty line quit() is called. Returns the value + ## parsed as an integer. + while true: + echo question + let input = stdin.readLine + try: + result = input.parseInt + break + except ValueError: + if input.len < 1: quit "Blank line detected, quitting.", 0 + echo "Sorry, `$1' doesn't seem to be a valid integer" % input + +proc interactiveMode() = + ## Asks the user for two integer values, adds them and exits. + let + paramA = parseUserInput("Enter the first parameter (blank to exit):") + paramB = parseUserInput("Enter the second parameter (blank to exit):") + echo "Calculating... $1 + $2 = $3" % [$paramA, $paramB, + $backend.myAdd(paramA, paramB)] + + +when isMainModule: + ## Main entry point. + let opt = parseCmdLine() + if cmdParams == opt.action: + echo "Param mode: $1 + $2 = $3" % [$opt.paramA, $opt.paramB, + $backend.myAdd(opt.paramA, opt.paramB)] + else: + echo "Entering interactive addition mode" + interactiveMode() diff --git a/examples/cross_calculator/nim_commandline/readme.txt b/examples/cross_calculator/nim_commandline/readme.txt new file mode 100644 index 000000000..f95bd962e --- /dev/null +++ b/examples/cross_calculator/nim_commandline/readme.txt @@ -0,0 +1,10 @@ +In this directory you will find the nim commandline version of the +cross-calculator sample. + +The commandline interface can be used non interactively through switches, or +interactively when running the command without parameters. + +Compilation is fairly easy despite having the source split in different +directories. Thanks to the nim.cfg file, which adds the ../nim_backend +directory as a search path, you can compile and run the example just fine from +the command line with 'nim c -r nimcalculator.nim'. diff --git a/examples/cross_calculator/nimrod_backend/backend.nim b/examples/cross_calculator/nimrod_backend/backend.nim deleted file mode 100644 index ffa4311f9..000000000 --- a/examples/cross_calculator/nimrod_backend/backend.nim +++ /dev/null @@ -1,5 +0,0 @@ -# Backend for the different user interfaces. - -proc myAdd*(x, y: int): int {.cdecl, exportc.} = - result = x + y - diff --git a/examples/cross_calculator/nimrod_commandline/nimcalculator.nim b/examples/cross_calculator/nimrod_commandline/nimcalculator.nim deleted file mode 100644 index 440834ca8..000000000 --- a/examples/cross_calculator/nimrod_commandline/nimcalculator.nim +++ /dev/null @@ -1,109 +0,0 @@ -# Implements a command line interface against the backend. - -import backend, parseopt, strutils - -const - USAGE = """nimcalculator - Nimrod cross platform calculator - (beta version, only integer addition is supported!) - -Usage: - nimcalculator [options] [-a=value -b=value] -Options: - -a=value sets the integer value of the a parameter - -b=value sets the integer value of the b parameter - -h, --help shows this help - -If no options are used, an interactive mode is entered. -""" - -type - TCommand = enum # The possible types of operation - cmdParams, # Two valid parameters were provided - cmdInteractive # No parameters were provided, run interactive mode - - TParamConfig = object of TObject - action: TCommand # store the type of operation - paramA, paramB: int # possibly store the valid parameters - - -proc parseCmdLine(): TParamConfig = - ## Parses the commandline. - ## - ## Returns a TParamConfig structure filled with the proper values or directly - ## calls quit() with the appropriate error message. - var - hasA = false - hasB = false - p = initOptParser() - key, val: TaintedString - - result.action = cmdInteractive # By default presume interactive mode. - try: - while true: - next p - key = p.key - val = p.val - - case p.kind - of cmdArgument: - stdout.write USAGE - quit "Erroneous argument detected: " & key, 1 - of cmdLongOption, cmdShortOption: - case key.normalize - of "help", "h": - stdout.write USAGE - quit 0 - of "a": - result.paramA = val.parseInt - hasA = true - of "b": - result.paramB = val.parseInt - hasB = true - else: - stdout.write USAGE - quit "Unexpected option: " & key, 2 - of cmdEnd: break - except EInvalidValue: - stdout.write USAGE - quit "Invalid value " & val & " for parameter " & key, 3 - - if hasA and hasB: - result.action = cmdParams - elif hasA or hasB: - stdout.write USAGE - quit "Error: provide both A and B to operate in param mode", 4 - - -proc parseUserInput(question: string): int = - ## Parses a line of user input, showing question to the user first. - ## - ## If the user input is an empty line quit() is called. Returns the value - ## parsed as an integer. - while true: - echo question - let input = stdin.readLine - try: - result = input.parseInt - break - except EInvalidValue: - if input.len < 1: quit "Blank line detected, quitting.", 0 - echo "Sorry, `$1' doesn't seem to be a valid integer" % input - -proc interactiveMode() = - ## Asks the user for two integer values, adds them and exits. - let - paramA = parseUserInput("Enter the first parameter (blank to exit):") - paramB = parseUserInput("Enter the second parameter (blank to exit):") - echo "Calculating... $1 + $2 = $3" % [$paramA, $paramB, - $backend.myAdd(paramA, paramB)] - - -when isMainModule: - ## Main entry point. - let opt = parseCmdLine() - if cmdParams == opt.action: - echo "Param mode: $1 + $2 = $3" % [$opt.paramA, $opt.paramB, - $backend.myAdd(opt.paramA, opt.paramB)] - else: - echo "Entering interactive addition mode" - interactiveMode() diff --git a/examples/cross_calculator/nimrod_commandline/nimrod.cfg b/examples/cross_calculator/nimrod_commandline/nimrod.cfg deleted file mode 100644 index c1aedcf6a..000000000 --- a/examples/cross_calculator/nimrod_commandline/nimrod.cfg +++ /dev/null @@ -1,4 +0,0 @@ -# Nimrod configuration file. -# The file is used only to add the path of the backend to the compiler options. - -path="../nimrod_backend" diff --git a/examples/cross_calculator/nimrod_commandline/readme.txt b/examples/cross_calculator/nimrod_commandline/readme.txt deleted file mode 100644 index 5430e7b47..000000000 --- a/examples/cross_calculator/nimrod_commandline/readme.txt +++ /dev/null @@ -1,10 +0,0 @@ -In this directory you will find the nimrod commandline version of the -cross-calculator sample. - -The commandline interface can be used non interactively through switches, or -interactively when running the command without parameters. - -Compilation is fairly easy despite having the source split in different -directories. Thanks to the nimrod.cfg file, which adds the ../nimrod_backend -directory as a search path, you can compile and run the example just fine from -the command line with 'nimrod c -r nimcalculator.nim'. -- cgit 1.4.1-2-gfad0