Class: Roby::App::Scripts::InterfaceScript

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/app/scripts.rb

Overview

Common implementation for scripts whose main functionality requires to connect to Roby’s remote interface

Examples:

basic usage

require 'roby/app/scripts'
Roby::App::Scripts::InterfaceScript.run(*ARGV) do |interface|
    # Do stuff with the roby interface
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = Roby.app, default_host: app.shell_interface_host || "localhost", default_port: app.shell_interface_port || Interface::DEFAULT_PORT) ⇒ InterfaceScript

Returns a new instance of InterfaceScript.



22
23
24
25
26
27
# File 'lib/roby/app/scripts.rb', line 22

def initialize(app = Roby.app,
               default_host: app.shell_interface_host || "localhost",
               default_port: app.shell_interface_port || Interface::DEFAULT_PORT)
    @app = app
    @host_options = Hash[host: default_host, port: default_port]
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



20
21
22
# File 'lib/roby/app/scripts.rb', line 20

def app
  @app
end

Class Method Details

.run(*args, banner: "", &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/roby/app/scripts.rb', line 60

def self.run(*args, banner: "", &block)
    Roby.display_exception do
        begin
            new.run(*args, banner: banner, &block)
        rescue Roby::Interface::ConnectionError => e
            Robot.fatal e.message
        rescue Interrupt
            Robot.warn "Interrupted by user"
        end
    end
end

Instance Method Details

#default_option_parser(banner: "") ⇒ Object



33
34
35
36
37
38
# File 'lib/roby/app/scripts.rb', line 33

def default_option_parser(banner: "")
    parser = OptionParser.new
    parser.banner = banner
    setup_option_parser(parser)
    parser
end

#hostObject



40
41
42
# File 'lib/roby/app/scripts.rb', line 40

def host
    @host_options.values_at(:host, :port)
end

#run(*args, banner: "", option_parser: default_option_parser(banner: banner)) {|interface| ... } ⇒ Object

Yields:

  • (interface)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/roby/app/scripts.rb', line 44

def run(
    *args, banner: "",
    option_parser: default_option_parser(banner: banner)
)

    app.guess_app_dir
    app.shell
    app.single
    app.load_base_config

    args = option_parser.parse(args)
    host, port = self.host
    interface = Interface::V1.connect_with_tcp_to(host, port)
    yield(interface)
end

#setup_option_parser(parser) ⇒ Object



29
30
31
# File 'lib/roby/app/scripts.rb', line 29

def setup_option_parser(parser)
    Roby::Application.host_options(parser, @host_options)
end