Module: Dapp::Helper::Cli

Included in:
CLI
Defined in:
lib/dapp/helper/cli.rb

Instance Method Summary collapse

Instance Method Details

#cli_wrapper(cli) ⇒ Object



10
11
12
13
14
# File 'lib/dapp/helper/cli.rb', line 10

def cli_wrapper(cli)
  yield
rescue OptionParser::MissingArgument, OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::AmbiguousOption => e
  print_error_with_help_and_die!(cli, e.message)
end

#composite_options(opt) ⇒ Object



69
70
71
72
# File 'lib/dapp/helper/cli.rb', line 69

def composite_options(opt)
  @composite_options ||= {}
  @composite_options[opt] ||= []
end

#in_validate!(v, list) ⇒ Object

Raises:

  • (OptionParser::InvalidArgument)


74
75
76
# File 'lib/dapp/helper/cli.rb', line 74

def in_validate!(v, list)
  raise OptionParser::InvalidArgument, "`#{v}` is not included in the list [#{list_msg_format(list)}]" unless list.include?(v)
end

#list_msg_format(list) ⇒ Object



78
79
80
# File 'lib/dapp/helper/cli.rb', line 78

def list_msg_format(list)
  list.map { |s| "'#{s}'"}.join(', ')
end

#parse_options(cli, argv) ⇒ Object



4
5
6
7
8
# File 'lib/dapp/helper/cli.rb', line 4

def parse_options(cli, argv)
  cli_wrapper(cli) do
    cli.parse_options(argv)
  end
end

#parse_subcommand(cli, args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dapp/helper/cli.rb', line 30

def parse_subcommand(cli, args)
  argv = args
  divided_subcommand = []
  subcommand_argv = []

  cmd_arr = args.dup
  loop do
    if cli.class::SUBCOMMANDS.include? cmd_arr.join(' ')
      argv = args[0...args.index(cmd_arr.first)]
      divided_subcommand = cmd_arr
      index = cmd_arr.one? ? args.index(cmd_arr.first).next : args.index(cmd_arr.last).next
      subcommand_argv = args[index..-1]
    elsif !cmd_arr.empty?
      cmd_arr.pop
      next
    end
    break
  end

  [argv, divided_subcommand, subcommand_argv]
end

#prepare_subcommand(divided_subcommand) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/dapp/helper/cli.rb', line 61

def prepare_subcommand(divided_subcommand)
  Array(divided_subcommand)
    .map { |c| c.split(/[-_]/) }
    .flatten
    .map(&:capitalize)
    .join
end


23
24
25
26
27
28
# File 'lib/dapp/helper/cli.rb', line 23

def print_error_with_help_and_die!(cli, error_message)
  STDERR.puts "Error: #{error_message}"
  puts
  puts cli.opt_parser
  exit 1
end

#required_argument(cli, argument) ⇒ Object



16
17
18
19
20
21
# File 'lib/dapp/helper/cli.rb', line 16

def required_argument(cli, argument)
  unless (arg = cli.cli_arguments.pop)
    print_error_with_help_and_die!(cli, "required argument `#{argument.upcase}`")
  end
  arg
end

#run_subcommand(cli, divided_subcommand, subcommand_argv) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/dapp/helper/cli.rb', line 52

def run_subcommand(cli, divided_subcommand, subcommand_argv)
  if !divided_subcommand.empty?
    cli.class.const_get(prepare_subcommand(divided_subcommand)).new.run(subcommand_argv)
  else
    puts cli.opt_parser
    exit 1
  end
end