Class: RunKit::Options::Config
- Inherits:
-
Object
- Object
- RunKit::Options::Config
- Defined in:
- lib/run_kit/options/config.rb
Instance Attribute Summary collapse
-
#app_name ⇒ Object
Returns the value of attribute app_name.
-
#banner ⇒ Object
Returns the value of attribute banner.
-
#color ⇒ Object
Returns the value of attribute color.
-
#exit ⇒ Object
Returns the value of attribute exit.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#help ⇒ Object
Returns the value of attribute help.
-
#help_flag ⇒ Object
readonly
Returns the value of attribute help_flag.
-
#lookup ⇒ Object
readonly
Returns the value of attribute lookup.
-
#naked ⇒ Object
(also: #naked?)
Returns the value of attribute naked.
-
#positionals ⇒ Object
readonly
Returns the value of attribute positionals.
-
#separators ⇒ Object
readonly
Returns the value of attribute separators.
-
#version ⇒ Object
Returns the value of attribute version.
-
#version_flag ⇒ Object
readonly
Returns the value of attribute version_flag.
Instance Method Summary collapse
-
#bool(*opts, default: nil, required: false) ⇒ Object
(also: #boolean)
flags.
-
#defaults ⇒ Object
handy helpers.
-
#flag(switch) ⇒ Object
one-liners.
- #flag?(switch) ⇒ Boolean
- #float(*opts, default: nil, required: false, choices: nil) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #int(*opts, default: nil, required: false, choices: nil) ⇒ Object (also: #integer)
- #key?(key) ⇒ Boolean
- #path(*opts, default: nil, required: false, choices: nil) ⇒ Object (also: #pathname)
-
#pos(meta, help = "") ⇒ Object
(also: #positional)
Add a positional param declared as
<url>. -
#prepare! ⇒ Object
Complete one-time setup after the caller has declared overrides.
- #required ⇒ Object
-
#sep(text = "") ⇒ Object
(also: #separator)
Add separator text at the current point in generated help.
- #str(*opts, default: nil, required: false, choices: nil) ⇒ Object (also: #string)
- #sym(*opts, default: nil, required: false, choices: nil) ⇒ Object (also: #symbol)
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
13 14 15 16 17 18 |
# File 'lib/run_kit/options/config.rb', line 13 def initialize @app_name = File.basename($PROGRAM_NAME) @naked = true @lookup = {} @flags, @positionals, @separators = [], [], [] end |
Instance Attribute Details
#app_name ⇒ Object
Returns the value of attribute app_name.
9 10 11 |
# File 'lib/run_kit/options/config.rb', line 9 def app_name @app_name end |
#banner ⇒ Object
Returns the value of attribute banner.
9 10 11 |
# File 'lib/run_kit/options/config.rb', line 9 def @banner end |
#color ⇒ Object
Returns the value of attribute color.
9 10 11 |
# File 'lib/run_kit/options/config.rb', line 9 def color @color end |
#exit ⇒ Object
Returns the value of attribute exit.
9 10 11 |
# File 'lib/run_kit/options/config.rb', line 9 def exit @exit end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
10 11 12 |
# File 'lib/run_kit/options/config.rb', line 10 def flags @flags end |
#help ⇒ Object
Returns the value of attribute help.
9 10 11 |
# File 'lib/run_kit/options/config.rb', line 9 def help @help end |
#help_flag ⇒ Object (readonly)
Returns the value of attribute help_flag.
10 11 12 |
# File 'lib/run_kit/options/config.rb', line 10 def help_flag @help_flag end |
#lookup ⇒ Object (readonly)
Returns the value of attribute lookup.
10 11 12 |
# File 'lib/run_kit/options/config.rb', line 10 def lookup @lookup end |
#naked ⇒ Object Also known as: naked?
Returns the value of attribute naked.
9 10 11 |
# File 'lib/run_kit/options/config.rb', line 9 def naked @naked end |
#positionals ⇒ Object (readonly)
Returns the value of attribute positionals.
10 11 12 |
# File 'lib/run_kit/options/config.rb', line 10 def positionals @positionals end |
#separators ⇒ Object (readonly)
Returns the value of attribute separators.
10 11 12 |
# File 'lib/run_kit/options/config.rb', line 10 def separators @separators end |
#version ⇒ Object
Returns the value of attribute version.
9 10 11 |
# File 'lib/run_kit/options/config.rb', line 9 def version @version end |
#version_flag ⇒ Object (readonly)
Returns the value of attribute version_flag.
10 11 12 |
# File 'lib/run_kit/options/config.rb', line 10 def version_flag @version_flag end |
Instance Method Details
#bool(*opts, default: nil, required: false) ⇒ Object Also known as: boolean
flags
40 41 42 |
# File 'lib/run_kit/options/config.rb', line 40 def bool(*opts, default: nil, required: false) add_flag(Flag.new(:bool, opts, default:, required:)) end |
#defaults ⇒ Object
handy helpers
74 75 76 77 78 79 |
# File 'lib/run_kit/options/config.rb', line 74 def defaults flags.filter_map do next if _1.required || _1 == help_flag || _1 == version_flag [_1.key, _1.default] end.to_h end |
#flag(switch) ⇒ Object
one-liners
82 |
# File 'lib/run_kit/options/config.rb', line 82 def flag(switch) = lookup[switch] |
#flag?(switch) ⇒ Boolean
83 |
# File 'lib/run_kit/options/config.rb', line 83 def flag?(switch) = lookup.key?(switch) |
#float(*opts, default: nil, required: false, choices: nil) ⇒ Object
44 45 46 |
# File 'lib/run_kit/options/config.rb', line 44 def float(*opts, default: nil, required: false, choices: nil) add_flag(Flag.new(:float, opts, default:, required:, choices:)) end |
#int(*opts, default: nil, required: false, choices: nil) ⇒ Object Also known as: integer
48 49 50 |
# File 'lib/run_kit/options/config.rb', line 48 def int(*opts, default: nil, required: false, choices: nil) add_flag(Flag.new(:int, opts, default:, required:, choices:)) end |
#key?(key) ⇒ Boolean
84 |
# File 'lib/run_kit/options/config.rb', line 84 def key?(key) = lookup.key?(key) |
#path(*opts, default: nil, required: false, choices: nil) ⇒ Object Also known as: pathname
52 53 54 |
# File 'lib/run_kit/options/config.rb', line 52 def path(*opts, default: nil, required: false, choices: nil) add_flag(Flag.new(:path, opts, default:, required:, choices:)) end |
#pos(meta, help = "") ⇒ Object Also known as: positional
Add a positional param declared as <url>.
21 22 23 24 25 26 27 |
# File 'lib/run_kit/options/config.rb', line 21 def pos(, help = "") Positional.new(meta:, help:).tap do raise ArgumentError, "duplicate positional #{_1.key}" if key?(_1.key) positionals << _1 lookup[_1.key] = _1 end end |
#prepare! ⇒ Object
Complete one-time setup after the caller has declared overrides.
88 89 90 91 92 93 94 |
# File 'lib/run_kit/options/config.rb', line 88 def prepare! return if @prepared @prepared = true @exit ||= lambda { |status| Kernel.exit(status) } @help_flag = add_builtin(["-h", "--help"], "Show this message") @version_flag = add_builtin(["-v", "--version"], "Show version") if version end |
#required ⇒ Object
85 |
# File 'lib/run_kit/options/config.rb', line 85 def required = flags.select(&:required?) |
#sep(text = "") ⇒ Object Also known as: separator
Add separator text at the current point in generated help.
30 31 32 33 34 |
# File 'lib/run_kit/options/config.rb', line 30 def sep(text = "") [flags.length, text].tap do separators << _1 end end |