NAME" def hello(name) puts "Hello #{name}" end end A simple Thor class exposes an executable with a number of subcommands, like git or bundler. In a Thor class, public methods become commands. Monday, September 2, 13
it generates for a Thor class. $ ruby ./cli Tasks: cli hello NAME # say hello to NAME cli help [TASK] # Describe available tasks o Monday, September 2, 13
CLI argument optional: class MyCLI < Thor desc "hello NAME", "say hello to NAME" def hello(name, from=nil) puts "from: #{from}" if from puts "Hello #{name}" end end Monday, September 2, 13
and flags as metadata about a Thor command: class MyCLI < Thor desc "hello NAME", "say hello to NAME" option :from def hello(name) puts "from: #{options[:from]}" if options[:from] puts "Hello #{name}" end end Monday, September 2, 13
:yell, :type => :boolean desc "hello NAME", "say hello to NAME" def hello(name) .... By default, options are Strings, but you can specify an alternate type for any options: Monday, September 2, 13
=> true option :yell, :type => :boolean desc "hello NAME", "say hello to NAME" def hello(name) .... end end You can also specify that a particular option is required. Monday, September 2, 13
When printing out full usage for a command using cli help hello, this description will appear next to the option. • :banner: The short description of the option, printed out in the usage description. By default, this is the upcase version of the flag (from=FROM). • :required: Indicates that an option is required • :default: The default value of this option if it is not provided. An option cannot be both :required and have a :default. • :type: :string, :hash, :array, :numeric, or :boolean • :aliases: A list of aliases for this option. Typically, you would use aliases to provide short versions of the option. The full list of metadata you can provide for an option: Monday, September 2, 13
"say hello to NAME" options :from => :required, :yell => :boolean def hello(name) .... end You can use a shorthand to specify a number of options at once if you just want to specify the type of the options. You could rewrite the previous example as: Monday, September 2, 13
for the entire class by using class_option. Class options take exactly the same parameters as options for individual commands, but apply across all commands for a class. The options hash in a given task will include any class options. Monday, September 2, 13
for the entire class by using class_option. Class options take exactly the same parameters as options for individual commands, but apply across all commands for a class. The options hash in a given task will include any class options. class MyCLI < Thor class_option :verbose, :type => :boolean desc "hello NAME", "say hello to NAME" options :from => :required, :yell => :boolean def hello(name) ... Monday, September 2, 13
to be able to specify a command that points at its own set of subcommands. One example of this is the git remote command, which exposes add, rename, rm, prune, set-head, and so in. Monday, September 2, 13