lib/sym/app/cli.rb in sym-2.6.2 vs lib/sym/app/cli.rb in sym-2.6.3

- old
+ new

@@ -54,35 +54,45 @@ class CLI # brings in #parse(Array[String] args) include CLISlop - attr_accessor :opts, :application, :outputs + attr_accessor :opts, :application, :outputs, :stdin, :stdout, :stderr, :kernel - def initialize(argv) - begin + def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = nil) + + self.stdin = stdin + self.stdout = stdout + self.stderr = stderr + self.kernel = kernel + + Sym::App.stdin = stdin + Sym::App.stdout = stdout + Sym::App.stderr = stderr + + begin # Re-map any legacy options to the new options self.opts = parse(argv) if opts[:sym_args] append_sym_args(argv) self.opts = parse(argv) end # Disable coloring if requested, or if piping STDOUT - if opts[:no_color] || !STDOUT.tty? + if opts[:no_color] || !self.stdout.tty? Colored2.disable! # reparse options without the colors to create new help msg self.opts = parse(argv) end rescue StandardError => e log :error, "#{e.message}" if opts error exception: e return end - self.application = ::Sym::Application.new(opts) + self.application = ::Sym::Application.new(opts, stdin, stdout, stderr, kernel) end def append_sym_args(argv) if env_args = sym_args argv << env_args.split(' ') @@ -93,10 +103,14 @@ def sym_args ENV[Sym::Constants::ENV_ARGS_VARIABLE_NAME] end + def execute! + execute + end + def execute return Sym::App.exit_code if Sym::App.exit_code != 0 result = application.execute if result.is_a?(Hash) self.output_proc ::Sym::App::Args.new({}).output_class @@ -104,15 +118,18 @@ end Sym::App.exit_code end def command - @command ||= self.application&.command + @command ||= self.application.command if self.application end def output_proc(proc = nil) - self.application&.output = proc if proc - self.application&.output + if self.application + self.application.output = proc if proc + return self.application.output + end + nil end def opts_present o = opts.to_hash o.keys.map { |k| opts[k] ? nil : k }.compact.each { |k| o.delete(k) }