Hanezu Create the worst product, and improve it.

Create CLI for my blog

If you are kind enough to check my code for hosting this blog, you can see that I am using some Ruby scripts to create post templates, or rename post.

But as I get more engaged in writing posts and needs more functions, ruby ***.rb is getting clumsy.

So I decides to build a CLI for this blog in Ruby.

  1. Commandline template
    1. Option Parser? ×
    2. Thor
      1. “Default over Configuration” principle
      2. method signature over desc
  2. Commandline Tool setup
    1. chmod u+x CommandlineTool.rb
    2. #!/usr/bin/env ruby

Commandline template

Option Parser? ×

I would like the commandline tool to not only have flag, but also action. What I means is, the template for usage should be

program action --flag -p param

As a result, the built-in ruby commandline tool Option Parser is not enough for me, since it only supports flags. Besides, the grammar is a little clumsy.

Thor

Thor behaves similar as Rails, for example

  desc "hello NAME", "say hello to NAME"
  def hello(name, from=nil)
    puts "from: #{from}" if from
    puts "Hello #{name}"
  end

“Default over Configuration” principle

the important string "hello NAME" following desc concisely describes the behavior of the action hello.

If you look at options and flags(here for more detail) , the options parameter that come out from nowhere demonstrate the power of default.

method signature over desc

It should also be mentioned that, the method signature actually determine how you will use it. For example, hello "Yehuda Katz" "Carl Lerche" is possible because of the optional argument of hello method.

Commandline Tool setup

chmod u+x CommandlineTool.rb

This line change the mode by user add execution rights to the script file.

#!/usr/bin/env ruby

Put this line to the first line of your script to tell the command line to execute the file by Ruby.

You are now nearly free from ruby *.rb and can enjoy ./*.rb.

comments powered by Disqus