[RUBY] 13th (thor, rubocop)

!Mac OS X-10.15.7!ruby-2.7.1p83

thor

If you try to have some behavior in one command, you need to handle options etc. properly. For that, there are some CLI builder gems, and here you will learn thor.

First of all

gem install thor

Incorporate into the previously created hello \ _ruby

require 'thor'
require "hello_rudy/version"

class HelloRudyCLI < Thor
  desc "hello NAME", "say hello to NAME"
  def hello(name)
    puts "Hello " + name
  end
end

HelloRudyCLI.start(ARGV)

This almost works. The string CLI must be at the end of the class name.

To make it work properly, fix it further.

> in hello_rudy.gemspec
  spec.add_runtime_dependency('thor')

> bundle update
> bundle install

> cat lib/hello_rudy/version.rb 
class HelloRudy #
  VERSION = "0.1.0"
end

It's done.

When I try to move it to local with bundle

> bundle exec exe/hello_rudy
Commands:
  hello_rudy hello NAME      # say hello to NAME
  hello_rudy help [COMMAND]  # Describe available commands or one specific command

> bundle exec exe/hello_rudy hello Rudy
Hello Rudy

Works with.

rubocop

It checks if the code is written according to the rules.

Besides simple checks such as tabs and blank formats, there are method length and abc size. Warning that the default method length is 10 lines or more. abc size is

--Assignment: Assignment to a variable --Branch: method call --Condition: Conditional statement

Warn if something is counted and exceeds the standard value.

It turns out that it's really important to write a short method that even has such a tool.


Recommended Posts

13th (thor, rubocop)
Thor & Rubocop
Chart type ruby-appendix-VI (thor, rubocop)
5th
6th
6th
9th
7th
11th
9th
7th
11th
10th
10th
11th class: Class