lib/bolt/cli.rb
# TODO: Move most of this to the parser.
#
# Parse the command and validate options. All errors that are raised here
# are not handled by the outputter, as it relies on config being loaded.
#
Move most of this to the parser.
Parse the command and validate options. All errors that are raised here
are not handled by the outputter, as it relies on config being loaded.
Move most of this to the parser . Parse the command and validate options . All errors that are raised here are not handled by the outputter as it relies on config being loaded .
def parse
      with_error_handling do
        options = {}
        parser  = BoltOptionParser.new(options)
        remaining = parser.permute(@argv) unless @argv.empty?
        if @argv.empty? || help?(options, remaining)
          options[:subcommand] = nil unless COMMANDS.include?(options[:subcommand])
          if Bolt::Util.first_run?
            FileUtils.touch(Bolt::Util.first_runs_free)
            if options[:subcommand].nil? && $stdout.isatty
              welcome_message
              raise Bolt::CLIExit
            end
          end
          parser.update
          puts parser.help
          raise Bolt::CLIExit
        end
        if options[:version]
          puts Bolt::VERSION
          raise Bolt::CLIExit
        end
        options[:object] = remaining.shift
        if options[:subcommand] == 'command' && options[:object]
          options[:object] = Bolt::Util.get_arg_input(options[:object])
        end
        if %w[task plan].include?(options[:subcommand])
          params, remaining = remaining.partition { |s| s =~ /.+=/ }
          if options[:params]
            unless params.empty?
              raise Bolt::CLIError,
                    "Parameters must be specified through either the --params " \
                    "option or param=value pairs, not both"
            end
            options[:params_parsed] = true
          elsif params.any?
            options[:params_parsed] = false
            options[:params] = Hash[params.map { |a| a.split('=', 2) }]
          else
            options[:params_parsed] = true
            options[:params] = {}
          end
        end
        options[:leftovers] = remaining
        unless options.key?(:verbose)
          options[:verbose] = options[:subcommand] != 'plan'
        end
        validate(options)
        validate_ps_version
        options
      end
    end
def parse with_error_handling do options = { } parser = BoltOptionParser . new ( options ) remaining = parser . permute ( @argv ) unless @argv . empty? if @argv . empty? || help? ( options , remaining ) options [ :subcommand ] = nil unless COMMANDS . include? ( options [ :subcommand ] ) if Bolt :: Util . first_run? FileUtils . touch ( Bolt :: Util . first_runs_free ) if options [ :subcommand ] . nil? && $stdout . isatty welcome_message raise Bolt :: CLIExit end end parser . update puts parser . help raise Bolt :: CLIExit end if options [ :version ] puts Bolt :: VERSION raise Bolt :: CLIExit end options [ :object ] = remaining . shift if options [ :subcommand ] == 'command' && options [ :object ] options [ :object ] = Bolt :: Util . get_arg_input ( options [ :object ] ) end if %w[ task plan ] . include? ( options [ :subcommand ] ) params , remaining = remaining . partition { | s | s =~ / .+= / } if options [ :params ] unless params . empty? raise Bolt :: CLIError , "Parameters must be specified through either the --params " "option or param=value pairs, not both" end options [ :params_parsed ] = true elsif params . any? options [ :params_parsed ] = false options [ :params ] = Hash [ params . map { | a | a . split ( '=' , 2 ) } ] else options [ :params_parsed ] = true options [ :params ] = { } end end options [ :leftovers ] = remaining unless options . key? ( :verbose ) options [ :verbose ] = options [ :subcommand ] != 'plan' end validate ( options ) validate_ps_version options end end
TODO: Move most of this to the parser.
TODO : Move most of this to the parser .
# This part aims to handle both `bolt <mode> --help` and `bolt help <mode>`. # If the subcommand is not enabled, display the default # help text # Update the parser for the subcommand (or lack thereof) # Handle reading a command from a file # Only parse params for task or plan # Default to verbose for everything except plans