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)
Related
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...
I'm trying to figure out how to define subparameters for one of my parameters. This is what I have that is NOT working:
require 'optparse'
options = {}
OptionParser.new do |parser|
parser.on("-r", "--require LIBRARY", "Require the LIBRARY before executing your script") do |lib|
parser.make_switch(["-p"], '--pop THING') do |o|
puts "You required #{o}!"
end
end
parser.on("-f", '--file FILE', 'File to be processed') do |file|
puts "This is the file: #{file}"
end
end.parse!
I'd like to do:
ruby myapp -r Library -p thing #<--required params
or
ruby myapp -f
I have a program that creates emails, what I want to do is when the -t flag is given and no argument is given with the flag, default to something, instead it outputs the usual: <main>': missing argument: -t (OptionParser::MissingArgument)
So my question being, if I have this flag:
require 'optparse'
OPTIONS = {}
OptionParser.new do |opts|
opts.on('-t INPUT', '--type INPUT', 'Specify who to say hello to'){ |o| OPTIONS[:type] = o }
end.parse!
def say_hello
puts "Hello #{OPTIONS[:type]}"
end
case
when OPTIONS[:type]
say_hello
else
puts "Hello World"
end
and I run this flag without the required argument INPUThow do I get the program to out put the Hello World instead of the: <main>': missing argument: -t (OptionParser::MissingArgument)?
Examples:
C:\Users\bin\ruby\test_folder>ruby opt.rb -t hello
Hello hello
C:\Users\bin\ruby\test_folder>ruby opt.rb -t
opt.rb:7:in `<main>': missing argument: -t (OptionParser::MissingArgument)
C:\Users\bin\ruby\test_folder>
I figured out that by adding brackets around the INPUT I can provide the option to provide input examples:
require 'optparse'
OPTIONS = {}
OptionParser.new do |opts|
opts.on('-t [INPUT]', '--type [INPUT]', 'Specify the type of email to be generated'){ |o| OPTIONS[:type] = o }
end.parse!
def say_hello
puts "Hello #{OPTIONS[:type]}"
end
case
when OPTIONS[:type]
say_hello
else
puts "Hello World"
end
Output:
C:\Users\bin\ruby\test_folder>ruby opt.rb -t
Hello World
C:\Users\bin\ruby\test_folder>ruby opt.rb -t hello
Hello hello
So if I do this:
require 'optparse'
OPTIONS = {}
OptionParser.new do |opts|
opts.on('-t [INPUT]', '--type [INPUT]', 'Specify the type of email to be generated'){ |o| OPTIONS[:type] = o }
end.parse!
def say_hello
puts "Hello #{OPTIONS[:type]}"
puts
puts OPTIONS[:type]
end
case
when OPTIONS[:type]
say_hello
else
puts "Hello World"
puts OPTIONS[:type] unless nil; puts "No value given"
end
I can output the information provided, or when there's no information provided I can output No value given:
C:\Users\bin\ruby\test_folder>ruby opt.rb -t hello
Hello hello
hello
C:\Users\bin\ruby\test_folder>ruby opt.rb -t
Hello World
No value given
# 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
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.