Class: Argvise
- Inherits:
-
Object
- Object
- Argvise
- Defined in:
- lib/argvise/core.rb,
lib/argvise/core.rb,
lib/argvise/version.rb,
lib/argvise/refinement.rb
Overview
To maintain mruby compatibility, define private_constant and
refinements in refinement.rb rather than in core.rb.
Defined Under Namespace
Modules: HashExt, HashMixin, HashRefin
Constant Summary collapse
- DEFAULT_OPTS =
v0.0.3 default options
{ bsd_style: false, kebab_case_flags: true }.freeze
- VERSION =
'0.0.10'
Instance Attribute Summary collapse
-
#bsd_style ⇒ Object
Returns the value of attribute bsd_style.
-
#kebab_case_flags ⇒ Object
Returns the value of attribute kebab_case_flags.
Class Method Summary collapse
-
.build(raw_cmd_hash, opts: nil) ⇒ Array<String>
Converts a hash into a command-line argument array.
-
.new_proc ⇒ ::Proc
Returns a Proc that wraps
Argvise.new, allowing functional-style chaining.
Instance Method Summary collapse
- #build ⇒ Array<String>
-
#initialize(raw_cmd_hash, opts: nil) ⇒ Argvise
constructor
opts.
-
#with_bsd_style(value = true) ⇒ self
Default: true.
-
#with_kebab_case_flags(value = true) ⇒ self
Default: true.
Constructor Details
#initialize(raw_cmd_hash, opts: nil) ⇒ Argvise
opts
When
bsd_styleis set tofalse, the builder operates in GNU-style mode, which typically uses hyphenated flags.If
kebab_case_flagsis set totrue, any underscores (_) in flag names will be automatically converted to hyphens (-).- For example, a flag like
--enable_jitwill be transformed into--enable-jit.
- For example, a flag like
When the value of a flag key is nil, the kebab_case_flags option has no effect.
— i.e., the key will not be transformed.
For example, the input {"a_b-c": nil} will result in ["a_b-c"],
and not be automatically transformed into ["a-b-c"].
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/argvise/core.rb', line 137 def initialize( raw_cmd_hash, opts: nil ) opts = DEFAULT_OPTS.merge(opts || {}) @raw_cmd_hash = raw_cmd_hash @bsd_style = opts[:bsd_style] @kebab_case_flags = opts[:kebab_case_flags] end |
Instance Attribute Details
#bsd_style ⇒ Object
Returns the value of attribute bsd_style.
65 66 67 |
# File 'lib/argvise/core.rb', line 65 def bsd_style @bsd_style end |
#kebab_case_flags ⇒ Object
Returns the value of attribute kebab_case_flags.
65 66 67 |
# File 'lib/argvise/core.rb', line 65 def kebab_case_flags @kebab_case_flags end |
Class Method Details
.build(raw_cmd_hash, opts: nil) ⇒ Array<String>
Converts a hash into a command-line argument array
84 85 86 87 88 89 90 |
# File 'lib/argvise/core.rb', line 84 def build( raw_cmd_hash, opts: nil ) opts ||= DEFAULT_OPTS new(raw_cmd_hash, opts:).build end |
.new_proc ⇒ ::Proc
Returns a Proc that wraps Argvise.new, allowing functional-style chaining.
Useful for transforming a hash of CLI arguments into a command array.
106 107 108 109 110 |
# File 'lib/argvise/core.rb', line 106 def new_proc ->(raw_cmd_hash) do new(raw_cmd_hash) end end |
Instance Method Details
#build ⇒ Array<String>
163 164 165 166 167 168 |
# File 'lib/argvise/core.rb', line 163 def build # @raw_cmd_hash.each_pair.flat_map { |k, v| process_pair(k.to_s, v) } @raw_cmd_hash.each_with_object([]) do |(k, v), memo| memo.concat(process_pair(k.to_s, v)) end end |
#with_bsd_style(value = true) ⇒ self
Default: true
150 151 152 153 |
# File 'lib/argvise/core.rb', line 150 def with_bsd_style(value = true) # rubocop:disable Style/OptionalBooleanParameter @bsd_style = value self end |
#with_kebab_case_flags(value = true) ⇒ self
Default: true
157 158 159 160 |
# File 'lib/argvise/core.rb', line 157 def with_kebab_case_flags(value = true) # rubocop:disable Style/OptionalBooleanParameter @kebab_case_flags = value self end |