Module: MiniCli

Defined in:
lib/mini-cli.rb,
lib/mini-cli/run.rb,
lib/mini-cli/version.rb

Constant Summary collapse

VERSION =
'0.6.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/mini-cli.rb', line 4

def self.included(mod)
  mod.const_set(
    :MiniCli__,
    Instance.new(caller_locations(1, 1).first.absolute_path)
  )
  mod.private_constant(:MiniCli__)
end

Instance Method Details

#after(&callback) ⇒ Object



60
61
62
# File 'lib/mini-cli.rb', line 60

def after(&callback)
  callback and __minicli__.after << callback
end

#before(&callback) ⇒ Object



56
57
58
# File 'lib/mini-cli.rb', line 56

def before(&callback)
  callback and __minicli__.before << callback
end

#error(code, message = nil) ⇒ Object



36
37
38
39
# File 'lib/mini-cli.rb', line 36

def error(code, message = nil)
  $stderr.puts("#{name}: #{message}") if message && show_errors?
  exit(__minicli__.error_code = code)
end

#error_codeObject



32
33
34
# File 'lib/mini-cli.rb', line 32

def error_code
  __minicli__.error_code
end

#help(helptext, *args) ⇒ Object



16
17
18
# File 'lib/mini-cli.rb', line 16

def help(helptext, *args)
  __minicli__.help(helptext, args)
end

#mainObject



48
49
50
51
52
53
54
# File 'lib/mini-cli.rb', line 48

def main
  __minicli__.run do
    yield(parse_argv)
  rescue Interrupt
    error(130, 'aborted')
  end
end

#name(name = nil) ⇒ Object



12
13
14
# File 'lib/mini-cli.rb', line 12

def name(name = nil)
  __minicli__.name(name)
end

#parse_argv(argv = nil, &argv_converter) ⇒ Object



41
42
43
44
45
46
# File 'lib/mini-cli.rb', line 41

def parse_argv(argv = nil, &argv_converter)
  return __minicli__.converter = argv_converter if argv_converter
  argv = Array.new(argv || ARGV)
  exit(show_help) if argv.index('--help') || argv.index('-h')
  __minicli__.main(argv, method(:error).to_proc)
end

#run(*cmd, stdin_data: nil, status: false, chdir: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mini-cli/run.rb', line 6

def run(*cmd, stdin_data: nil, status: false, chdir: nil)
  in_read, in_write = IO.pipe
  opts = { err: i[child out], in: in_read }
  opts[:chdir] = chdir if chdir
  ret = IO.popen(*cmd, opts, &__run_proc(stdin_data, in_write))
  status ? [Process.last_status, ret] : ret
rescue Errno::ENOENT
  nil
ensure
  in_read&.close
  in_write&.close
end

#run_ruby(*cmd, **run_options) ⇒ Object



19
20
21
# File 'lib/mini-cli/run.rb', line 19

def run_ruby(*cmd, **run_options)
  run(Shellwords.join(__ruby_cmd + cmd), **run_options)
end

#run_script(script, status: false, chdir: nil) ⇒ Object



23
24
25
# File 'lib/mini-cli/run.rb', line 23

def run_script(script, status: false, chdir: nil)
  run_ruby(stdin_data: script, status: status, chdir: chdir)
end

#show_errors=(value) ⇒ Object



28
29
30
# File 'lib/mini-cli.rb', line 28

def show_errors=(value)
  __minicli__.show_errors = value ? true : false
end

#show_errors?Boolean



24
25
26
# File 'lib/mini-cli.rb', line 24

def show_errors?
  __minicli__.show_errors
end

#show_helpObject



20
21
22
# File 'lib/mini-cli.rb', line 20

def show_help
  __minicli__.show_help
end