Returns the commands for this Thor class and all subclasses.
If you want to use defaults that don’t match the type of an option, either specify ‘check_default_type: false` or call `allow_incompatible_default_type!`.
Adds an argument to the class and creates an attr_accessor for it.
Returns this class arguments, looking up in the ancestors chain.
:nodoc:.
:nodoc:.
:nodoc:.
:nodoc:.
If you want to raise an error when the default value of an option does not match the type call check_default_type! This will be the default; for compatibility a deprecation warning is issued if necessary.
:nodoc:.
If you want to raise an error for unknown options, call check_unknown_options! This is disabled by default to allow dynamic invocations.
:nodoc:.
Adds and declares option group for required at least one of options in the block and arguments.
Returns this class at least one of required options array set, looking up in the ancestors chain.
Adds and declares option group for exclusive options in the block and arguments.
Returns this class exclusive options array set, looking up in the ancestors chain.
Adds an option to the set of class options.
Adds a bunch of options to the set of class options.
Returns the commands for this Thor class.
If true, option set will not suspend the execution of the command when a required option is not provided.
A flag that makes the process exit with status 1 if any error happens.
Defines the group.
:nodoc:.
:nodoc:.
Sets the namespace for the Thor or Thor::Group class.
All methods defined inside the given block are not added as commands.
Allows to use private methods from parent in child classes as commands.
Removes a previous defined argument.
Removes a previous defined class option.
Removes a given command from this Thor class.
Parses the command and options from the given args, instantiate the class and invoke the command.
If true, option parsing is suspended as soon as an unknown option or a regular argument is encountered.
:nodoc:.
If you want only strict string args (useful when cascading thor classes), call strict_args_position! This is disabled by default to allow dynamic invocations.
:nodoc:.
Returns the commands for this Thor class and all subclasses.
An ordered hash with commands names as keys and Thor::Command objects as values.
482483484485 | # File 'lib/thor/base.rb', line 482defall_commands@all_commands||=from_superclass(:all_commands,Hash.new)@all_commands.merge!(commands)end |
If you want to use defaults that don’t match the type of an option, either specify ‘check_default_type: false` or call `allow_incompatible_default_type!`
189190191 | # File 'lib/thor/base.rb', line 189defallow_incompatible_default_type!@check_default_type=falseend |
Adds an argument to the class and creates an attr_accessor for it.
Arguments are different from options in several aspects. The first one is how they are parsed from the command line, arguments are retrieved from position:
thorcommandNAME
Instead of:
thorcommand--name=NAME
Besides, arguments are used inside your code as an accessor (self.argument), while options are all kept in a hash (self.options).
Finally, arguments cannot have type :default or :boolean but can be optional (supplying :optional => :true or :required => false), although you cannot have a required argument after a non-required argument. If you try it, an error is raised.
The name of the argument.
Described below.
:desc - Description for the argument. :required - If the argument is required or not. :optional - If the argument is optional or not. :type - The type of the argument, can be :string, :hash, :array, :numeric. :default - Default value for this argument. It cannot be required and have default values. :banner - String to show on usage notes.
Raised if you supply a required argument after a non required one.
261262263264265266267268269270271272273274275276277278279280281282283284285286 | # File 'lib/thor/base.rb', line 261defargument(name,={})is_thor_reserved_word?(name,:argument)no_commands{attr_accessorname}required=if.key?(:optional)![:optional]elsif.key?(:required)[:required]else[:default].nil?endremove_argumentnameifrequiredarguments.eachdo|argument|nextifargument.required?raiseArgumentError,"You cannot have#{name.to_s.inspect} as required argument after" \"the non-required argument#{argument.human_name.inspect}."endend[:required]=requiredarguments<<Thor::Argument.new(name,)end |
293294295 | # File 'lib/thor/base.rb', line 293defarguments@arguments||=from_superclass(:arguments,[])end |
:nodoc:
162163164 | # File 'lib/thor/base.rb', line 162defattr_accessor(*)#:nodoc:no_commands{super}end |
:nodoc:
154155156 | # File 'lib/thor/base.rb', line 154defattr_reader(*)#:nodoc:no_commands{super}end |
:nodoc:
158159160 | # File 'lib/thor/base.rb', line 158defattr_writer(*)#:nodoc:no_commands{super}end |
:nodoc:
193194195196 | # File 'lib/thor/base.rb', line 193defcheck_default_type#:nodoc:@check_default_type=from_superclass(:check_default_type,nil)unlessdefined?(@check_default_type)@check_default_typeend |
If you want to raise an error when the default value of an option does not match the type call check_default_type! This will be the default; for compatibility a deprecation warning is issued if necessary.
183184185 | # File 'lib/thor/base.rb', line 183defcheck_default_type!@check_default_type=trueend |
:nodoc:
172173174 | # File 'lib/thor/base.rb', line 172def#:nodoc:@check_unknown_options||=from_superclass(:check_unknown_options,false)end |
If you want to raise an error for unknown options, call check_unknown_options! This is disabled by default to allow dynamic invocations.
168169170 | # File 'lib/thor/base.rb', line 168def@check_unknown_options=trueend |
:nodoc:
Returns:
176177178 | # File 'lib/thor/base.rb', line 176def(config)#:nodoc:!!end |
Adds and declares option group for required at least one of options in the block and arguments. You can declare options as the outside of the block.
class_at_least_onedoclass_option:oneclass_option:twoend
Or
class_option:oneclass_option:twoclass_at_least_one:one,:two
If you do not give “–one” and “–two” AtLeastOneRequiredArgumentError will be raised.
You can use class_at_least_one and class_exclusive at the same time.
class_exclusivedoclass_at_least_onedoclass_option:oneclass_option:twoendend
Then it is required either only one of “–one” or “–two”.
392393394395 | # File 'lib/thor/base.rb', line 392defclass_at_least_one(*args,&block)(:class_options,:class_at_least_one_option_names,*args,&block)end |
Returns this class at least one of required options array set, looking up in the ancestors chain.
411412413 | # File 'lib/thor/base.rb', line 411defclass_at_least_one_option_names@class_at_least_one_option_names||=from_superclass(:class_at_least_one_option_names,[])end |
Adds and declares option group for exclusive options in the block and arguments. You can declare options as the outside of the block.
class_exclusivedoclass_option:oneclass_option:twoend
Or
class_option:oneclass_option:twoclass_exclusive:one,:two
If you give “–one” and “–two” at the same time ExclusiveArgumentsError will be raised.
357358359360 | # File 'lib/thor/base.rb', line 357defclass_exclusive(*args,&block)(:class_options,:class_exclusive_option_names,*args,&block)end |
Returns this class exclusive options array set, looking up in the ancestors chain.
402403404 | # File 'lib/thor/base.rb', line 402defclass_exclusive_option_names@class_exclusive_option_names||=from_superclass(:class_exclusive_option_names,[])end |
Adds an option to the set of class options
The name of the argument.
Described below.
– Description for the argument.
– If the argument is required or not.
– Default value for this argument.
– The group for this options. Use by class options to output options in different levels.
– Aliases for this option.Note: Thor follows a convention of one-dash-one-letter options. Thus aliases like “-something” wouldn’t be parsed; use either “--something” or “-s” instead.
– The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
– String to show on usage notes.
– If you want to hide this option from the help.
328329330331332333 | # File 'lib/thor/base.rb', line 328defclass_option(name,={})unless[Symbol,String].any?{|klass|name.is_a?(klass)}raiseArgumentError,"Expected a Symbol or String, got#{name.inspect}"endbuild_option(name,,)end |
Adds a bunch of options to the set of class options.
:foo=>false,:bar=>:required,:baz=>:string
If you prefer more detailed declaration, check class_option.
Hash[Symbol => Object]
306307308309310 | # File 'lib/thor/base.rb', line 306def(=nil)@class_options||=from_superclass(:class_options,{})(,@class_options)if@class_optionsend |
Returns the commands for this Thor class.
An ordered hash with commands names as keys and Thor::Command objects as values.
471472473 | # File 'lib/thor/base.rb', line 471defcommands@commands||=Hash.newend |
If true, option set will not suspend the execution of the command when a required option is not provided.
Returns:
207208209 | # File 'lib/thor/base.rb', line 207defdisable_required_check?(command_name)#:nodoc:falseend |
A flag that makes the process exit with status 1 if any error happens.
Returns:
628629630631 | # File 'lib/thor/base.rb', line 628defexit_on_failure?Thor.deprecation_warning"Thor exit with status 0 on errors. To keep this behavior, you must define `exit_on_failure?` in `#{self.name}`"falseend |
Defines the group. This is used when thor list is invoked so you can specify that only commands from a pre-defined group will be shown. Defaults to standard.
name<String|Symbol>
457458459460461462463 | # File 'lib/thor/base.rb', line 457defgroup(name=nil)ifname@group=name.to_selse@group||=from_superclass(:group,"standard")endend |
:nodoc:
Raises:
618619620621622623624625 | # File 'lib/thor/base.rb', line 618defhandle_argument_error(command,error,args,arity)#:nodoc:name=[command.ancestor_name,command.name].compact.join("")msg="ERROR: \"#{basename}#{name}\" was called with".dupmsg<<"no arguments"ifargs.empty?msg<<"arguments"<<args.inspectunlessargs.empty?msg<<"\nUsage: \"#{(command).split("\n").join("\"\n \"")}\""raiseInvocationError,msgend |
:nodoc:
Raises:
613614615 | # File 'lib/thor/base.rb', line 613defhandle_no_command_error(command,has_namespace=$thor_runner)#:nodoc:raiseUndefinedCommandError.new(command,all_commands.keys,(namespaceifhas_namespace))end |
Sets the namespace for the Thor or Thor::Group class. By default the namespace is retrieved from the class name. If your Thor class is named Scripts::MyScript, the help method, for example, will be called as:
thor scripts:my_script -h
If you change the namespace:
namespace:my_scripts
You change how your commands are invoked:
thormy_scripts-h
Finally, if you change your namespace to default:
namespace:default
Your commands can be invoked with a shortcut. Instead of:
thor:my_command
566567568569570571572 | # File 'lib/thor/base.rb', line 566defnamespace(name=nil)ifname@namespace=name.to_selse@namespace||=Thor::Util.namespace_from_thor_class(self)endend |
530531532 | # File 'lib/thor/base.rb', line 530defno_commands(&block)no_commands_context.enter(&block)end |
Returns:
540541542 | # File 'lib/thor/base.rb', line 540defno_commands?no_commands_context.entered?end |
536537538 | # File 'lib/thor/base.rb', line 536defno_commands_context@no_commands_context||=NestedContext.newend |
Allows to use private methods from parent in child classes as commands.
names<Array>:: Method names to be used as commands
public_command:foopublic_command:foo,:bar,:baz
606607608609610 | # File 'lib/thor/base.rb', line 606defpublic_command(*names)names.eachdo|name|class_eval"def#{name}(*); super end",__FILE__,__LINE__endend |
Removes a previous defined argument. If :undefine is given, undefine accessors as well.
Arguments to be removed
remove_argument:fooremove_argument:foo,:bar,:baz,:undefine=>true
426427428429430431432433 | # File 'lib/thor/base.rb', line 426defremove_argument(*names)=names.last.is_a?(Hash)?names.pop:{}names.eachdo|name|arguments.delete_if{|a|a.name==name.to_s}undef_methodname,"#{name}="if[:undefine]endend |
Removes a previous defined class option.
Class options to be removed
remove_class_option:fooremove_class_option:foo,:bar,:baz
445446447448449 | # File 'lib/thor/base.rb', line 445defremove_class_option(*names)names.eachdo|name|.delete(name)endend |
Removes a given command from this Thor class. This is usually done if you are inheriting from another class and don’t want it to be available anymore.
By default it only remove the mapping to the command. But you can supply :undefine => true to undefine the method from the class as well.
The name of the command to be removed
You can give :undefine => true if you want commands the method to be undefined from the class as well.
500501502503504505506507508 | # File 'lib/thor/base.rb', line 500defremove_command(*names)=names.last.is_a?(Hash)?names.pop:{}names.eachdo|name|commands.delete(name.to_s)all_commands.delete(name.to_s)undef_methodnameif[:undefine]endend |
Parses the command and options from the given args, instantiate the class and invoke the command. This method is used when the arguments must be parsed from an array. If you are inside Ruby and want to use a Thor class, you can simply initialize it:
script=MyScript.new(args,,config)script.invoke(:command,first_arg,second_arg,third_arg)
582583584585586587588589590591592593594 | # File 'lib/thor/base.rb', line 582defstart(given_args=ARGV,config={})config[:shell]||=Thor::Base.shell.newdispatch(nil,given_args.dup,nil,config)rescueThor::Error=>econfig[:debug]||ENV["THOR_DEBUG"]=="1"?(raisee):config[:shell].error(e.)exit(false)ifexit_on_failure?rescueErrno::EPIPE# This happens if a thor command is piped to something like `head`,# which closes the pipe when it's done reading. This will also# mean that if the pipe is closed, further unnecessary# computation will not occur.exit(true)end |
If true, option parsing is suspended as soon as an unknown option or a regular argument is encountered. All remaining arguments are passed to the command as regular arguments.
Returns:
201202203 | # File 'lib/thor/base.rb', line 201defstop_on_unknown_option?(command_name)#:nodoc:falseend |
:nodoc:
218219220 | # File 'lib/thor/base.rb', line 218defstrict_args_position#:nodoc:@strict_args_position||=from_superclass(:strict_args_position,false)end |
If you want only strict string args (useful when cascading thor classes), call strict_args_position! This is disabled by default to allow dynamic invocations.
214215216 | # File 'lib/thor/base.rb', line 214defstrict_args_position!@strict_args_position=trueend |
:nodoc:
Returns:
222223224 | # File 'lib/thor/base.rb', line 222defstrict_args_position?(config)#:nodoc:!!strict_args_positionend |