Sha256: 49907c83510674b5e4dd9fc00b30517acbf2280ea6917f6d15cf7346368de351

Contents?: true

Size: 1.94 KB

Versions: 9

Compression:

Stored size: 1.94 KB

Contents

module MTX
  
  class OptionsParser
    
    def self.parse(args, *required_by_command)
      uri_options = {}
      command_options = {}

      parser = OptionParser.new do |opts|
        opts.banner = "Usage: mtx [options]"
        opts.on("--transition TRANSITION", "Transition name.") do |transition|
          command_options[:transition] = transition
        end

        opts.on("--card CARD", "Card number.") do |card|
          command_options[:card] = card
        end

        opts.on("--properties ARGS", Array, "User-entered properties and values for the transition in array format. Must be an even number of comma-delimited values, like \"A,B,'C with spaces','D with spaces'\".") do |args|
          command_options[:properties] = args.in_groups_of(2).map { |key, value| {'name' => key, 'value' => value} }
        end
      
        opts.on("--comment COMMENT", "Transition comment. This may be required depending on your transition settings.") do |comment|
          command_options[:comment] = comment
        end

        opts.on("--username USERNAME", "Mingle username.") do |username|
          uri_options[:username] = username
        end

        opts.on("--password PASSWORD", "Mingle password.") do |password|
          uri_options[:password] = password
        end

        opts.on("--host_port HOST_PORT", "Host and port.") do |host_and_port|
          uri_options[:host_and_port] = host_and_port
        end

        opts.on("--project PROJECT", "Project name.") do |project|
          uri_options[:project] = project
        end
      end
    
      parser.parse! args

      ([:project, :host_and_port] | required_by_command).each do |arg|
        unless command_options[arg] || uri_options[arg]
          # TODO: let commands handle their own errors
          $stderr.puts "Missing command-line argument --#{arg.to_s}, use --help for command-line options."
          exit 1
        end
      end

      [uri_options, command_options]
    end
  end
  
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
schubert-minglr-1.3.0 lib/minglr/mtx/options_parser.rb
schubert-minglr-1.3.1 lib/minglr/mtx/options_parser.rb
schubert-minglr-1.3.11 lib/minglr/mtx/options_parser.rb
schubert-minglr-1.3.2 lib/minglr/mtx/options_parser.rb
schubert-minglr-1.3.3 lib/minglr/mtx/options_parser.rb
schubert-minglr-1.3.5 lib/minglr/mtx/options_parser.rb
schubert-minglr-1.3.8 lib/minglr/mtx/options_parser.rb
schubert-minglr-1.3.9 lib/minglr/mtx/options_parser.rb
minglr-1.3.11 lib/minglr/mtx/options_parser.rb