Adds -y/-n switches for non interactive operation.

This commit is contained in:
Grzegorz Adam Hankiewicz 2013-11-07 23:42:27 +01:00
commit d2ff13144d

View file

@ -19,6 +19,11 @@ type
of ActionSearch: of ActionSearch:
search: seq[string] # Search string. search: seq[string] # Search string.
TForcePrompt = enum
DontForcePrompt, ForcePromptYes, ForcePromptNo
var forcePrompts = DontForcePrompt
const const
help = """ help = """
Usage: babel COMMAND [opts] Usage: babel COMMAND [opts]
@ -31,8 +36,10 @@ Commands:
list Lists all packages. list Lists all packages.
Options: Options:
-h Print this help message. -h, -help Print this help message.
-v Print version information. -v, -version Print version information.
-y, -accept Accept all interactive prompts.
-n, -reject Reject all interactive prompts.
""" """
babelVersion = "0.1.0" babelVersion = "0.1.0"
defaultPackageURL = "https://github.com/nimrod-code/packages/raw/master/packages.json" defaultPackageURL = "https://github.com/nimrod-code/packages/raw/master/packages.json"
@ -82,11 +89,25 @@ proc parseCmdLine(): TAction =
case key case key
of "help", "h": writeHelp() of "help", "h": writeHelp()
of "version", "v": writeVersion() of "version", "v": writeVersion()
of "accept", "y": forcePrompts = ForcePromptYes
of "reject", "n": forcePrompts = ForcePromptNo
of cmdEnd: assert(false) # cannot happen of cmdEnd: assert(false) # cannot happen
if result.typ == ActionNil: if result.typ == ActionNil:
writeHelp() writeHelp()
proc prompt(question: string): bool = proc prompt(question: string): bool =
## Asks an interactive question and returns the result.
##
## The proc will return immediately without asking the user if the global
## forcePrompts has a value different than DontForcePrompt.
case forcePrompts
of ForcePromptYes:
echo(question & " -> [forced yes]")
return true
of ForcePromptNo:
echo(question & " -> [forced no]")
return false
of DontForcePrompt:
echo(question & " [y/N]") echo(question & " [y/N]")
let yn = stdin.readLine() let yn = stdin.readLine()
case yn.normalize case yn.normalize