Class: TreasureData::Command::List::CommandOption
- Inherits:
-
OptionParser
- Object
- OptionParser
- TreasureData::Command::List::CommandOption
- Defined in:
- lib/td/command/list.rb
Instance Attribute Summary collapse
-
#argv ⇒ Object
Returns the value of attribute argv.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#message ⇒ Object
Returns the value of attribute message.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #banner ⇒ Object
- #cmd_parse(argv = @argv||ARGV) ⇒ Object
- #cmd_usage(msg = nil) ⇒ Object
- #group ⇒ Object
-
#initialize(name, args, description) ⇒ CommandOption
constructor
A new instance of CommandOption.
- #on(*argv) ⇒ Object
- #usage ⇒ Object
- #with_args(argv) ⇒ Object
Constructor Details
#initialize(name, args, description) ⇒ CommandOption
Returns a new instance of CommandOption.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/td/command/list.rb', line 7 def initialize(name, args, description) super() @name = name @description = description.to_s if args.last.to_s =~ /_$/ @varlen = true args.push args.pop.to_s[0..-2]+'...' elsif args.last.to_s =~ /_\?$/ @varlen = true args.push args.pop.to_s[0..-3]+'...?' end @req_args, opt_args = args.partition {|a| a.to_s !~ /\?$/ } @opt_args = opt_args.map {|a| a.to_s[0..-2].to_sym } @args = @req_args + @opt_args @usage_args = "#{@name}" @req_args.each {|a| @usage_args << " <#{a}>" } @opt_args.each {|a| @usage_args << " [#{a}]" } @has_options = false self.summary_indent = " " = "usage:\n" << " $ #{File.basename($0)} #{@usage_args}\n" << "\n" << "description:\n" @description.split("\n").each {|l| << " #{l}\n" } self. = @message = nil @argv = nil end |
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
46 47 48 |
# File 'lib/td/command/list.rb', line 46 def argv @argv end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
105 106 107 |
# File 'lib/td/command/list.rb', line 105 def description @description end |
#message ⇒ Object
Returns the value of attribute message.
46 47 48 |
# File 'lib/td/command/list.rb', line 46 def @message end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
104 105 106 |
# File 'lib/td/command/list.rb', line 104 def name @name end |
Instance Method Details
#banner ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/td/command/list.rb', line 52 def s = super.dup if @has_options s << "\n" s << "options:\n" end s << "\n" s end |
#cmd_parse(argv = @argv||ARGV) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/td/command/list.rb', line 66 def cmd_parse(argv=@argv||ARGV) parse!(argv) if argv.length < @req_args.length || (!@varlen && argv.length > @argv.length) cmd_usage nil end if argv.length <= 1 return argv[0] else return argv end rescue cmd_usage $! end |
#cmd_usage(msg = nil) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/td/command/list.rb', line 80 def cmd_usage(msg=nil) puts self. if msg puts "" puts "error: #{msg}" end exit 1 end |
#group ⇒ Object
89 90 91 |
# File 'lib/td/command/list.rb', line 89 def group name.split(':', 2).first end |
#on(*argv) ⇒ Object
93 94 95 96 |
# File 'lib/td/command/list.rb', line 93 def on(*argv) @has_options = true super end |
#usage ⇒ Object
62 63 64 |
# File 'lib/td/command/list.rb', line 62 def usage "%-40s # %s" % [@usage_args, @description] end |
#with_args(argv) ⇒ Object
98 99 100 101 102 |
# File 'lib/td/command/list.rb', line 98 def with_args(argv) d = dup d.argv = argv d end |