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.
706 707 708 709 710 711 712 713 714 |
# File 'lib/hiiro.rb', line 706 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.
703 704 705 |
# File 'lib/hiiro.rb', line 703 def bin_name @bin_name end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
703 704 705 |
# File 'lib/hiiro.rb', line 703 def handler @handler end |
#name ⇒ Object (readonly) Also known as: subcommand_name
Returns the value of attribute name.
703 704 705 |
# File 'lib/hiiro.rb', line 703 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
703 704 705 |
# File 'lib/hiiro.rb', line 703 def opts @opts end |
#subcmd_args ⇒ Object (readonly)
Returns the value of attribute subcmd_args.
703 704 705 |
# File 'lib/hiiro.rb', line 703 def subcmd_args @subcmd_args end |
#subcmd_opts ⇒ Object (readonly)
Returns the value of attribute subcmd_opts.
703 704 705 |
# File 'lib/hiiro.rb', line 703 def subcmd_opts @subcmd_opts end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
703 704 705 |
# File 'lib/hiiro.rb', line 703 def values @values end |
Instance Method Details
#exact_match?(subcmd) ⇒ Boolean
725 726 727 |
# File 'lib/hiiro.rb', line 725 def exact_match?(subcmd) name == subcmd.to_s end |
#full_name ⇒ Object
733 734 735 |
# File 'lib/hiiro.rb', line 733 def full_name [bin_name, name].join(?-) end |
#location ⇒ Object
741 742 743 |
# File 'lib/hiiro.rb', line 741 def location handler.source_location&.join(':') end |
#match?(subcmd) ⇒ Boolean
729 730 731 |
# File 'lib/hiiro.rb', line 729 def match?(subcmd) name.start_with?(subcmd.to_s) end |
#opts_hint ⇒ Object
769 770 771 |
# File 'lib/hiiro.rb', line 769 def opts_hint subcmd_opts&.hint end |
#params_string ⇒ Object
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
# File 'lib/hiiro.rb', line 745 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
716 717 718 719 720 721 722 723 |
# File 'lib/hiiro.rb', line 716 def run(*args) if opts parsed = opts.parse(args) handler.call(parsed) else handler.call(*args) end end |
#type ⇒ Object
737 738 739 |
# File 'lib/hiiro.rb', line 737 def type :subcommand end |