jwaldrip / admiral.cr

A robust DSL for writing command line interfaces written in Crystal.
https://jwaldrip.github.com/admiral.cr
MIT License
135 stars 14 forks source link

Negative numbers not accepted as argument #21

Closed akrejczinger closed 4 years ago

akrejczinger commented 5 years ago

This is probably related to #15 . Example code below:

require "admiral"

class NegativeNumber < Admiral::Command
  define_flag typed_int : Int32, required: true
  define_argument typed_arg : Int32, required: true

  def run
    puts flags.typed_int
    puts arguments.typed_arg
  end
end

puts "Negative flag:"
NegativeNumber.run "2 --typed-int -1"  # negative number works for flags
puts "Negative argument:"
NegativeNumber.run "-2 --typed-int 1"  # negative number produces a traceback for arguments

The typed_arg argument is defined as Int32, which can be positive or negative. But if I try to input a negative argument to it, a traceback is shown. The output of the above code:

Negative flag:
-1
2
Negative argument:
1
Unhandled exception: Nil assertion failed (NilAssertionError)
  from /usr/lib/crystal/nil.cr:106:5 in 'not_nil!'
  from src/negative.cr:5:3 in 'typed_arg'
  from src/negative.cr:9:5 in 'run'
  from src/negative.cr:3:1 in 'parse_and_run'
  from src/negative.cr:3:1 in 'run'
  from src/negative.cr:16:1 in '__crystal_main'
  from /usr/lib/crystal/crystal/main.cr:97:5 in 'main_user_code'
  from /usr/lib/crystal/crystal/main.cr:86:7 in 'main'
  from /usr/lib/crystal/crystal/main.cr:106:3 in 'main'
  from __libc_start_main
  from _start
  from ???
jwaldrip commented 4 years ago

This one is tough. Technically the negative number is trying to be referenced as a short flag. There a couple of options here: