Module: Slidict::Cli::Options::ClassMethods
- Defined in:
- lib/slidict/cli/options.rb
Overview
Class-level DSL (options name: -> { default }, ...) mixed into any
class that includes Options.
Instance Method Summary collapse
-
#flag(*switches, desc:, arg: nil, coerce: nil, group: :default) ⇒ Object
Declares a flag.
- #flag_groups ⇒ Object
- #flags(group = :default) ⇒ Object
-
#options(**defaults) ⇒ Object
Defines the assignment in an anonymous module (rather than directly on the including class) so a class that needs to post-process a value can still define its own
initialize, callsuper, and have it reach this one instead of silently overwriting it. -
#usage(&text) ⇒ Object
Declares the
-h/--helpusage text, generating a privateprint_helpthat writes it to@outputand returns SUCCESS, instead of every command spelling that out.
Instance Method Details
#flag(*switches, desc:, arg: nil, coerce: nil, group: :default) ⇒ Object
Declares a flag. group lets a class with several argv shapes
(e.g. Slides' list vs. create/edit) keep separate flag sets;
parse_flags!/flags_help default to :default. desc may be a
Proc (instance_exec'd lazily, like an options default) for text
that depends on another file having loaded by call time rather
than by require time (e.g. Output::Format.names).
82 83 84 85 |
# File 'lib/slidict/cli/options.rb', line 82 def flag(*switches, desc:, arg: nil, coerce: nil, group: :default) key = switches.last.sub(/\A-+/, "").tr("-", "_").to_sym flag_groups[group] << Flag.new(switches: switches, key: key, arg: arg, coerce: coerce, desc: desc) end |
#flag_groups ⇒ Object
87 88 89 |
# File 'lib/slidict/cli/options.rb', line 87 def flag_groups @flag_groups ||= Hash.new { |h, k| h[k] = [] } end |
#flags(group = :default) ⇒ Object
91 92 93 |
# File 'lib/slidict/cli/options.rb', line 91 def flags(group = :default) flag_groups[group] end |
#options(**defaults) ⇒ Object
Defines the assignment in an anonymous module (rather than directly
on the including class) so a class that needs to post-process a
value can still define its own initialize, call super, and have
it reach this one instead of silently overwriting it.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/slidict/cli/options.rb', line 64 def (**defaults) attr_reader(*defaults.keys) include(Module.new do define_method(:initialize) do |**overrides| defaults.each do |name, default| instance_variable_set(:"@#{name}", resolve_option(name, default, overrides)) end end end) end |
#usage(&text) ⇒ Object
Declares the -h/--help usage text, generating a private
print_help that writes it to @output and returns SUCCESS,
instead of every command spelling that out. Takes a block for the
same lazy-evaluation reason as flag's desc:.
99 100 101 102 103 104 |
# File 'lib/slidict/cli/options.rb', line 99 def usage(&text) private(define_method(:print_help) do @output.puts instance_exec(&text) SUCCESS end) end |