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,20 +89,34 @@ 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 =
echo(question & " [y/N]") ## Asks an interactive question and returns the result.
let yn = stdin.readLine() ##
case yn.normalize ## The proc will return immediately without asking the user if the global
of "y", "yes": ## forcePrompts has a value different than DontForcePrompt.
case forcePrompts
of ForcePromptYes:
echo(question & " -> [forced yes]")
return true return true
of "n", "no": of ForcePromptNo:
return false echo(question & " -> [forced no]")
else:
return false return false
of DontForcePrompt:
echo(question & " [y/N]")
let yn = stdin.readLine()
case yn.normalize
of "y", "yes":
return true
of "n", "no":
return false
else:
return false
let babelDir = getHomeDir() / ".babel" let babelDir = getHomeDir() / ".babel"
let pkgsDir = babelDir / "pkgs" let pkgsDir = babelDir / "pkgs"