Movatterモバイル変換


[0]ホーム

URL:


Libraries »thor(1.3.2) »Index (C) »Thor »Base »ClassMethods

Module: Thor::Base::ClassMethods

Defined in:
lib/thor/base.rb

Instance Method Summarycollapse

Instance Method Details

#all_commandsObjectAlso known as:all_tasks

Returns the commands for this Thor class and all subclasses.

Returns

Hash

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

#allow_incompatible_default_type!Object

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

#argument(name, options = {}) ⇒Object

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.

Parameters

name<Symbol>

The name of the argument.

options<Hash>

Described below.

Options

: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.

Errors

ArgumentError

Raised if you supply a required argument after a non required one.

261262263264265266267268269270271272273274275276277278279280281282283284285286
# File 'lib/thor/base.rb', line 261defargument(name,options={})is_thor_reserved_word?(name,:argument)no_commands{attr_accessorname}required=ifoptions.key?(:optional)!options[:optional]elsifoptions.key?(:required)options[:required]elseoptions[: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}."endendoptions[:required]=requiredarguments<<Thor::Argument.new(name,options)end

#argumentsObject

Returns this class arguments, looking up in the ancestors chain.

Returns

Array

293294295
# File 'lib/thor/base.rb', line 293defarguments@arguments||=from_superclass(:arguments,[])end

#attr_accessorObject

:nodoc:

162163164
# File 'lib/thor/base.rb', line 162defattr_accessor(*)#:nodoc:no_commands{super}end

#attr_readerObject

:nodoc:

154155156
# File 'lib/thor/base.rb', line 154defattr_reader(*)#:nodoc:no_commands{super}end

#attr_writerObject

:nodoc:

158159160
# File 'lib/thor/base.rb', line 158defattr_writer(*)#:nodoc:no_commands{super}end

#check_default_typeObject

: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

#check_default_type!Object

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

#check_unknown_optionsObject

:nodoc:

172173174
# File 'lib/thor/base.rb', line 172defcheck_unknown_options#:nodoc:@check_unknown_options||=from_superclass(:check_unknown_options,false)end

#check_unknown_options!Object

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 168defcheck_unknown_options!@check_unknown_options=trueend

#check_unknown_options?(config) ⇒Boolean

:nodoc:

Returns:

  • (Boolean)
176177178
# File 'lib/thor/base.rb', line 176defcheck_unknown_options?(config)#:nodoc:!!check_unknown_optionsend

#class_at_least_one(*args, &block) ⇒Object

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.

Examples

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)register_options_relation_for(:class_options,:class_at_least_one_option_names,*args,&block)end

#class_at_least_one_option_namesObject

Returns this class at least one of required options array set, looking up in the ancestors chain.

Returns

Array[Array]

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

#class_exclusive(*args, &block) ⇒Object

Adds and declares option group for exclusive options in the block and arguments. You can declare options as the outside of the block.

Parameters

Array

Examples

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)register_options_relation_for(:class_options,:class_exclusive_option_names,*args,&block)end

#class_exclusive_option_namesObject

Returns this class exclusive options array set, looking up in the ancestors chain.

Returns

Array[Array]

402403404
# File 'lib/thor/base.rb', line 402defclass_exclusive_option_names@class_exclusive_option_names||=from_superclass(:class_exclusive_option_names,[])end

#class_option(name, options = {}) ⇒Object

Adds an option to the set of class options

Parameters

name<Symbol>

The name of the argument.

options<Hash>

Described below.

Options

:desc

– Description for the argument.

:required

– If the argument is required or not.

:default

– Default value for this argument.

:group

– The group for this options. Use by class options to output options in different levels.

:aliases

– 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.

:type

– The type of the argument, can be :string, :hash, :array, :numeric or :boolean.

:banner

– String to show on usage notes.

:hide

– If you want to hide this option from the help.

328329330331332333
# File 'lib/thor/base.rb', line 328defclass_option(name,options={})unless[Symbol,String].any?{|klass|name.is_a?(klass)}raiseArgumentError,"Expected a Symbol or String, got#{name.inspect}"endbuild_option(name,options,class_options)end

#class_options(options = nil) ⇒Object

Adds a bunch of options to the set of class options.

class_options:foo=>false,:bar=>:required,:baz=>:string

If you prefer more detailed declaration, check class_option.

Parameters

Hash[Symbol => Object]

