Skip to content

Instantly share code, notes, and snippets.

@peterkeen
Last active August 24, 2022 21:00
Show Gist options
  • Save peterkeen/f7cfedb1e35018557229e6541207d56e to your computer and use it in GitHub Desktop.
Save peterkeen/f7cfedb1e35018557229e6541207d56e to your computer and use it in GitHub Desktop.
require 'sorbet-runtime'
require 'active_support'
class ApplicationScript < T::InexactStruct
def self.run!(args = ARGV)
options = {}
OptionParser.new do |parser|
props.each do |propname, prop|
# special case T::Boolean because OptionParser doesn't understand it
if prop[:type] == T::Boolean
parser.on("--[no-]#{propname.to_s.dasherize}") do |val|
options[propname] = val
end
else
parser.on("--#{propname.to_s.dasherize} #{propname.upcase}", prop[:type]) do |val|
options[propname] = val
end
end
end
end.parse!(args)
call(**options)
end
def self.call(**kwargs)
new(**kwargs).call
end
def call
raise NotImplementedError
end
end
class SomeScript < ApplicationScript
const :filename, String
const :dry_run, T::Boolean, default: false
def call
pp [filename, dry_run]
end
end
if $PROGRAM_NAME = __FILE__
SomeScript.run!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment