self.parse_options

in files/gitlab-ctl-commands-ee/lib/patroni.rb [31:163]


  def self.parse_options(args)
    loop do
      break if args.shift == 'patroni'
    end

    options = {}

    global = OptionParser.new do |opts|
      opts.banner = 'patroni [options] command [options]'
      opts.on('-q', '--quiet', 'Silent or quiet mode') do |q|
        options[:quiet] = q
      end
      opts.on('-v', '--verbose', 'Verbose or debug mode') do |v|
        options[:verbose] = v
      end
      opts.on('-h', '--help', 'Usage help') do
        Utils.warn_and_exit usage
      end
    end

    commands = {
      'bootstrap' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
        opts.on('--scope=SCOPE', 'Name of the cluster to be bootstrapped') do |scope|
          options[:scope] = scope
        end
        opts.on('--datadir=DATADIR', 'Path to the data directory of the cluster instance to be bootstrapped') do |datadir|
          options[:datadir] = datadir
        end
        opts.on('--srcdir=SRCDIR', 'Path to the configuration source directory') do |srcdir|
          options[:srcdir] = srcdir
        end
      end,
      'check-leader' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
      end,
      'check-replica' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
      end,
      'check-standby-leader' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
      end,
      'members' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
      end,
      'pause' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
        opts.on('-w', '--wait', 'Wait until pause is applied on all nodes') do |w|
          options[:wait] = w
        end
      end,
      'resume' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
        opts.on('-w', '--wait', 'Wait until pause is cleared on all nodes') do |w|
          options[:wait] = w
        end
      end,
      'failover' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
        opts.on('--master [MASTER]', 'The name of the current master') do |m|
          options[:master] = m
        end
        opts.on('--candidate [CANDIDATE]', 'The name of the candidate') do |c|
          options[:candidate] = c
        end
      end,
      'switchover' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
        opts.on('--master [MASTER]', 'The name of the current master') do |m|
          options[:master] = m
        end
        opts.on('--candidate [CANDIDATE]', 'The name of the candidate') do |c|
          options[:candidate] = c
        end
        opts.on('--scheduled [SCHEDULED]', 'Schedule of switchover') do |t|
          options[:scheduled] = t
        end
      end,
      'restart' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
      end,
      'reload' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
      end,
      'reinitialize-replica' => OptionParser.new do |opts|
        opts.on('-h', '--help', 'Prints this help') do
          Utils.warn_and_exit opts
        end
        opts.on('--member [MEMBER]', 'The cluster member name to reinitialize') do |member|
          options[:member] = member
        end
        opts.on('-w', '--wait', 'Wait until reinitialization completes') do |wait|
          options[:wait] = wait
        end
      end
    }

    global.order! args
    command = args.shift

    raise OptionParser::ParseError, "Patroni command is not specified." \
      if command.nil? || command.empty?

    raise OptionParser::ParseError, "Unknown Patroni command: #{command}" \
      unless commands.key? command

    options[:command] = command
    commands[command].order! args
    options
  end