Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Command-Line Argument Sorting and Parsing, for Ruby
CLASP stands for Command-Line Argument Sorting and Parsing. The first CLASP library was a C library with a C++ wrapper (see project CLASP). There have been several implementations in other languages (as listed in Relation projects). CLASP.Ruby is the Ruby version.
All CLASP libraries provide the facilities to Command Line Interface (CLI) programs as described in detail below.
The libCLImate.Ruby library is implemented in terms of CLASP.Ruby but provides a higher-level abstration as well as several utility functions and the facility to fully specify command-line arguments declaratively in the __END__
section of a source file. More information and examples provided in the libCLImate.Ruby project.
Install via gem as in:
gem install clasp-ruby
or add it to your Gemfile
.
Use via require, as in:
require 'clasp'
All CLASP libraries discriminate between three types of command-line arguments:
For example, in the command line
myprog --all -c --opt1=val1 infile outfile
there are:
--all
and -c
;--opt1
, which has the value val1
; andinfile
and outfile
.Flags and options may have alias. If the alias for --all
is -a
and the alias for --opt1
is -o
then the following command-line is exactly equivalent to the previous one:
myprog -a -c -o val1 infile outfile
One-letter flags may be combined. Hence, the following command-line is exactly equivalent to the previous ones:
myprog -ac -o val1 infile outfile
Option aliases may specify a value. If the alias -v1
means --opt1=val1
then the following command-line is exactly equivalent to the previous ones:
myprog -ac -v1 infile outfile
Option aliases that are one letter may be combined with one-letter flags. If the alias -v
means --opt1=val1
then the following command-line is exactly equivalent to the previous ones:
myprog -acv infile outfile
UNIX standard arguments confer specific meanings:
--help
means that the program should show the usage/help information and terminate;--version
means that the program should show the version information and terminate;--
means that all subsequent arguments should be treated as values, regardless of any hyphen-prefixes or embedded =
signs.To support such above special processing, CLASP libraries provide facilities for declarative specification of command-line flags and options, and aliases thereof. For the previous example, the CLASP.Ruby code would look like the following:
# file: cr-example.rb
PROGRAM_VERSION = '0.1.2'
Specifications = [
CLASP.Flag('--all', alias: '-a', help: 'processes all item types'),
CLASP.Flag('-c', help: 'count the processed items'),
CLASP.Option('--opt1', alias: '-o', help: 'an option of some kind', values_range: %w{ val1, val2 }),
CLASP.Flag('--opt1=val1', alias: '-v'),
# see next section for why these two are here
CLASP::Flag.Help,
CLASP::Flag.Version,
]
# assuming the command-line `myprog -acv infile outfile`
Args = CLASP::Arguments.new(ARGV, Specifications)
puts Args.flags.size # => 2
puts Args.flags[0].name # => "--all"
puts Args.flags[1].name # => "-c"
puts Args.options.size # => 1
puts Args.options[0].name # => "--opt1"
puts Args.options[0].value # => "val1"
puts Args.values.size # => 2
puts Args.values[0] # => "infile"
puts Args.values[1] # => "outfile"
There are aspects common to all CLI programs, such as responding to --help
and --version
. All CLASP libraries provide facilities to assist the programmer: CLASP.Ruby provides the two module methods CLASP.show_usage() and CLASP.show_version(), as shown in the following code extending the example above:
Args.flags.each do |f|
case f.name
when CLASP::Flag.Help.name
CLASP.show_usage(Specifications, exit: 0, values: '<input-file> <output-file>')
when CLASP::Flag.Version.name
CLASP.show_version(Specifications, exit: 0, version: PROGRAM_VERSION)
when '--all'
# do something appropriate to `--all`
. . .
Given the command
./cr-example.rb --help
then the program will output the following
USAGE: cr-example.rb [ ... flags and options ... ] <input-file> <output-file>
flags/options:
-a
--all
processes all item types
-c
count the processed items
-v --opt1=val1
-o <value>
--opt1=<value>
an option of some kind where <value> one of:
val1,
val2
--help
shows this help and terminates
--version
shows version and terminates
and given the command
./cr-example.rb --version
then the program will output the following
cr-example.rb 0.1.2
Examples are provided in the examples
directory, along with a markdown description for each. A detailed list TOC of them is provided in EXAMPLES.md.
Defect reports, feature requests, and pull requests are welcome on https://github.com/synesissoftware/CLASP.Ruby.
CLASP.Ruby is inspired by the C/C++ CLASP library, which is documented in the articles:
Other CLASP libraries include:
Projects in which CLASP.Ruby is used include:
CLASP.Ruby is released under the 3-clause BSD license. See LICENSE for details.
FAQs
Unknown package
We found that clasp-ruby demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.