Class: CkuruTools::ArgsProcessor
- Inherits:
-
HashInitializerClass
- Object
- HashInitializerClass
- CkuruTools::ArgsProcessor
- Defined in:
- lib/args.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#noparse ⇒ Object
Returns the value of attribute noparse.
Instance Method Summary collapse
-
#initialize(h) ⇒ ArgsProcessor
constructor
A simple, terse, and concise wrapper around GetoptLong command line parsing.
- #parse ⇒ Object
Constructor Details
#initialize(h) ⇒ ArgsProcessor
A simple, terse, and concise wrapper around GetoptLong command line parsing.
Lets start with an example:
args = CkuruTools::ArgsProcessor.new(:args => [“-flag”,“value”,“Required_value+”])
response = args.parse
Now if the script where to be called with
myscript -f -v VALUE –Required_value required
response => => true, :value => “VALUE”, :Required_value => “required”
Defaults: -h : always generates help (do not use this flag) -d : Interface into CkuruTools::Debug (increments debugging level); (do not override this flag)
48 49 50 |
# File 'lib/args.rb', line 48 def initialize(h) super h end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
25 26 27 |
# File 'lib/args.rb', line 25 def args @args end |
#noparse ⇒ Object
Returns the value of attribute noparse.
26 27 28 |
# File 'lib/args.rb', line 26 def noparse @noparse end |
Instance Method Details
#parse ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/args.rb', line 52 def parse argsparse, argshash = [], {} argsparse.push(["--debug","-d",GetoptLong::NO_ARGUMENT], ["--help","-h",GetoptLong::NO_ARGUMENT]) args.each do |arg| if matchdata = arg.match(/-(([a-zA-Z0-9])[a-zA-Z0-9]+)/) short = "-#{matchdata[2]}" long = "-#{arg}" argsparse.push([long,short,GetoptLong::NO_ARGUMENT]) argshash[long] = ArgParsed.new(:spec => arg, :type => :flag, :long => long, :short => short, :required => false) elsif matchdata = arg.match(/(([a-zA-Z0-9])[a-zA-Z0-9]+)/) short = "-#{matchdata[2]}" long = "--#{arg.stripplusses}" argsparse.push([long,short,GetoptLong::REQUIRED_ARGUMENT]) argshash[long] = ArgParsed.new(:spec => arg, :type => :value, :long => long, :short => short, :required => arg.match(/\+/) ? true : false) else raise "unrecognized arg spec #{arg}" end end ret = {} opts = GetoptLong.new *argsparse opts.each do |opt, arg| if opt == '--debug' CkuruTools::Debug.instance.level = CkuruTools::Debug.instance.level + 1 ckebug 1, "incrementing debug level" elsif opt == '--help' puts "Options are:" argshash.keys.each do |a| puts "\t#{argshash[a].long},#{argshash[a].short}" + (argshash[a].required ? " (required)" : '') end exit 0 else ckebug 1, "parsing #{opt}, #{arg}" case argshash[opt].type when :flag ret[argshash[opt].long.stripdashes.to_sym] = true when :value ret[argshash[opt].long.stripdashes.to_sym] = arg end end end missing_required = [] argshash.keys.each do |a| if argshash[a].required missing_required.push a unless ret.has_key? a.stripdashes.to_sym end end ckebug 1, ret.inspect if missing_required.length > 0 missing_required.each do |m| ckebug 0, "#{argshash[m].long}(#{argshash[m].short}) is required" end raise "missing required values" end ret end |