
Security News
Insecure Agents Podcast: Certified Patches, Supply Chain Security, and AI Agents
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.
micro-optparse
Advanced tools
There are lots of command line parser out there, for example trollop is a great alternative. However, it is 800 lines long. In addition, trollop sucks at validating the input. So µ-optparse is for you if you are looking for
µ-optparse is a small wrapper around optparse, weighing less than 80 lines of code. optparse (or OptionParser) on the other hand is a command line parser, which ships with ruby. After you defined available options, it automatically creates a help page and is able to parse ARGV accordingly. However, optparse requires you to repeat yourself quite often, which leads to many lines of code, just to configure the available options. µ-optparse removes this obstacle by extracting the information contained in few lines of configuration. In addition, µ-optparse extends optparse by some powerful validations, which where inspired by OptiFlag.
require 'rubygems' # necessary for ruby v1.8.*
require 'micro-optparse'
options = Parser.new do |p|
p.banner = "This is a fancy script, for usage see below"
p.version = "fancy script 0.0 alpha"
p.option :severity, "set severity", :default => 4, :value_in_set => [4,5,6,7,8]
p.option :verbose, "enable verbose output"
p.option :mutation, "set mutation", :default => "MightyMutation", :value_matches => /Mutation/
p.option :plus_selection, "use plus-selection if set", :default => true
p.option :selection, "selection used", :default => "BestSelection", :short => "l"
p.option :chance, "set mutation chance", :default => 0.8, :value_satisfies => lambda {|x| x >= 0.0 && x <= 1.0}
end.process!
What this piece of code does is the following:
"--verbose" for :verbose:plus_selection becomes --plus-selectiontrue or falsevalue_in_set is given, it validates if the input value is in the given arrayvalue_matches is given, it validates if the input value matches the regular expressionvalue_satisfies is given, it validates if the lambda or Proc evaluates to true when fed with the input value:mutation in your script, write options[:mutation]The automatically generated help message looks like this:
This is a fancy script, for usage see below
-s, --severity 4 set severity
-v, --[no-]verbose enable verbose output
-m, --mutation MightyMutation set mutation
-p, --[no-]plus-selection use plus-selection if set
-l, --selection BestSelection selection used
-c, --chance 0.8 set mutation chance
-h, --help Show this message
-V, --version Print version
To show some example calls and results, I will use a simplified version of the Parser above:
require 'rubygems' # necessary for ruby v1.8.*
require 'micro-optparse'
options = Parser.new do |p|
p.option :severity, "set severity", :default => 4, :value_in_set => [4,5,6,7,8]
p.option :mutation, "set mutation", :default => "MightyMutation", :value_matches => /Mutation/
p.option :plus_selection, "use plus-selection if set", :default => true, :optional => true
end.process!
ruby myprogram.rb --help will yield a help message formatted like the one aboveruby myprogram.rb will fill the variable options with the hash {:severity => 4, :mutation => "MightyMutation"} due to the given default values (:plus_selection does not appear in the result, due to the :optional => true setting, but can still be filled by the user)ruby myprogram.rb -s 2 --mutation WeakMutation -p true will fill the variable options with the hash {:severity => 2, :mutation => "WeakMutation", :plus_selection => true}, since the given values overwrite default valuesµ-optparse can parse any array which is formatted like ARGV, e.g. ["--severity", "4"] is the same as typing "--severity 4" behind your program call.
You can even process several arrays with the same parser (see example below).
In addition, you don't need to specify all options at once, i.e. you can pass the parser around and add more options until you call the process!-method.
require 'rubygems' # necessary for ruby v1.8.*
require 'micro-optparse'
parser = Parser.new
parser.option :eat_snickers, "How many?", :default => 0
options1 = parser.process!(["--eat-snickers", "2"])
options2 = parser.process!(["--eat-snickers", "1"])
You can either go and install the gem via gem install micro-optparse or grab it from this repository and paste it into your script.
If you choose the latter option, you may delete the validate-method to spare another 15 lines of code.
If you want to contribute, you can fork this repository, make your changes and send me a pull request. However, improvements must be one of the following:
You must define default values, if the option should accept an argument. Every option without a default value (or with true or false as default) is treated as a switch: true if given and false / default otherwise.
No it's not. Every option that has no default argument is a switch and if an option has a default argument, well there is a default to fall back to. However, what you can do is using µ-optparse to parse all options and switches (which are then removed from the ARGV array) and use everything that remains in ARGV as the mandatory arguments. Of course you have to raise an error yourself if no argument is left.
Consider the following example to implement mandatory arguments yourself:
require 'rubygems' # necessary for ruby v1.8.*
require 'micro-optparse'
options = Parser.new do |p|
p.option :meal, "Choose Meal", :default => "CucumberSalad"
end.parse!
raise ArgumentError, "No files given!" unless ARGV.size > 0
file = ARGV.shift
If this short file is saved as script.rb, a call could look like the following: ruby script.rb --meal=BrainSandwich file1.txt file2.txt.
Yes, just define an option which takes a String as an argument, i.e. pass a string as the default value for that option. Now everything between quotes will be parsed as the value for that argument, e.g. ruby testscript.rb --selection 'I want the best selection you have!!! And if possible, add Donuts.' Note that double quotes may cause trouble, for example I get an error if I use an exclamation mark in double quotes, but no error in single quotes.
Yes, just define an option which takes an Array as an argument, i.e. pass an array as the default value for that option. The input will be split by comma. If the arguments contain spaces, wrap the whole thing in single quotes or double quotes.
For example if you want to accept multiple file names with whitespaces in them:
require 'rubygems' # necessary for ruby v1.8.*
require 'micro-optparse'
options = Parser.new do |p|
p.option :filenames, "Files which will be processed", :default => []
end.process!
p options[:filenames]
ruby testscript.rb --filenames 'todo.txt,my great adventures.txt' yields ["todo.txt", "my great adventures.txt"].
Yes and No. The literal answer to this question is "No", since a default value must be given for all arguments, except switches which default to false. However, what you are probably trying to achieve is to define an option which does only show up in the resulting hash, if the argument was given by the user. This is actually possible! Just add :optional => true to the line where you define your option and you are ready to go! The default value is still used for type checking.
require 'rubygems' # necessary for ruby v1.8.*
require 'micro-optparse'
options = Parser.new do |p|
p.option :file, "File to process (optional)", :default => "String", :optional => true
end.process!
puts options
Example usage of this parser:
ruby myprogram.rb will yield {}ruby myprogram.rb --file testfile.txt will yield {:file => "testfile.txt"}Yes you can. To disable them globally, i.e. for all arguments of the parser, just create the Parser with the default setting :no_short => true. To disable the short accessor of just a few arguments, add :no_short => true as a setting to these arguments.
For example to disable short accessors for all arguments:
require 'rubygems' # necessary for ruby v1.8.*
require 'micro-optparse'
options = Parser.new(:no_short => true) do |p|
p.option :foo, "Foo"
end.process!
puts options
To disable short accessor of just a few arguments:
require 'rubygems' # necessary for ruby v1.8.*
require 'micro-optparse'
options = Parser.new do |p|
p.option :foo, "Foo without short", :no_short => true
p.option :bar, "Bar with short"
end.process!
puts options
FAQs
Unknown package
We found that micro-optparse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.

Security News
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.