Module: Dapp::Helper::Cli
- Included in:
- CLI
- Defined in:
- lib/dapp/helper/cli.rb
Overview
Cli
Instance Method Summary collapse
- #cli_wrapper(cli) ⇒ Object
- #composite_options(opt) ⇒ Object
- #parse_options(cli, argv) ⇒ Object
-
#parse_subcommand(cli, args) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #prepare_subcommand(divided_subcommand) ⇒ Object
- #required_argument(cli) ⇒ Object
-
#run_subcommand(cli, divided_subcommand, subcommand_argv) ⇒ Object
rubocop:enable Metrics/MethodLength.
Instance Method Details
#cli_wrapper(cli) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/dapp/helper/cli.rb', line 11 def cli_wrapper(cli) yield rescue OptionParser::MissingArgument, OptionParser::InvalidOption, OptionParser::InvalidArgument => e STDERR.puts "Error: #{e.}" puts cli.opt_parser exit 1 end |
#composite_options(opt) ⇒ Object
66 67 68 69 |
# File 'lib/dapp/helper/cli.rb', line 66 def (opt) @composite_options ||= {} @composite_options[opt] ||= [] end |
#parse_options(cli, argv) ⇒ Object
5 6 7 8 9 |
# File 'lib/dapp/helper/cli.rb', line 5 def (cli, argv) cli_wrapper(cli) do cli.(argv) end end |
#parse_subcommand(cli, args) ⇒ Object
rubocop:disable Metrics/MethodLength
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dapp/helper/cli.rb', line 28 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
62 63 64 |
# File 'lib/dapp/helper/cli.rb', line 62 def prepare_subcommand(divided_subcommand) Array(divided_subcommand).map(&:capitalize).join end |
#required_argument(cli) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/dapp/helper/cli.rb', line 19 def required_argument(cli) unless (arg = cli.cli_arguments.pop) puts cli.opt_parser exit 1 end arg end |
#run_subcommand(cli, divided_subcommand, subcommand_argv) ⇒ Object
rubocop:enable Metrics/MethodLength
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dapp/helper/cli.rb', line 51 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 STDERR.puts 'Error: subcommand not passed' puts puts cli.opt_parser exit 1 end end |