Class: Argvise

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_cmd_hash, opts: nil) ⇒ Argvise

opts

  • When bsd_style is set to false, the builder operates in GNU-style mode, which typically uses hyphenated flags.

  • If kebab_case_flags is set to true, any underscores (_) in flag names will be automatically converted to hyphens (-).

    • For example, a flag like --enable_jit will be transformed into --enable-jit.

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"].

Examples:


require 'argvise'
cmd = { ruby: nil, r: "argvise", verbose: true, e: true, "puts Argvise::VERSION": nil }
opts = Argvise::DEFAULT_OPTS
Argvise.new(cmd, opts:).build.then{system *_1}

Parameters:

  • opts (Hash) (defaults to: nil)

    : { bsd_style: Boolean, kebab_case_flags: Boolean }



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_styleObject

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_flagsObject

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

Examples:

require 'argvise'
cmd = { ruby: nil, r: "argvise", verbose: true, e: true, "puts Argvise::VERSION": nil }
opts = { bsd_style: false }
Argvise.build(cmd, opts:)

Parameters:

  • raw_cmd_hash (Hash)

    The hash to be converted (i.e., raw input data)

  • opts (Hash) (defaults to: nil)

Returns:

  • (Array<String>)

    The generated array of command-line arguments

See Also:



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.

Examples:


require 'argvise'
{ ruby: nil, r: "argvise", e: true, "puts Argvise::VERSION": nil }
  .then(&Argvise.new_proc)
  .build
  .then{system *_1}

Returns:

  • (::Proc)

    .call(raw_cmd_hash) => self

See Also:



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

#buildArray<String>

Returns:

  • (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

Returns:

  • (self)


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

Returns:

  • (self)


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