File: declaration.rb

package info (click to toggle)
ruby-clamp 1.1.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 312 kB
  • sloc: ruby: 2,359; makefile: 4
file content (80 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require "clamp/errors"
require "clamp/subcommand/definition"

module Clamp
  module Subcommand

    module Declaration

      def recognised_subcommands
        @recognised_subcommands ||= []
      end

      def subcommand(name, description, subcommand_class = self, &block)
        subcommand_class = Class.new(subcommand_class, &block) if block
        declare_subcommand_parameters unless has_subcommands?
        recognised_subcommands << Subcommand::Definition.new(name, description, subcommand_class)
      end

      def has_subcommands?
        !recognised_subcommands.empty?
      end

      def find_subcommand(name)
        recognised_subcommands.find { |sc| sc.is_called?(name) }
      end

      def find_subcommand_class(*names)
        names.inject(self) do |command_class, name|
          return nil unless command_class
          subcommand = command_class.find_subcommand(name)
          subcommand.subcommand_class if subcommand
        end
      end

      def inheritable_attributes
        recognised_options + inheritable_parameters
      end

      def default_subcommand=(name)
        if has_subcommands?
          raise Clamp::DeclarationError, "default_subcommand must be defined before subcommands"
        end
        @default_subcommand = name
      end

      def default_subcommand(*args, &block)
        if args.empty?
          @default_subcommand
        else
          $stderr.puts "WARNING: Clamp default_subcommand syntax has changed; check the README."
          $stderr.puts "  (from #{caller.first})"
          self.default_subcommand = args.first
          subcommand(*args, &block)
        end
      end

      private

      def declare_subcommand_parameters
        if @default_subcommand
          parameter "[SUBCOMMAND]", "subcommand",
                    :attribute_name => :subcommand_name,
                    :default => @default_subcommand,
                    :inheritable => false
        else
          parameter "SUBCOMMAND", "subcommand",
                    :attribute_name => :subcommand_name,
                    :required => false,
                    :inheritable => false
        end
        remove_method :default_subcommand_name
        parameter "[ARG] ...", "subcommand arguments",
                  :attribute_name => :subcommand_arguments,
                  :inheritable => false
      end

    end

  end
end