Saturday, August 22, 2020

Use OptionParser to Parse Commands in Ruby

Use OptionParser to Parse Commands in Ruby In the article talking about OptionParsers highlights we examined a portion of the reasons that make utilizing OptionParser in Ruby desirable over glancing through ARGV physically to parse orders by hand. Presently its opportunity to get down to figuring out how to utilize OptionParser and its highlights. The accompanying standard code will be utilized for all the models in this instructional exercise. To attempt any of the models, just put the models opts.on hinder close to the TODO remark. Running the program will print the condition of the choices has and ARGV, permitting you to look at the impacts of your switches. #!/usr/canister/env rubyrequire optparserequire pp# This hash will hold the entirety of the options# parsed from the order line by# OptionParser.options {}optparse OptionParser.new do|opts|# TODO: Put order line choices here# This shows the assistance screen, all projects are# expected to have this option.opts.on( - h, help, Display this screen ) doputs optsexitendend# Parse the order line. Recall there are two forms# of the parse technique. The parse strategy just parses# ARGV, while the parse! technique parses ARGV and removes# any alternatives discovered there, just as any parameters for# the choices. Whats left is the rundown of documents to resize.optparse.parse!pp Options:, optionspp ARGV:, ARGV Straightforward Switch A straightforward switch is a contention with no discretionary structures or no parameters. The impact will be to just set a banner in the alternatives hash. No different parameters will be passed to the on technique. options[:simple] falseopts.on( - s, basic, Simple contention ) dooptions[:simple] trueend Switch with Mandatory Parameter Switches that take a parameter just need to express the parameter name in the long type of the switch. For instance, - f, record FILE implies the - f or document switch takes a solitary parameter called FILE, and this parameter is required. You can't utilize either - f or record without additionally passing it a parameter. options[:mand] opts.on( - m, obligatory FILE, Mandatory contention ) do|f|options[:mand] battle Switch with Optional Parameter Switch parameters dont must be obligatory, they can be discretionary. To pronounce a switch parameter discretionary, place its name in sections in the switch depiction. For instance, logfile [FILE] implies the FILE parameter is discretionary. If not provided, the program will accept a normal default, for example, a document called log.txt. In the model, the colloquialism a b || c is utilized. This is only shorthand for a b, however on the off chance that b is bogus or nil, a c. options[:opt] falseopts.on( - o, discretionary [OPT], Optional contention ) do|f|options[:opt] f || nothingend Consequently Convert to Float OptionParser can consequently change over contention to certain kinds. One of these sorts is Float. To consequently change over your contentions to a change to Float, pass Float to the on strategy after your switch portrayal strings. Programmed transformations are convenient. In addition to the fact that they save you the progression of changing over the string to the ideal kind, yet additionally check the configuration for you and will toss a special case on the off chance that it is arranged mistakenly. options[:float] 0.0opts.on( - f, glide NUM, Float, Convert to drift ) do|f|options[:float] battle Some different kinds that OptionParser can change over to naturally incorporate Time and Integer. Arrangements of Arguments Contentions can be deciphered as records. This can be viewed as changing over to an exhibit, as you changed over to Float. While your alternative string can characterize the parameter to be called a,b,c, OptionParser will indiscriminately permit any number of components in the rundown. Along these lines, on the off chance that you need a particular number of components, make certain to check the cluster length yourself. options[:list] []opts.on( - l, list a,b,c, Array, List of parameters ) do|l|options[:list] loan Set of Arguments Some of the time it bodes well to confine contentions to a change to a couple of decisions. For instance, the accompanying switch will just take a solitary compulsory parameter, and the parameter must be one of indeed, no or possibly. On the off chance that the parameter is whatever else by any means, a special case will be tossed. To do this, pass a rundown of satisfactory parameters as images after the switch portrayal strings. options[:set] :yesopts.on( - s, set OPT, [:yes, :no, :maybe], Parameters from a set ) do|s|options[:set] send Refuted Forms Switches can have a refuted structure. The switch nullified can have one that does the contrary impact, called no-refuted. To portray this in the switch depiction string, place the elective segment in sections: [no-]negated. In the event that the main structure is experienced, genuine will be passed to the square, and bogus will be blocked if the subsequent structure is experienced. options[:neg] falseopts.on( - n, [no-]negated, Negated structures ) do|n|options[:neg] nend

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.