Class: Hiiro::Runners::Subcommand
- Inherits:
-
Object
- Object
- Hiiro::Runners::Subcommand
- Defined in:
- lib/hiiro.rb
Instance Attribute Summary collapse
-
#bin_name ⇒ Object
readonly
Returns the value of attribute bin_name.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#name ⇒ Object
(also: #subcommand_name)
readonly
Returns the value of attribute name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#subcmd_args ⇒ Object
readonly
Returns the value of attribute subcmd_args.
-
#subcmd_opts ⇒ Object
readonly
Returns the value of attribute subcmd_opts.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #exact_match?(subcmd) ⇒ Boolean
- #full_name ⇒ Object
-
#initialize(bin_name, name, handler, values = {}, opts: nil, subcmd_args: [], subcmd_opts: nil) ⇒ Subcommand
constructor
A new instance of Subcommand.
- #location ⇒ Object
- #match?(subcmd) ⇒ Boolean
- #opts_hint ⇒ Object
- #params_string ⇒ Object
- #run(*args) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(bin_name, name, handler, values = {}, opts: nil, subcmd_args: [], subcmd_opts: nil) ⇒ Subcommand
Returns a new instance of Subcommand.
707 708 709 710 711 712 713 714 715 |
# File 'lib/hiiro.rb', line 707 def initialize(bin_name, name, handler, values={}, opts: nil, subcmd_args: [], subcmd_opts: nil) @bin_name = bin_name @name = name.to_s @handler = handler @values = values || {} @opts = opts @subcmd_args = subcmd_args || [] @subcmd_opts = subcmd_opts end |
Instance Attribute Details
#bin_name ⇒ Object (readonly)
Returns the value of attribute bin_name.
704 705 706 |
# File 'lib/hiiro.rb', line 704 def bin_name @bin_name end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
704 705 706 |
# File 'lib/hiiro.rb', line 704 def handler @handler end |
#name ⇒ Object (readonly) Also known as: subcommand_name
Returns the value of attribute name.
704 705 706 |
# File 'lib/hiiro.rb', line 704 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
704 705 706 |
# File 'lib/hiiro.rb', line 704 def opts @opts end |
#subcmd_args ⇒ Object (readonly)
Returns the value of attribute subcmd_args.
704 705 706 |
# File 'lib/hiiro.rb', line 704 def subcmd_args @subcmd_args end |
#subcmd_opts ⇒ Object (readonly)
Returns the value of attribute subcmd_opts.
704 705 706 |
# File 'lib/hiiro.rb', line 704 def subcmd_opts @subcmd_opts end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
704 705 706 |
# File 'lib/hiiro.rb', line 704 def values @values end |
Instance Method Details
#exact_match?(subcmd) ⇒ Boolean
726 727 728 |
# File 'lib/hiiro.rb', line 726 def exact_match?(subcmd) name == subcmd.to_s end |
#full_name ⇒ Object
734 735 736 |
# File 'lib/hiiro.rb', line 734 def full_name [bin_name, name].join(?-) end |
#location ⇒ Object
742 743 744 |
# File 'lib/hiiro.rb', line 742 def location handler.source_location&.join(':') end |
#match?(subcmd) ⇒ Boolean
730 731 732 |
# File 'lib/hiiro.rb', line 730 def match?(subcmd) name.start_with?(subcmd.to_s) end |
#opts_hint ⇒ Object
770 771 772 |
# File 'lib/hiiro.rb', line 770 def opts_hint subcmd_opts&.hint end |
#params_string ⇒ Object
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/hiiro.rb', line 746 def params_string if subcmd_args.any? return subcmd_args.map { |a| "<#{a}>" }.join(' ') end return nil unless handler.respond_to?(:parameters) params = handler.parameters return nil if params.empty? return nil if params == [[:rest]] || params == [[:rest, :args]] params.map { |type, name| case type when :req then "<#{name}>" when :opt then "[#{name}]" when :rest then "[*#{name}]" if name when :keyreq then "<#{name}:>" when :key then "[#{name}:]" when :keyrest then "[**#{name}]" if name when :block then "[&#{name}]" if name end }.compact.join(' ') end |
#run(*args) ⇒ Object
717 718 719 720 721 722 723 724 |
# File 'lib/hiiro.rb', line 717 def run(*args) if opts parsed = opts.parse(args) handler.call(parsed) else handler.call(*args) end end |
#type ⇒ Object
738 739 740 |
# File 'lib/hiiro.rb', line 738 def type :subcommand end |