module MaxCube module Network module TCP class Client # Provides handling of concrete commands from shell command line. module Commands private # List of commands and their aliases. COMMANDS = { 'usage' => %w[? h help], 'data' => %w[B buffer d], 'history' => %w[H hist], 'clear' => %w[C], 'dump' => %w[D], 'list' => %w[l], 'config' => %w[c], 'send' => %w[cmd s set], 'pair' => %w[n], 'ntp' => %w[N f], 'url' => %w[U u], 'wake' => %w[w z], 'metadata' => %w[m meta], 'delete' => %w[del], 'reset' => %w[], 'verbose' => %w[V], 'save' => %w[S], 'load' => %w[L], 'persist' => %w[P], 'quit' => %w[q], }.freeze # Returns usage message for a single command. # @param command [String] command key from {COMMANDS}. # @param args [String] arguments accepted by command. # @param description [String] description of command. # @param message [String, nil] optional message type # that is about to be sent by the command. # @param response [String, nil] optional message type # of expected response to this message. # @return [String] usage message for +command+. def usage_cmd(command, args, description, message = nil, response = nil) cmds_str = (COMMANDS[command].dup << command).join('|') cmds_str << ' ' << args unless args.empty? description, *rest = description.split("\n") rest << "[#{message} message]" if message rest << "[#{response} response]" if response rest = if rest.empty? '' else rest.map { |s| ' ' * 52 + s }.join("\n") << "\n" end ' ' << cmds_str << ' ' * (48 - cmds_str.size) << description << "\n" << rest end # Prints usage message composed mainly # from particular {#usage_cmd} calls. def cmd_usage puts "\nUSAGE: []\nCOMMADS:\n" << usage_cmd('usage', '', 'Prints this message') << usage_cmd('data', '', 'Lists buffered received data (hashes)') << usage_cmd('history', '', 'Lists all received data incl. the cleared') << usage_cmd('clear', '', "Clears collected data\n" \ '(resp. moves it to history)') << usage_cmd('dump', '', "Shortcut for 'data' + 'clear'") << usage_cmd('list', '', 'Requests for new list of devices', 'l', 'L') << usage_cmd('config', '', 'Requests for configuration message', 'c', 'C') << usage_cmd('send', '{}', 'Sends settings to connected devices', 's', 'S') << usage_cmd('pair', '{}', 'Sets device into pairing mode' \ " with optional timeout\n" \ '(request for a new device)', 'n', 'N') << usage_cmd('ntp', '{}', 'Requests for NTP servers' \ ' and optionally updates them', 'f', 'F') << usage_cmd('url', '{ }', 'Configures Cube\'s portal URL', 'u') << usage_cmd('wake', '{