lib/cmds/util/tokenize_option.rb in cmds-0.2.2 vs lib/cmds/util/tokenize_option.rb in cmds-0.2.3
- old
+ new
@@ -2,20 +2,22 @@
require 'nrser/refinements'
require_relative "defaults"
class Cmds
+ TOKENIZE_OPT_KEYS = [:array_mode, :array_join_string, :false_mode]
+
# turn an option name and value into an array of shell-escaped string
# token suitable for use in a command.
#
# @param [String] name
# string name (one or more characters).
#
# @param [*] value
# value of the option.
#
- # @param [Hash] opts
+ # @param [Hash] **opts
# @option [Symbol] :array_mode (:multiple)
# one of:
#
# 1. `:multiple` (default) provide one token for each value.
#
@@ -31,13 +33,15 @@
# string to join array values with when `:array_mode` is `:join`.
#
# @return [Array<String>]
# string tokens.
#
- def self.tokenize_option name, value, opts = {}
- opts = defaults opts, [:array_mode, :array_join_string, :false_mode]
+ def self.tokenize_option name, value, **opts
+ pp opts: opts
+ opts = defaults opts, TOKENIZE_OPT_KEYS
+
unless name.is_a?(String) && name.length > 0
raise ArgumentError.new NRSER.squish <<-END
`name` must be a String of length greater than zero,
found #{ name.inspect }
END
@@ -70,10 +74,10 @@
esc(name) +
separator +
esc(value.join opts[:array_join_string]) ]
when :json
- [prefix + esc(name) + separator + esc(JSON.dump value)]
+ [prefix + esc(name) + separator + "'" + JSON.dump(value).gsub(%{'}, %{'"'"'}) + "'"]
else
# SOL
raise ArgumentError.new NRSER.squish <<-END
bad array_mode option: #{ opts[:array_mode] },
\ No newline at end of file