Last active
August 24, 2022 21:00
-
-
Save peterkeen/f7cfedb1e35018557229e6541207d56e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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