Skip to content
Kward Search API index

Class: Kward::NativeShell::Invocation

Inherits:
Struct
  • Object
show all
Defined in:
lib/kward/shell/native_invocation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#arguments ⇒ Object

Returns the value of attribute arguments

Returns:

  • (Object) —

    the current value of arguments



3
4
5
# File 'lib/kward/shell/native_invocation.rb', line 3

def arguments
  @arguments
end

#command ⇒ Object

Returns the value of attribute command

Returns:

  • (Object) —

    the current value of command



3
4
5
# File 'lib/kward/shell/native_invocation.rb', line 3

def command
  @command
end

#command_name ⇒ Object

Returns the value of attribute command_name

Returns:

  • (Object) —

    the current value of command_name



3
4
5
# File 'lib/kward/shell/native_invocation.rb', line 3

def command_name
  @command_name
end

#file ⇒ Object

Returns the value of attribute file

Returns:

  • (Object) —

    the current value of file



3
4
5
# File 'lib/kward/shell/native_invocation.rb', line 3

def file
  @file
end

#help ⇒ Object

Returns the value of attribute help

Returns:

  • (Object) —

    the current value of help



3
4
5
# File 'lib/kward/shell/native_invocation.rb', line 3

def help
  @help
end

#interactive ⇒ Object

Returns the value of attribute interactive

Returns:

  • (Object) —

    the current value of interactive



3
4
5
# File 'lib/kward/shell/native_invocation.rb', line 3

def interactive
  @interactive
end

#options ⇒ Object

Returns the value of attribute options

Returns:

  • (Object) —

    the current value of options



3
4
5
# File 'lib/kward/shell/native_invocation.rb', line 3

def options
  @options
end

Class Method Details

.parse(arguments) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/kward/shell/native_invocation.rb', line 4

def self.parse(arguments)
  args = arguments.dup
  options = []
  mode = nil
  interactive = false
  while args.first&.start_with?("-", "+") && args.first != "-"
    option = args.shift
    break if option == "--"
    if option == "--help"
      return new(help: true)
    end
    option[1..].each_char do |flag|
      case flag
      when "c", "s"
        raise ArgumentError, "-c and -s are mutually exclusive" if mode && mode != flag
        raise ArgumentError, "+#{flag} is not supported" if option.start_with?("+")
        mode = flag
      when "i" then interactive = option.start_with?("-")
      when "o"
        raise ArgumentError, "#{option} requires an option name" if args.empty?
        options.concat(["#{option[0]}o", args.shift])
      else options << "#{option[0]}#{flag}"
      end
    end
  end
  args.shift if args.first == "-"
  command = args.shift if mode == "c"
  raise ArgumentError, "-c requires a command" if mode == "c" && !command
  file = args.shift unless mode || args.empty?
  command_name = args.shift if mode == "c"
  new(options: options, arguments: args, file: file, command: command, command_name: command_name, interactive: interactive)
end

Instance Method Details

#prompt?(input, error) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/kward/shell/native_invocation.rb', line 37

def prompt?(input, error)
  !help && !command && !file && input.tty? && error.tty?
end