Class: Fusion::CLI::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/fusion/cli/options.rb

Defined Under Namespace

Classes: UsageError

Constant Summary collapse

USAGE =
<<~TEXT
  usage: fusion [options] <file.fsn> [json-input]
         fusion [options] -e '<source>' [json-input]
         fusion --repl

  use cases:
    (default)       pipe: apply the program to one input
    --stream        apply the program to each line of an NDJSON stream
    --repl          interactive expressions and `identifier = expression`

  options:
    -e '<source>'   inline program instead of a file
    --input MODE    how the input marks an error value
    --output MODE   how the output marks an error value
    -!              treat the input as an error value (unix input mode only)

  modes: unix, bang, array, object
    unix    pipe only (default there): plain JSON; output: stdout/exit 0
            for values, stderr/exit 1 for error payloads
    bang    a leading "!" marks an error value (default for --stream)
    array   [0, value] marks a value, [1, payload] an error
    object  {"value": _} marks a value, {"error": _} an error
TEXT
MODES =
%w[unix bang array object].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(use_case:, input_mode:, output_mode:, inline_source:, program_path:, explicit_input:, error_input:) ⇒ Options

Returns a new instance of Options.



45
46
47
48
49
50
51
52
53
# File 'lib/fusion/cli/options.rb', line 45

def initialize(use_case:, input_mode:, output_mode:, inline_source:, program_path:, explicit_input:, error_input:)
  @use_case = use_case
  @input_mode = input_mode
  @output_mode = output_mode
  @inline_source = inline_source
  @program_path = program_path
  @explicit_input = explicit_input
  @error_input = error_input
end

Instance Attribute Details

#explicit_inputObject (readonly)

Returns the value of attribute explicit_input.



43
44
45
# File 'lib/fusion/cli/options.rb', line 43

def explicit_input
  @explicit_input
end

#inline_sourceObject (readonly)

Returns the value of attribute inline_source.



43
44
45
# File 'lib/fusion/cli/options.rb', line 43

def inline_source
  @inline_source
end

#input_modeObject (readonly)

Returns the value of attribute input_mode.



43
44
45
# File 'lib/fusion/cli/options.rb', line 43

def input_mode
  @input_mode
end

#output_modeObject (readonly)

Returns the value of attribute output_mode.



43
44
45
# File 'lib/fusion/cli/options.rb', line 43

def output_mode
  @output_mode
end

#program_pathObject (readonly)

Returns the value of attribute program_path.



43
44
45
# File 'lib/fusion/cli/options.rb', line 43

def program_path
  @program_path
end

#use_caseObject (readonly)

Returns the value of attribute use_case.



43
44
45
# File 'lib/fusion/cli/options.rb', line 43

def use_case
  @use_case
end

Class Method Details

.parse(argv) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fusion/cli/options.rb', line 59

def self.parse(argv)
  arguments = argv.dup
  use_case = :pipe
  input_mode = nil
  output_mode = nil
  error_input = false
  inline_source = nil
  positional = []

  until arguments.empty?
    argument = arguments.shift
    case argument
    when "--stream" then use_case = :stream
    when "--repl" then use_case = :repl
    when "--input" then input_mode = shift_mode(arguments, "--input")
    when "--output" then output_mode = shift_mode(arguments, "--output")
    when "-!" then error_input = true
    when "-e"
      inline_source = arguments.shift
      raise UsageError, "-e requires a source argument" if inline_source.nil?
    when /\A--/
      raise UsageError, "unknown option #{argument}"
    else
      # Anything else is positional. A single leading "-" stays positional
      # so negative numbers work as the json-input argument: fusion f.fsn -5
      positional << argument
    end
  end

  validate(use_case, input_mode, output_mode, error_input, inline_source, positional)
end

Instance Method Details

#error_input?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/fusion/cli/options.rb', line 55

def error_input?
  @error_input
end