diff options
author | Lee Jarvis <lee@jarvis.co> | 2010-12-10 23:33:03 +0000 |
---|---|---|
committer | Lee Jarvis <lee@jarvis.co> | 2010-12-10 23:33:03 +0000 |
commit | 0be001d3ba9915e011b5c31dd44852a6a210301f (patch) | |
tree | a14bd80bc3eb2768cc385810dc5b1f638ada7d9e /lib | |
parent | 3eb01255d88d77d9e09908e886fd9c3aadc44d0f (diff) | |
download | slop-0be001d3ba9915e011b5c31dd44852a6a210301f.tar.gz |
added better formatting to Option#to_s with indentation
Diffstat (limited to 'lib')
-rw-r--r-- | lib/slop/option.rb | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/lib/slop/option.rb b/lib/slop/option.rb index 0d4310e..8466cb5 100644 --- a/lib/slop/option.rb +++ b/lib/slop/option.rb @@ -16,6 +16,8 @@ class Slop # @return [Proc] attr_reader :callback + @@max_option_size = 0 + # @param [Hash] options Option attributes # @option options [Symbol,#to_s] :flag # @option options [Symbol,#to_s] :option @@ -44,6 +46,16 @@ class Slop @limit = options[:limit] || 0 @argument_value = nil + + if @option + if requires_argument? + size = (@option.size * 2) + 4 + else + size = @option.size + 2 + end + + @@max_option_size = size if @@max_option_size < size + end end # Set the argument value @@ -134,13 +146,36 @@ class Slop @option || @flag end + # @todo Write specs for the output string def to_s str = "\t" - str << "-#{@flag}" if @flag - str << "\t" - str << "--#{@option}\t\t" if @option - str << "#{@description}" if @description - str << "\n" + + if @flag + str << "-#{@flag}" + else + str << " " * 4 + end + + if @option + str << ", " if @flag + optionstr = "--#{@option}" + + if requires_argument? + if optional_argument? + optionstr << " [#{@option}]" + else + optionstr << " <#{@option}>" + end + end + size_diff = @@max_option_size - ((@option.size * 2) + 4) + optionstr << " " * size_diff + str << optionstr + else + str << " " * (@@max_option_size + 2) + end + + str << "\t#{@description}" if @description + str end def inspect |