Class: Maintainers::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/maintainers/cli.rb

Overview

CLI entry point

Constant Summary collapse

SUBCOMMANDS_WE_LOVE =
[
  "create",
  "add",
  "remove",
  "list",
  "report",
  "help",
  "--help",
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



11
12
13
# File 'lib/maintainers/cli.rb', line 11

def initialize
  @options = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/maintainers/cli.rb', line 9

def options
  @options
end

Instance Method Details

#parse(args) ⇒ Hash

Return an options hash

Returns:

  • (Hash)

    Return an options hash



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/maintainers/cli.rb', line 31

def parse(args)

  subcommands = {
    'create' => OptionParser.new do |opts|
        opts.on("-i", "--issues [ISSUES URL]", "issues url") do |v|
          options[:issues] = v
        end
     end,
    'add' => OptionParser.new do |opts|
        opts.on("-g", "--github [GITHUB ID]", "github id") do |v|
          options[:github] = v
        end
        opts.on("-e", "--email [EMAIL ADDRESS]", "email address") do |v|
          options[:email] = v
        end
        opts.on("-n", "--name [REAL NAME]", "real name") do |v|
          options[:name] = v
        end
     end,
    'remove' => OptionParser.new do |opts|
        opts.on("-g", "--github [GITHUB ID]", "github id") do |v|
          options[:github] = v
        end
     end,
    'list' => OptionParser.new do |opts|
     end,
   }

  if args.count == 0
    $stderr.puts "Give me some args please"
    usage
  end

  subcommand = args.shift
  options[:subcommand] = subcommand

  unless subcommands.keys.include? subcommand
    usage
  end

  subcommands[subcommand].order!

  if subcommand == 'create' && options[:issues].nil?
    $stderr.puts "Please specify --issues"
    usage
  end

  if subcommand == 'add' && options[:github].nil?
    $stderr.puts "Please specify --github"
    usage
  end

  if args.count > 0
    $stderr.puts "Unexpected additional args #{args}"
    usage
  end

  options
end

#run(args = ARGV) ⇒ Fixnum

Returns exit code.

Returns:

  • (Fixnum)

    exit code



92
93
94
95
96
97
98
99
100
101
# File 'lib/maintainers/cli.rb', line 92

def run(args = ARGV)
  @options = parse(args)

  runner = Runner.new(@options)
  runner.run
rescue StandardError, SyntaxError => e
  $stderr.puts e.message
  $stderr.puts e.backtrace
  return 1
end

#usageObject



15
16
17
18
# File 'lib/maintainers/cli.rb', line 15

def usage
  puts "Imagine a usage statement here"
  exit 1
end