Class: DataForge::CLI::Options

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



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

def initialize
  @execute = true
  @user_params = {}
end

Instance Attribute Details

#command_scriptObject

Returns the value of attribute command_script.



51
52
53
# File 'lib/data_forge/cli/options.rb', line 51

def command_script
  @command_script
end

#executeObject

Returns the value of attribute execute.



51
52
53
# File 'lib/data_forge/cli/options.rb', line 51

def execute
  @execute
end

#user_paramsObject

Returns the value of attribute user_params.



51
52
53
# File 'lib/data_forge/cli/options.rb', line 51

def user_params
  @user_params
end

Class Method Details

.parse(args, output = STDOUT) ⇒ Object



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
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/data_forge/cli/options.rb', line 7

def self.parse(args, output = STDOUT)
  args = args.dup

  options = new

  OptionParser.new do |parser|
    parser.default_argv = args
    parser.banner = "Usage: [bundle exec] forge [options] command_script.rb"

    parser.separator ""
    parser.separator "Options:"

    parser.on("-Uname=value",
            /^(?<name>\w+)=(?<value>\S+)$/,
            "User-defined parameter value to be passed to the command script.",
            "Can be specified multiple times (with a different name).") do |_, name, value|
      options.user_params[name.to_sym] = value
    end

    parser.separator ""
    parser.separator "Common options:"

    parser.on_tail("-h", "--help", "Show this message") do
      output.puts parser
      options.execute = false
    end

    parser.on_tail("-v", "--version", "Show version information") do
      output.puts "DataForge, version #{DataForge::VERSION}"
      options.execute = false
    end
  end.parse!

  if options.execute
    raise "No command script specified" if args.empty?
    raise "More than one command script specified" unless args.size == 1
    options.command_script = args.first
  end

  options
end