Class: OptparseLite::OptParser
Defined Under Namespace
Modules: Error
Classes: Response, UnparsedParamters
Instance Method Summary
collapse
Methods included from OptHelper
#dashes
Methods included from HelpHelper
#cmd, #hdr, #help_requested?, #looks_like_header?, #prefix, #txt
Constructor Details
#initialize(&block) ⇒ OptParser
Returns a new instance of OptParser.
680
681
682
683
684
685
686
|
# File 'lib/optparse-lite.rb', line 680
def initialize(&block)
@block = block
@compiled = false
@items = []
@names = {}
@specs = []
end
|
Instance Method Details
#compile! ⇒ Object
687
688
689
690
|
# File 'lib/optparse-lite.rb', line 687
def compile!
instance_eval(&@block)
@compiled = true
end
|
#doc_sexp ⇒ Object
691
692
693
694
|
# File 'lib/optparse-lite.rb', line 691
def doc_sexp
compile! unless @compiled
common_doc_sexp @items
end
|
#parse(argv) ⇒ Object
695
696
697
698
699
|
# File 'lib/optparse-lite.rb', line 695
def parse argv
opts = parse_argv argv
resp = validate_and_populate(opts)
[resp, opts]
end
|
#specs ⇒ Object
700
701
702
703
|
# File 'lib/optparse-lite.rb', line 700
def specs
compile! unless @compiled
@specs.map{|idx| @items[idx]}
end
|
#syntax_tokens ⇒ Object
704
705
706
|
# File 'lib/optparse-lite.rb', line 704
def syntax_tokens
specs.map{|x| x.syntax_tokens * ','}
end
|
#validate_and_populate(opts) ⇒ Response
this does the following: for all unrecognized opts, add one error (one error encompases all of them), populate defaults, normalize them either to the accessor or last long or last short surface form with a symbol key (maybe), make sure that opts that don’t take parameter don’t have them and opts that require them do.
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
# File 'lib/optparse-lite.rb', line 713
def validate_and_populate opts
compile! unless @compiled
resp = Response.new
sing = class << opts; self end
specs = self.specs
opts.keys.each do |key|
val = opts.delete(key) if ! @names.key?(key)
resp.unrecognized_parameter(key, val)
else
spec = specs[@names[key]]
if spec.required? && val == true
resp.required_argument_missing(spec, key)
elsif val != true && ! spec.takes_argument?
resp.argument_not_allowed(spec, key, val)
else opts[spec.normalized_key] = val
if spec.accessor
acc = spec.accessor
sing.send(:define_method, spec.accessor){ self[acc] }
end
end
end
end
these = specs.map{|s| s.has_default? ? s.normalized_key : nil }.compact
employ = these - opts.keys
employ.each{|k| opts[k]=specs.detect{|s| s.normalized_key == k}.default}
resp
end
|