Required Options #5

florianpilz opened this Issue Nov 28, 2011 · 2 comments

1 participant


Currently all options are, well, optional. This is due to the fact that a default value must be given to set the required type.
It would be nice to be able to have options which must be given by the user, therefore have no default.

This is important for scripts which require a file to work on, since a default value would make no sense and lead to a crash if no counter-measurements are taken. (For example using "" as default and check for length > 0 or the like.)

@florianpilz florianpilz was assigned Nov 28, 2011

Another idea: Parsing the last / the only command line parameter without an identifier like --file.

For example to parse a command line call like ruby program.rb -v --intensity 3 /path/to/file. Maybe even with the file anywhere between other options. This would require some thought, e.g. if only a single unidentified option should be allowed and so on.


It never occurred to me, that all unparsed arguments are left in the ARGV array. Therefore one could use micro-optparse to handle all arguments & switches and use the remaining arguments as if they were the required arguments. The following example should explain my thoughts:

require "rubygems"
require "micro-optparse"

options = do |p|
  p.option :meal, "Choose Meal", :default => "CucumberSalad"

raise ArgumentError, "No files given!" unless ARGV.size > 0
file = ARGV.shift

Example call: ./script --meal BrainSandwich file1.txt file2.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment