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 obstruct 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/container/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# accepted to have this option.opts.on( - h, help, Display this screen ) doputs optsexitendend# Parse the order line. Recollect there are two forms# of the parse strategy. The parse technique basically parses# ARGV, while the parse! strategy 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 Basic Switch A basic 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 strategy. 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 document without additionally passing it a parameter. options[:mand] opts.on( - m, required FILE, Mandatory contention ) do|f|options[:mand] fight Switch with Optional Parameter Switch parameters dont must be required, they can be discretionary. To proclaim a switch parameter discretionary, place its name in sections in the switch portrayal. For instance, logfile [FILE] implies the FILE parameter is discretionary. If not provided, the program will accept a normal default, for example, a record called log.txt. In the model, the phrase 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 Naturally Convert to Float OptionParser can naturally change over contention to certain kinds. One of these sorts is Float. To naturally change over your contentions to a change to Float, pass Float to the on strategy after your switch depiction strings. Programmed changes are helpful. In addition to the fact that they save you the progression of changing over the string to the ideal sort, yet additionally check the configuration for you and will toss a special case on the off chance that it is organized inaccurately. options[:float] 0.0opts.on( - f, coast NUM, Float, Convert to skim ) do|f|options[:float] fight Some different sorts that OptionParser can change over to consequently incorporate Time and Integer. Arrangements of Arguments Contentions can be deciphered as records. This can be viewed as changing over to a cluster, 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 Once in a while it bodes well to confine contentions to a change to a couple of decisions. For instance, the accompanying switch will just take a solitary obligatory parameter, and the parameter must be one of indeed, no or possibly. In the event that the parameter is whatever else by any means, an exemption 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 Invalidated Forms Switches can have an invalidated structure. The switch nullified can have one that does the contrary impact, called no-discredited. To depict this in the switch portrayal string, place the elective segment in sections: [no-]negated. On the off chance that the primary 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.