From f5865550be77b5346adcea1a31c8d329f6b8a184 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 1 Oct 2014 09:07:08 -0400 Subject: [PATCH] niti: add -n to run the program for each line in file-name arguments Signed-off-by: Jean Privat --- lib/niti_runtime.nit | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/nit.nit | 11 ++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 lib/niti_runtime.nit diff --git a/lib/niti_runtime.nit b/lib/niti_runtime.nit new file mode 100644 index 0000000..a618fba --- /dev/null +++ b/lib/niti_runtime.nit @@ -0,0 +1,53 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# This file is free software, which comes along with NIT. This software is +# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. You can modify it is you want, provided this header +# is kept unaltered, and a notification of the changes is added. +# You are allowed to redistribute it and sell it, alone or is a part of +# another product. + +# Runtime library to loop around the main program for each line in file-name arguments +# +# Used by the interpreter when `-n` is used +module niti_runtime + +redef class Sys + redef fun run do + if not args.is_empty then + open_next_stream + end + loop + read_next_line + main + end + end + + # Read the next useful line from file-name arguments + private fun read_next_line + do + if stdin.eof then + open_next_stream + end + var line = stdin.read_line + loop + if not stdin.eof then break + open_next_stream + if not line.is_empty then break + line = stdin.read_line + end + self.line = line + end + + # Open the next file until there is no more arguments + private fun open_next_stream + do + if args.is_empty then exit(0) + stdin.close + stdin = new IFStream.open(args.shift) + end + + # The next line to process by the main program + var line: String +end diff --git a/src/nit.nit b/src/nit.nit index 7ed1528..24bb00e 100644 --- a/src/nit.nit +++ b/src/nit.nit @@ -29,7 +29,8 @@ var opt = new OptionString("compatibility (does noting)", "-o") toolcontext.option_context.add_option(opt) var opt_mixins = new OptionArray("Additionals module to min-in", "-m") var opt_eval = new OptionBool("Specifies the program from command-line", "-e") -toolcontext.option_context.add_option(opt_mixins, opt_eval) +var opt_loop = new OptionBool("Repeatedly run the program for each line in file-name arguments", "-n") +toolcontext.option_context.add_option(opt_mixins, opt_eval, opt_loop) # We do not add other options, so process them now! toolcontext.process_options(args) @@ -49,6 +50,14 @@ if opt_eval.value then toolcontext.check_errors var parent = null + if opt_loop.value then + var nruntime = modelbuilder.load_module("niti_runtime") + if nruntime == null then + toolcontext.check_errors + abort + end + parent = nruntime.mmodule + end modelbuilder.load_rt_module(parent, amodule, "-") -- 1.7.9.5