Module: Clitopic::Commands::ClassMethods
- Included in:
- Clitopic::Commands
- Defined in:
- lib/clitopic/commands.rb
Instance Attribute Summary collapse
-
#binary ⇒ Object
Returns the value of attribute binary.
-
#current_cmd ⇒ Object
Returns the value of attribute current_cmd.
-
#current_topic ⇒ Object
Returns the value of attribute current_topic.
Instance Method Summary collapse
- #all_commands ⇒ Object
- #current_args ⇒ Object
- #current_options ⇒ Object
- #find_cmd(command) ⇒ Object
- #global_commands ⇒ Object
- #global_option(name, *args, &blk) ⇒ Object
- #global_options ⇒ Object
- #invalid_arguments ⇒ Object
- #load_commands(dir) ⇒ Object
- #prepare_run(cmd, arguments) ⇒ Object
- #run(cmd, arguments = []) ⇒ Object
- #shift_argument ⇒ Object
- #validate_arguments!(invalid_options) ⇒ Object
Instance Attribute Details
#binary ⇒ Object
Returns the value of attribute binary.
9 10 11 |
# File 'lib/clitopic/commands.rb', line 9 def binary @binary end |
#current_cmd ⇒ Object
Returns the value of attribute current_cmd.
9 10 11 |
# File 'lib/clitopic/commands.rb', line 9 def current_cmd @current_cmd end |
#current_topic ⇒ Object
Returns the value of attribute current_topic.
9 10 11 |
# File 'lib/clitopic/commands.rb', line 9 def current_topic @current_topic end |
Instance Method Details
#all_commands ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/clitopic/commands.rb', line 64 def all_commands cmds = [] Topics.topics.each do |k,topic| topic.commands.each do |name, cmd| if name == 'index' cmds << topic.name else cmds << "#{topic.name}:#{name}" end end end cmds += global_commands.keys return cmds end |
#current_args ⇒ Object
17 18 19 |
# File 'lib/clitopic/commands.rb', line 17 def current_args @current_args end |
#current_options ⇒ Object
21 22 23 |
# File 'lib/clitopic/commands.rb', line 21 def @current_options ||= {} end |
#find_cmd(command) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/clitopic/commands.rb', line 109 def find_cmd(command) cmd_name, sub_cmd_name = command.split(':') if global_commands.has_key?(command) current_cmd = global_commands[cmd_name] elsif !Topics[cmd_name].nil? sub_cmd_name = 'index' if sub_cmd_name.nil? current_topic = Topics[cmd_name] current_cmd = current_topic.commands[sub_cmd_name] else return nil, nil end return current_cmd, current_topic end |
#global_commands ⇒ Object
29 30 31 |
# File 'lib/clitopic/commands.rb', line 29 def global_commands @global_commands ||= {} end |
#global_option(name, *args, &blk) ⇒ Object
33 34 35 36 |
# File 'lib/clitopic/commands.rb', line 33 def global_option(name, *args, &blk) # args.sort.reverse gives -l, --long order << Clitopic::Utils.parse_option(name, *args, &blk) end |
#global_options ⇒ Object
25 26 27 |
# File 'lib/clitopic/commands.rb', line 25 def @global_options ||= [] end |
#invalid_arguments ⇒ Object
38 39 40 |
# File 'lib/clitopic/commands.rb', line 38 def invalid_arguments @invalid_arguments end |
#load_commands(dir) ⇒ Object
11 12 13 14 15 |
# File 'lib/clitopic/commands.rb', line 11 def load_commands(dir) Dir[File.join(dir, "*.rb")].each do |file| require file end end |
#prepare_run(cmd, arguments) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/clitopic/commands.rb', line 79 def prepare_run(cmd, arguments) @current_options, @current_args = cmd.parse(arguments.dup) return @current_options, @current_args rescue OptionParser::ParseError => e $stderr.puts Clitopic::Helpers.format_with_bang(e.) cmd. = {} cmd.arguments = {} @current_options = nil @current_arguments = nil run(cmd.fullname, ["--help"]) end |
#run(cmd, arguments = []) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/clitopic/commands.rb', line 91 def run(cmd, arguments=[]) if cmd == "-h" || cmd == "--help" cmd = 'help' end @current_cmd, @current_topic = find_cmd(cmd) if !@current_cmd Clitopic::Helpers.error([ "`#{cmd}` is not a command.", Clitopic::Helpers.display_suggestion(cmd, all_commands), "See `help` for a list of available commands." ].compact.join("\n\n")) end if @current_cmd.[:load_defaults] == true || Clitopic.load_defaults? @current_cmd.load_defaults end prepare_run(@current_cmd, arguments) @current_cmd.call end |
#shift_argument ⇒ Object
42 43 44 45 |
# File 'lib/clitopic/commands.rb', line 42 def shift_argument # dup argument to get a non-frozen string @invalid_arguments.shift.dup rescue nil end |
#validate_arguments!(invalid_options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/clitopic/commands.rb', line 47 def validate_arguments!() unless .empty? arguments = .map {|arg| "\"#{arg}\""} if arguments.length == 1 = "Invalid option: #{arguments.first}" elsif arguments.length > 1 = "Invalid options: " << arguments[0...-1].join(", ") << " and " << arguments[-1] end $stderr.puts(Clitopic::Helpers.format_with_bang() + "\n\n") run(@current_cmd.fullname, ["--help"]) end end |