Class: SpreenPr::CLI
- Inherits:
-
Object
- Object
- SpreenPr::CLI
- Defined in:
- lib/spreen_pr/cli.rb,
sig/generated/spreen_pr/cli.rbs
Overview
Command line interface behind the pr-title executable:
pr-title [BRANCH_NAME] [--format text|json].
Constant Summary collapse
- FORMATS =
: Array
%w[text json].freeze
Instance Attribute Summary collapse
-
#action ⇒ Symbol
readonly
: Symbol.
-
#argv ⇒ Array[String]
readonly
: Array.
-
#format ⇒ String
readonly
: String.
Class Method Summary collapse
Instance Method Summary collapse
- #branch_name ⇒ String
- #current_git_branch ⇒ String?
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
- #parser ⇒ OptionParser
- #print_help ⇒ Integer
- #print_title ⇒ Integer
- #print_version ⇒ Integer
- #run ⇒ Integer
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
23 24 25 26 27 |
# File 'lib/spreen_pr/cli.rb', line 23 def initialize(argv) @argv = argv.dup @format = 'text' @action = :print_title end |
Instance Attribute Details
#action ⇒ Symbol (readonly)
: Symbol
42 43 44 |
# File 'lib/spreen_pr/cli.rb', line 42 def action @action end |
#argv ⇒ Array[String] (readonly)
: Array
40 41 42 |
# File 'lib/spreen_pr/cli.rb', line 40 def argv @argv end |
#format ⇒ String (readonly)
: String
41 42 43 |
# File 'lib/spreen_pr/cli.rb', line 41 def format @format end |
Class Method Details
.start(argv = ARGV) ⇒ Integer
17 18 19 |
# File 'lib/spreen_pr/cli.rb', line 17 def self.start(argv = ARGV) new(argv).run end |
Instance Method Details
#branch_name ⇒ String
64 65 66 67 68 69 70 |
# File 'lib/spreen_pr/cli.rb', line 64 def branch_name branch = argv.shift || current_git_branch return branch unless branch.nil? || branch.empty? raise ArgumentError, 'No branch name given and the current git branch could not be detected' end |
#current_git_branch ⇒ String?
73 74 75 76 77 |
# File 'lib/spreen_pr/cli.rb', line 73 def current_git_branch IO.popen(%w[git branch --show-current], err: File::NULL, &:read).strip rescue SystemCallError nil end |
#parser ⇒ OptionParser
80 81 82 83 84 85 86 87 |
# File 'lib/spreen_pr/cli.rb', line 80 def parser @parser ||= OptionParser.new('Usage: pr-title [BRANCH_NAME] [options]') do |opt| opt.on('--format FORMAT', FORMATS, "Output format (#{FORMATS.join(' or ')}, default: text)") { |value| @format = value } opt.on('--version', 'Print the version') { @action = :print_version } opt.on('-h', '--help', 'Print this help') { @action = :print_help } end end |
#print_help ⇒ Integer
58 59 60 61 |
# File 'lib/spreen_pr/cli.rb', line 58 def print_help puts parser 0 end |
#print_title ⇒ Integer
45 46 47 48 49 |
# File 'lib/spreen_pr/cli.rb', line 45 def print_title application = Application.new(branch_name:) puts(format == 'json' ? JSON.generate(application.to_h) : application.title) 0 end |
#print_version ⇒ Integer
52 53 54 55 |
# File 'lib/spreen_pr/cli.rb', line 52 def print_version puts VERSION 0 end |
#run ⇒ Integer
30 31 32 33 34 35 36 |
# File 'lib/spreen_pr/cli.rb', line 30 def run parser.parse!(argv) __send__(action) rescue ArgumentError, OptionParser::ParseError => e warn e. 1 end |