Quick, No Dependency Ruby Command Line Argument Parsing with -s
If you pass the -s flag to the Ruby interpreter, it turns switches on the CLI into global variables: —bob turns into $bob. There’s no validation, no help, etc, so this is really only for quick scripts, but its nifty nevertheless.
For example, save this script as greet.rb:
#!/usr/bin/env ruby -s
puts "Hello, #{$name}!"and you can pass a value at the command line like this:
$ ruby -s greet.rb -name=World
Hello, World!Any unrecognized argument that does not start with - is left in ARGV as usual. Flags not preceded by - are not parsed — they pass through normally.
One practical use case is writing small utility scripts that need a handful of configurable parameters without pulling in a full option-parsing library like OptionParser. For a two- or three-flag script you might run once a week, that overhead often does not pay for itself.
Note: if you use a shebang line, you must include the flag there — #!/usr/bin/env ruby -s — rather than passing it at invocation time, since env does not split arguments the same way on all platforms. On Linux, you may find that env -S is needed:
#!/usr/bin/env -S ruby -sFor anything beyond a few flags, though, OptionParser or a gem like thor will serve you better.