lib/backticks/cli.rb in backticks-0.3.1 vs lib/backticks/cli.rb in backticks-0.4.0
- old
+ new
@@ -8,30 +8,30 @@
# Although Getopt is simple, it has the tremendous advantage of being
# compatible with a wide range of other schemes including GNU getopt-long,
# golang flags, and most Java utilities. It's a great choice of default
# CLI.
module Getopt
- # Translate a series of positional and keyword arguments into command-line
- # line parameters consisting of words and options.
+ # Translate a series Ruby positional and keyword arguments into command-
+ # parameters consisting of words and options.
#
# Each positional argument can be a Hash, an Array, or another object.
# They are handled as follows:
# - Hash is translated to a sequence of options; see #options
# - Array is appended to the command line as a sequence of words
- # - other objects are turned into a strong with #to_s and appended to the command line as a single word
+ # - other objects are turned into a string with #to_s and appended to the command line as a single word
#
# @return [Array] list of String words and options
#
# @example recursively find all text files
# parameters('ls', l:true, R:true, '*.txt') => 'ls -l -R *.txt
#
# @example install your favorite gem
# parameters('gem', 'install', no_document:true, 'backticks')
- def self.parameters(*cmd)
+ def self.parameters(*sugar)
argv = []
- cmd.each do |item|
+ sugar.each do |item|
case item
when Array
# list of words to append to argv
argv.concat(item.map { |e| e.to_s })
when Hash
@@ -44,11 +44,11 @@
end
argv
end
- # Translate Ruby method parameters into command-line parameters using a
+ # Translate Ruby keyword arguments into command-line parameters using a
# notation that is compatible with traditional Unix getopt. Command lines
# generated by this method are also mostly compatible with the following:
# - GNU getopt
# - Ruby trollop gem
# - Golang flags package
@@ -68,16 +68,16 @@
# produce the most idiomatic or compact command line for a given program;
# its output is, however, almost always valid for utilities that use
# Unix-like parameters.
#
# @return [Array] list of String command-line options
- def self.options(**opts)
+ def self.options(kwargs={})
flags = []
# Transform opts into golang flags-style command line parameters;
# append them to the command.
- opts.each do |kw, arg|
+ kwargs.each do |kw, arg|
if kw.length == 1
if arg == true
# true: boolean flag
flags << "-#{kw}"
elsif arg
@@ -102,6 +102,6 @@
flags
end
end
end
-end
\ No newline at end of file
+end