Skip to content

Commit

Permalink
Move OptionParser extensions out of Cmdparse scope
Browse files Browse the repository at this point in the history
  • Loading branch information
gettalong committed Dec 28, 2016
1 parent b72743b commit 71e8665
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions lib/cmdparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@
OptionParser::Officious.delete('version')
OptionParser::Officious.delete('help')


# Extension for OptionParser objects to allow access to some internals.
class OptionParser #:nodoc:

# Access the option list stack.
attr_reader :stack

# Returns +true+ if at least one local option is defined.
#
# The zeroth stack element is not respected when doing the query because it contains either the
# OptionParser::DefaultList or a CmdParse::MultiList with the global options of the
# CmdParse::CommandParser.
def options_defined?
stack[1..-1].each do |list|
list.each_option do |switch|
return true if ::OptionParser::Switch === switch && (switch.short || switch.long)
end
end
false
end

# Returns +true+ if a banner has been set.
def banner?
!@banner.nil?
end

end


# Namespace module for cmdparse.
#
# See CmdParse::CommandParser and CmdParse::Command for the two important classes.
Expand Down Expand Up @@ -125,33 +154,6 @@ def #{mname}(*args, &block) #:nodoc:

end

# Extension for OptionParser objects to allow access to some internals.
class ::OptionParser #:nodoc:

# Access the option list stack.
attr_reader :stack

# Returns +true+ if at least one local option is defined.
#
# The zeroth stack element is not respected when doing the query because it contains either the
# OptionParser::DefaultList or a CmdParse::MultiList with the global options of the
# CmdParse::CommandParser.
def options_defined?
stack[1..-1].each do |list|
list.each_option do |switch|
return true if ::OptionParser::Switch === switch && (switch.short || switch.long)
end
end
false
end

# Returns +true+ if a banner has been set.
def banner?
!@banner.nil?
end

end

# === Base class for commands
#
# This class implements all needed methods so that it can be used by the CommandParser class.
Expand Down

0 comments on commit 71e8665

Please sign in to comment.