Option Parser with short flags

Here is some test code with Option Parser,
it doesn’t seem to work as expected … is it a bug, or am I using it wrong,
the flags I need to use in my app it things like controls and compliance, so short flags are con or com
each time I use the long flags in this example, they give an error, guidance will be appreciated

require "option_parser"
OptionParser.parse do |parser|
  parser.banner = "Usage: crunch [arguments]"

  parser.on("-spo", "--spoon", "Prints out spoon") { puts "spoon" }
  parser.on("-spa", "--spanner", "Prints out spanner") { puts "spanner" }
  parser.on("-h", "--help", "Show this help") do
    puts parser
    exit
  end
end

I think it would make sense for the option parser to forbid the short argument from having more than 1 character as by convention that’s how long they should be. Otherwise there is no way to differentiate between your -spo option versus -s -p -o (which the option parser does not support either atm tho).

EDIT: OptionParser support for stacked options · Issue #10981 · crystal-lang/crystal · GitHub

3 Likes