Note that there are some explanatory texts on larger screens.

plurals
  1. POSuppress short arguments with OptionParser
    primarykey
    data
    text
    <p>I have a Ruby app with a relatively broad set of command-line arguments. I would like to suppress "short" variants for a number of options so that they can only be used in the long ("double dash") form.</p> <p>Can I somehow suppress short dash variants for some options?</p> <p><strong>UPDATE 2013/10/08</strong></p> <p>It turned out that indeed omitting the short variant works! However, for me it didn't because for some reason in my program all the short keys were prefixed with a space. So a simple case like this:</p> <pre><code>require 'optparse' op = OptionParser.new op.on(" -f", "--from FORMAT", "Use the specific format") {} op.on("--flip", "Do a flip") {} op.parse! </code></pre> <p>caused the exception:</p> <pre><code>ruby why.rb -f some-foos why.rb:17:in `&lt;main&gt;': ambiguous option: -f (OptionParser::AmbiguousOption </code></pre> <p>while the advice given (note the lack of space after the opening quote):</p> <pre><code>require 'optparse' OptionParser.new do |opts| opts.on("-d", "--ding DING", "Should not conflict with dangerous-option") do puts "ding set!" end opts.on("--dangerous-option", "Set dangerous option") do |v| puts "dangerous option set to #{v}" end end.parse! </code></pre> <p>works fine.</p> <pre><code>$ruby dang.rb -d xyz ding set! </code></pre> <p>So thanks <strong>p11y</strong> for pointing me in the right direction with a working example. Also, if this "leading space" is in place, optparse will not complain - but it will change the interpretation of your short keys (or, better to say, will ignore them and show them as part of your help line! - and still use the auto-generated keys instead).</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. 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