Make Thor show message for top level command - ruby

Is there any way to make Thor show a general message for the top level command?
$my_command help
I'd like to show a welcome message here.
Commands:
my_command help [COMMAND]

The closest thing I can think of is adding a default task and using it to invoke the help task. You'd get this message when calling $my_command with no arguments
require 'thor'
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
def hello(name)
puts "Hello #{name}"
end
desc "greeting", "this is just a greeting"
def greeting
puts "Welcome to MyCLI"
invoke :help
end
default_task :greeting
end
MyCLI.start(ARGV)
# $my_command
# output:
# Welcome to MyCLI
# Commands:
# test.rb greeting # this is just a greeting
# test.rb hello NAME # say hello to NAME
# test.rb help [COMMAND] # Describe available commands or one spec...

Related

How do i run Ruby script from command line ?

I've a file, which I can run from command line using:
ruby filename.rb
Which outputs:
12345
A different script containing:
def hi()
puts "hello"
end
does not return anything when I run it from the command-line.
How do I run this from the command line?
Add puts hi("John") to the bottom of the method:
def hi(name)
puts "hello"
end
puts hi("John")
Where "John" is whatever name you want it to be.
Then just run it as usual, ruby yourfilename.rb
Try putting this in filename.rb:
def hi()
puts "hello"
end
hi
Then run your code in the command line: with ruby filename.rb

Command Aliasing in Thor

Is it possible to create aliases for commands in Thor?
Much like command aliasing in Commander. https://github.com/tj/commander#command-aliasing
I am able to find aliases for options, but not for the command itself.
Using the example from Thor,
#!/usr/bin/env ruby
require 'thor'
# cli.rb
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
def hello(name)
puts "Hello #{name}"
end
end
MyCLI.start(ARGV)
I should be able to run
$ ./cli.rb hello John
Hello John
I would like to alias the command "hello" to "hi" as well.
You can use map for this:
http://www.rubydoc.info/github/wycats/thor/master/Thor#map-class_method
#!/usr/bin/env ruby
require 'thor'
# cli.rb
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
def hello(name)
puts "Hello #{name}"
end
map hi: :hello
end
MyCLI.start(ARGV)
Use method_option for aliases.
#!/usr/bin/env ruby
require 'thor'
# cli.rb
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
method_option :hello , :aliases => "-hello" , :desc => "Hello Command"
def hello(name)
puts "Hello #{name}"
end
end
MyCLI.start(ARGV)

Why is Ruby output suppressed on the console?

# code.rb
def hello
puts "hello"
end
:$ ruby code.rb
Nothing is output on the console! I am using Ubuntu 13.04.
If I run the same code in IRB it works!
You have to call your code, you're just defining a method:
# code.rb
def hello
puts "hello"
end
hello
$ ruby code.rb
You define a method, but you're never calling it. Try this:
# code.rb
def hello
puts "hello"
end
hello
Run it:
:$ ruby code.rb
You need to call the method, in this case, hello in the script:
def hello
puts "hello"
end
hello

Running command line commands from Thor executable

In my executable Ruby file I have the following:
#!/usr/bin/env ruby
require 'thor'
include Thor::Actions
class UI < Thor
# def self.source_root
# File.dirname(__FILE__)
# end
desc "makecal", "Generates postscript calendar to your desktop"
def makecal
# puts `ls ~`
puts run('ls ~')
# puts run "pcalmakecal -B -b all -d Helvetica/8 -t Helvetica/16 -S #{Time.now.month} #{Time.now.year} > ~/Desktop/#{Time.now.month}-#{Time.now.year}"
end
end
UI.start
In the terminal when I run the file as is I get an empty line as Thor's run command is returning a NilClass.
However, when I un-comment the puts `ls ~` and comment out Thor's run method I get an output of my home directory as expected.
I'm having trouble figuring out why I can't get Thor's run method to work like Ruby's ticks.
Any ideas where I may have went wrong?
Thanks for looking
I didn't put the include statement inside my class and that messed things up. The code should be:
#!/usr/bin/env ruby
require 'makecal'
class UI < Thor
include Thor::Actions
# def self.source_root
# File.dirname(__FILE__)
# end
#
desc "makecal", "Generates postscript calendar to your desktop"
def makecal
# puts `ls ~`
puts run('ls ~')
# puts run "pcal -B -b all -d Helvetica/8 -t Helvetica/16 -S #{Time.now.month} #{Time.now.year} > ~/Desktop/#{Time.now.month}-#{Time.now.year}"
end
end
UI.start
Thor's documentation on this method is actually wrong and incomplete. It documents that it returns the "contents of the command" (which I assume means the standard output), but it, by defualt, does nothing.
But, you can, apparently, use the :capture option to get what you want:
unless options[:pretend]
config[:capture] ? `#{command}` : system("#{command}")
end
So, try doing
puts run("ls ~", :capture => true)
And see if that does it.

How can I get "thor list" to list methods in ruby class

I have problems listing my tasks in thor, even with the simplest program:
class Mytest < Thor
desc "Hello world", "Puts 'hello world' on the console"
def hello
puts "Hello world"
end
end
Here is the console output when i run thor list
$ thor list
mytest
------
thor mytest:world # Puts 'hello world' on the console
As you can see the "hello" method name is omitted from the output. Can you help me out please?
Thank you,
Paul
Just change the 'desc' line to:
desc "hello", "Puts 'hello world' on the console"
Hope it helps.

Resources