Module: Rbcli::Parser

Defined in:
lib/rbcli/engine/parser.rb

Class Method Summary collapse

Class Method Details

.parseObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
90
91
# File 'lib/rbcli/engine/parser.rb', line 29

def self.parse
  # We show deprecation warnings before anything else, encouraging the developer to update their code.
  Rbcli::DeprecationWarning.display_warnings

  @cliopts = Trollop::options do
    data = Rbcli.configuration(:me)
    version "#{data[:scriptname]} version: #{data[:version]}"
    banner "\#{data[:description]}\nFor more information on individual commands, run `\#{data[:scriptname]} <command> -h`.\n\nUsage:\n     \#{data[:scriptname]} [options] command [parameters]\n\nCommands:\n\#{Rbcli::Command.descriptions 6, 21}\n\n[options]:\n    EOS\n    data[:options].each do |name, opts|\n      opts[:default] = nil unless opts.key? :default\n      opts[:required] = false unless opts.key? :required\n      opts[:permitted] = nil unless opts.key? :permitted\n      opt name.to_sym, opts[:description], type: opts[:type], default: opts[:default], required: opts[:required], permitted: opts[:permitted], short: opts[:short]\n    end\n    if data[:remote_execution]\n      opt :remote_exec, 'Remote user@host:port to execute command on', type: :string, default: nil\n      opt :identity, 'Identity for remote execution', type: :string, default: nil\n    end\n    opt :json_output, 'Output result in machine-friendly JSON format', short: :none, type: :boolean, default: false if data[:allow_json]\n    opt :config_file, 'Specify a config file manually', short: :none, type: :string, default: data[:config_userfile] unless data[:config_userfile].nil?\n    opt :generate_config, 'Generate a new config file', short: :none unless data[:config_userfile].nil? #defaults to false\n    stop_on Rbcli::Command.commands.keys\n  end\n\n  @cmd = [ARGV.shift] # get the subcommand\n  if @cliopts[:generate_config]\n    Rbcli::Config::generate_userconf @cliopts[:config_file]\n    puts \"User config generated at \#{@cliopts[:config_file]} using default values.\"\n  elsif @cmd[0].nil?\n    default_action = Rbcli.configuration(:me, :default_action) || Rbcli.configuration(:hooks, :default_action)\n    if default_action.nil?\n      Trollop::educate\n    else\n      default_action.call @cliopts\n    end\n  elsif Rbcli::Command.commands.key? @cmd[0]\n    @cmd << Rbcli::Command.commands[@cmd[0]].class.parseopts\n\n    if (@cliopts[:remote_exec_given] and not @cliopts[:identity_given]) or (not @cliopts[:remote_exec_given] and @cliopts[:identity_given])\n      Trollop::die 'Must use `--remote-exec` and `--identity` together.'\n    end\n\n    Rbcli.configuration(:me, :pre_hook).call @cliopts unless Rbcli.configuration(:me, :pre_hook).nil?\n    Rbcli.configuration(:hooks, :pre_hook).call @cliopts unless Rbcli.configuration(:hooks, :pre_hook).nil?\n    Rbcli::Command.runcmd(@cmd.shift, @cmd[0], @cliopts)\n    Rbcli.configuration(:me, :post_hook).call @cliopts unless Rbcli.configuration(:me, :post_hook).nil?\n    Rbcli.configuration(:hooks, :post_hook).call @cliopts unless Rbcli.configuration(:hooks, :post_hook).nil?\n  else\n    Trollop::die \"Unknown subcommand \#{@cmd[0].inspect}\"\n  end\n\nend\n"