306307308309310
# File 'lib/thor/base.rb', line 306defclass_options(options=nil)@class_options||=from_superclass(:class_options,{})build_options(options,@class_options)ifoptions@class_optionsend

#commandsObjectAlso known as:tasks

Returns the commands for this Thor class.

Returns

Hash

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

#disable_required_check?(command_name) ⇒Boolean

If true, option set will not suspend the execution of the command when a required option is not provided.

Returns:

  • (Boolean)
207208209
# File 'lib/thor/base.rb', line 207defdisable_required_check?(command_name)#:nodoc:falseend

#exit_on_failure?Boolean

A flag that makes the process exit with status 1 if any error happens.

Returns:

  • (Boolean)
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

#group(name = nil) ⇒Object

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.

Parameters

name<String|Symbol>

457458459460461462463
# File 'lib/thor/base.rb', line 457defgroup(name=nil)ifname@group=name.to_selse@group||=from_superclass(:group,"standard")endend

#handle_argument_error(command, error, args, arity) ⇒Object

: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: \"#{banner(command).split("\n").join("\"\n       \"")}\""raiseInvocationError,msgend

#handle_no_command_error(command, has_namespace = $thor_runner) ⇒ObjectAlso known as:handle_no_task_error

: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

#namespace(name = nil) ⇒Object

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

#no_commands(&block) ⇒ObjectAlso known as:no_tasks

All methods defined inside the given block are not added as commands.

So you can do:

classMyScript<Thorno_commandsdodefthis_is_not_a_commandendendend

You can also add the method and remove it from the command list:

classMyScript<Thordefthis_is_not_a_commandendremove_command:this_is_not_a_commandend
530531532
# File 'lib/thor/base.rb', line 530defno_commands(&block)no_commands_context.enter(&block)end

#no_commands?Boolean

Returns:

  • (Boolean)
540541542
# File 'lib/thor/base.rb', line 540defno_commands?no_commands_context.entered?end

#no_commands_contextObject

536537538
# File 'lib/thor/base.rb', line 536defno_commands_context@no_commands_context||=NestedContext.newend

#public_command(*names) ⇒ObjectAlso known as:public_task

Allows to use private methods from parent in child classes as commands.

Parameters

names<Array>:: Method names to be used as commands

Examples

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

#remove_argument(*names) ⇒Object

Removes a previous defined argument. If :undefine is given, undefine accessors as well.

Parameters

names<Array>

Arguments to be removed

Examples

remove_argument:fooremove_argument:foo,:bar,:baz,:undefine=>true
426427428429430431432433
# File 'lib/thor/base.rb', line 426defremove_argument(*names)options=names.last.is_a?(Hash)?names.pop:{}names.eachdo|name|arguments.delete_if{|a|a.name==name.to_s}undef_methodname,"#{name}="ifoptions[:undefine]endend

#remove_class_option(*names) ⇒Object

Removes a previous defined class option.

Parameters

names<Array>

Class options to be removed

Examples

remove_class_option:fooremove_class_option:foo,:bar,:baz
445446447448449
# File 'lib/thor/base.rb', line 445defremove_class_option(*names)names.eachdo|name|class_options.delete(name)endend

#remove_command(*names) ⇒ObjectAlso known as:remove_task

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.

Parameters

name<Symbol|String>

The name of the command to be removed

options<Hash>

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)options=names.last.is_a?(Hash)?names.pop:{}names.eachdo|name|commands.delete(name.to_s)all_commands.delete(name.to_s)undef_methodnameifoptions[:undefine]endend

#start(given_args = ARGV, config = {}) ⇒Object

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,options,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.message)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

#stop_on_unknown_option?(command_name) ⇒Boolean

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:

  • (Boolean)
201202203
# File 'lib/thor/base.rb', line 201defstop_on_unknown_option?(command_name)#:nodoc:falseend

#strict_args_positionObject

:nodoc:

218219220
# File 'lib/thor/base.rb', line 218defstrict_args_position#:nodoc:@strict_args_position||=from_superclass(:strict_args_position,false)end

#strict_args_position!Object

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

#strict_args_position?(config) ⇒Boolean

:nodoc:

Returns:

  • (Boolean)
222223224
# File 'lib/thor/base.rb', line 222defstrict_args_position?(config)#:nodoc:!!strict_args_positionend
Generated on Wed Jul 9 20:16:01 2025 byyard 0.9.37 (ruby-3.4.3).

[8]ページ先頭

©2009-2025 Movatter.jp