Class: Pablo::Token
- Inherits:
-
Object
- Object
- Pablo::Token
- Defined in:
- lib/pablo.rb
Overview
The base class for all parsing tokens, i.e. Options and Commands.
Each token is expected to implement a mark and a parse method. The mark method is expected to mark all the occurrences of the command with a reference to it’s self
object. The parse method will get all the parameters (including subcommands) for consuming.
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#default ⇒ Object
Returns the value of attribute default.
-
#ldesc ⇒ Object
readonly
Returns the value of attribute ldesc.
-
#sdesc ⇒ Object
readonly
Returns the value of attribute sdesc.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#default! ⇒ Object
Whether or not the current token is a default token.
-
#initialize(toplevel) ⇒ Token
constructor
Initializes the structure so it’s ready for processing.
-
#long_desc(desc) ⇒ Object
Call this to provide a longer description of what the token does.
-
#mark(args) ⇒ Object
Marks all occurences of this command’s aliases.
-
#mark_children(args) ⇒ Object
Goes through @tokens and let’s each of them mark the args.
-
#name(a) ⇒ Object
Adds the alias
a
to the list of aliases for this token. -
#parse(args, &block) ⇒ Object
A very basic implementation of parse that can be utilized by subclasses to make their code look tidier.
-
#parse_children(args) ⇒ Object
Goes through @tokens and let’s each of them parse the args.
-
#run(&block) ⇒ Object
Sets the
block
to run on discovery of the token. -
#run! ⇒ Object
Run the associated block explicitly.
-
#short_desc(desc) ⇒ Object
Call this to provide a short descriptive text for the
--help/-h/help
command. -
#unmark(args) ⇒ Object
Removes all markings from the
args
so they can be passed to the @run block.
Constructor Details
#initialize(toplevel) ⇒ Token
Initializes the structure so it’s ready for processing
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/pablo.rb', line 98 def initialize toplevel @toplevel = toplevel @toplevel.everyone << self if self != @toplevel @aliases = [] @run = lambda {} @requests = [] @tokens = [] @default = false @sdesc = "" @ldesc = "" end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
126 127 128 |
# File 'lib/pablo.rb', line 126 def aliases @aliases end |
#default ⇒ Object
Returns the value of attribute default.
220 221 222 |
# File 'lib/pablo.rb', line 220 def default @default end |
#ldesc ⇒ Object (readonly)
Returns the value of attribute ldesc.
254 255 256 |
# File 'lib/pablo.rb', line 254 def ldesc @ldesc end |
#sdesc ⇒ Object (readonly)
Returns the value of attribute sdesc.
244 245 246 |
# File 'lib/pablo.rb', line 244 def sdesc @sdesc end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
110 111 112 |
# File 'lib/pablo.rb', line 110 def tokens @tokens end |
Instance Method Details
#default! ⇒ Object
Whether or not the current token is a default token. I.e. it will be called in case no (recognized) arguments were supplied for the parent command (or the entire argument string if this is a level 1 token).
Note: The blocks of default tokens’ @run blocks will be supplied with an empty array as a single argument when run as a default.
219 |
# File 'lib/pablo.rb', line 219 def default!; @default = true; end |
#long_desc(desc) ⇒ Object
Call this to provide a longer description of what the token does. This will be displayed when the user calls –help/-h/help command.
250 251 252 |
# File 'lib/pablo.rb', line 250 def long_desc desc @ldesc = desc end |
#mark(args) ⇒ Object
Marks all occurences of this command’s aliases
131 132 133 134 135 136 137 138 |
# File 'lib/pablo.rb', line 131 def mark args args.collect { |pair| @aliases.each { |a| pair[0] << self if pair[1] == a } pair } mark_children args end |
#mark_children(args) ⇒ Object
Goes through @tokens and let’s each of them mark the args.
196 197 198 199 200 |
# File 'lib/pablo.rb', line 196 def mark_children args org_args = args.dup @tokens.each { |t| args = t.mark args } args end |
#name(a) ⇒ Object
Adds the alias a
to the list of aliases for this token.
122 123 124 |
# File 'lib/pablo.rb', line 122 def name a @aliases << a.to_s end |
#parse(args, &block) ⇒ Object
A very basic implementation of parse that can be utilized by subclasses to make their code look tidier.
Basically goes through the args
and removes the consumed arguments if there are any and calls the @run block if there is any.
If you supply a block
it will be executed instead of the @run block.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/pablo.rb', line 149 def parse args, &block # if no block has been given, we do the default # watch out here: if you use a name that is already present (like args) for # the block argument variable, it will actually use the already present # variable! (not nice!) block = lambda { |argus| @run.call argus } unless block_given? return args if ( not @run and not block_given? ) or # no need to parse if we don't run anything... @aliases.empty? # ...or don't have any names anyways. # find ourselves myself = args.index { |pair| pair[0].include? self } # if we're not present, we can go return args unless myself # where the next mark is stop = args[myself+1..-1].index { |pair| not pair[0].empty? } stop += myself+1 if stop # which arguments we will consume myargs = args[myself+1...stop] unless stop.nil? myargs = args[myself+1..-1] if stop.nil? # and call the block block.call unmark(myargs) # consume the arguments if there are any (0..args.length-1).each { |i| # remove ourselves from our call args[i][0].delete self if myself == i # if others have registered for these arguments as well, let them have # their fair share of the cake. if args[myself] and args[myself][0].empty? # remove the whole call if there's noone else requesting it args[i] = nil if myself == i # remove our arguments args[i] = nil if myself < i and ( not stop or i < stop ) end } args.reject! { |a| a.nil? } end |
#parse_children(args) ⇒ Object
Goes through @tokens and let’s each of them parse the args.
205 206 207 208 |
# File 'lib/pablo.rb', line 205 def parse_children args @tokens.each { |t| args = t.parse args } args end |
#run(&block) ⇒ Object
Sets the block
to run on discovery of the token.
115 116 117 |
# File 'lib/pablo.rb', line 115 def run &block @run = block if block_given? end |
#run! ⇒ Object
Run the associated block explicitly. Used when running default commands.
226 227 228 |
# File 'lib/pablo.rb', line 226 def run! @run.call [] if @run end |
#short_desc(desc) ⇒ Object
Call this to provide a short descriptive text for the --help/-h/help
command
240 241 242 |
# File 'lib/pablo.rb', line 240 def short_desc desc @sdesc = desc end |
#unmark(args) ⇒ Object
Removes all markings from the args
so they can be passed to the @run block.
233 234 235 |
# File 'lib/pablo.rb', line 233 def unmark args args.collect { |pair| pair[1] } end |