Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While you can put your command-line arguments into a file, flags and all, there are better ways to remember configuration settings. </p> <p>Instead of storing the flags, use a <a href="http://www.yaml.org/" rel="nofollow">YAML</a> file. YAML is a great data format, that translates easily to Ruby hashes and objects. "<a href="http://www.yaml.org/YAML_for_ruby.html" rel="nofollow">Yaml Cookbook</a>" is a very useful page for learning the ins and outs of the format with Ruby. There are YAML parsers for a myriad other languages, making it easy to share the settings, which can be useful as a system grows.</p> <p>With a little creative code you can use your YAML as the base settings, and let your CLI flags override the stored settings.</p> <p>If you're not familiar with YAML, it's easy to get a start on the file using something like:</p> <pre><code>require 'yaml' data = { 'command' =&gt; %w[result execute chart scpfile], 'query' =&gt; ['remote command', 'unix command'], 'servername' =&gt; 'CHSXEDWHDC002', } puts data.to_yaml </code></pre> <p>Which outputs:</p> <pre><code>--- command: - result - execute - chart - scpfile query: - remote command - unix command servername: CHSXEDWHDC002 </code></pre> <p>Redirect that output to a file ending in <code>.yaml</code> and you're on your way.</p> <p>To read it back into a script use:</p> <pre><code>require 'yaml' data = YAML.load_file('path/to/data.yaml') </code></pre> <p>A quick round-trip test shows:</p> <pre><code>require 'yaml' data = { 'command' =&gt; %w[result execute chart scpfile], 'query' =&gt; ['remote command', 'unix command'], 'servername' =&gt; 'CHSXEDWHDC002', } YAML.load(data.to_yaml) </code></pre> <p>Which looks like:</p> <pre><code>{"command"=&gt;["result", "execute", "chart", "scpfile"], "query"=&gt;["remote command", "unix command"], "servername"=&gt;"CHSXEDWHDC002"} </code></pre> <p>If you want to have defaults, stored in the YAML file, and override them with command-line flags, read the data from the file then use that resulting object as the base for OptionParse:</p> <pre><code>require 'optparse' require 'yaml' # Note, YAML can deal with symbols as keys, but other languages might not like them. options = { :comd =&gt; %w[result execute chart scpfile], :query =&gt; ['remote command', 'unix command'], :hname =&gt; 'CHSXEDWHDC002', } # we'll overwrite the options variable to pretend we loaded it from a file. options = YAML.load(options.to_yaml) OptionParser.new do |opts| opts.on("-c", "--Command result,execue,chart,scpfile", String, "Single command to execute ") do |c| options[:comd] = c end opts.on("-q", "--query remote command, unix command", String, "performs the command on local or remote machine") do |q| options[:query] = q end opts.on("-s", "--Servername CHSXEDWHDC002 ", String, "server name to execute the command") do |v| options[:hname] = v end opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end end.parse! </code></pre> <p>That's not tested, but we do similar things at work all the time, so save it to a file and poke at it with a stick for a while, and see what you come up with.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload