Clamp
=====
[![Gem Version](https://badge.fury.io/rb/clamp.png)](http://badge.fury.io/rb/clamp)
[![Build Status](https://secure.travis-ci.org/mdub/clamp.png?branch=master)](http://travis-ci.org/mdub/clamp)
"Clamp" is a minimal framework for command-line utilities.
It handles boring stuff like parsing the command-line, and generating help, so you can get on with making your command actually do stuff.
Not another one!
----------------
Yeah, sorry. There are a bunch of existing command-line parsing libraries out there, and Clamp draws inspiration from a variety of sources, including [Thor], [optparse], and [Clip]. In the end, though, I wanted a slightly rounder wheel. (Although, Clamp has a _lot_ in common with Ara T. Howard's [main.rb]. Had I been aware of that project at the time, I might not have written Clamp.)
[optparse]: http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html
[Thor]: http://github.com/wycats/thor
[Clip]: http://clip.rubyforge.org/
[main.rb]: https://github.com/ahoward/main
Quick Start
-----------
A typical Clamp script looks like this:
```ruby
require 'clamp'
Clamp do
option "--loud", :flag, "say it loud"
option ["-n", "--iterations"], "N", "say it N times", :default => 1 do |s|
Integer(s)
end
parameter "WORDS ...", "the thing to say", :attribute_name => :words
def execute
the_truth = words.join(" ")
the_truth.upcase! if loud?
iterations.times do
puts the_truth
end
end
end
```
Internally, Clamp models a command as a Ruby class (a subclass of `Clamp::Command`), and a command execution as an instance of that class. The example above is really just syntax-sugar for:
```ruby
require 'clamp'
class SpeakCommand < Clamp::Command
option "--loud", :flag, "say it loud"
option ["-n", "--iterations"], "N", "say it N times", :default => 1 do |s|
Integer(s)
end
parameter "WORDS ...", "the thing to say", :attribute_name => :words
def execute
the_truth = words.join(" ")
the_truth.upcase! if loud?
iterations.times do
puts the_truth
end
end
end
SpeakCommand.run
```
Class-level methods like `option` and `parameter` declare attributes, in a similar way to `attr_accessor`, and arrange for them to be populated automatically based on command-line arguments. They are also used to generate `help` documentation.
There are more examples demonstrating various features of Clamp [on Github][examples].
[examples]: https://github.com/mdub/clamp/tree/master/examples
Declaring options
-----------------
Options are declared using the `option` method. The three required arguments are:
1. the option switch (or switches),
2. an option argument name
3. a short description
For example:
```ruby
option "--flavour", "FLAVOUR", "ice-cream flavour"
```
It works a little like `attr_accessor`, defining reader and writer methods on the command class. The attribute name is inferred from the switch (in this case, "`flavour`"). When you pass options to your command, Clamp will populate the attributes, which are then available for use in your `#execute` method.
```ruby
def execute
puts "You chose #{flavour}. Excellent choice!"
end
```
If you don't like the inferred attribute name, you can override it:
```ruby
option "--type", "TYPE", "type of widget", :attribute_name => :widget_type
# to avoid clobbering Object#type
```
### Short/long option switches
The first argument to `option` can be an array, rather than a single string, in which case all the switches are treated as aliases:
```ruby
option ["-s", "--subject"], "SUBJECT", "email subject line"
```
### Flag options
Some options are just boolean flags. Pass "`:flag`" as the second parameter to tell Clamp not to expect an option argument:
```ruby
option "--verbose", :flag, "be chatty"
```
For flag options, Clamp appends "`?`" to the generated reader method; ie. you get a method called "`#verbose?`", rather than just "`#verbose`".
Negatable flags are easy to generate, too:
```ruby
option "--[no-]force", :flag, "be forceful (or not)"
```
Clamp will handle both "`--force`" and "`--no-force`" options, setting the value of "`#force?`" appropriately.
### Required options
Although 'required option' is a an oxymoron, Clamp lets you mark an option as required, and will verify that a value is provided:
```ruby
option "--password", "PASSWORD", "the secret password", :required => true
```
Note that it makes no sense to mark a `:flag` option, or one with a `:default`, as `:required`.
### Multivalued options
Declaring an option "`:multivalued`" allows it to be specified multiple times on the command line.
```ruby
option "--format", "FORMAT", "output format", :multivalued => true
```
The underlying attribute becomes an Array, and the suffix "`_list`" is appended to the default attribute name. In this case, an attribute called "`format_list`" would be generated (unless you override the default by specifying an `:attribute_name`).
### Hidden options
Declaring an option "`:hidden`" will cause it to be hidden from `--help` output.
```ruby
option "--some-option", "VALUE", "Just a little option", :hidden => true
```
Declaring parameters
--------------------
Positional parameters can be declared using `parameter`, specifying
1. the parameter name, and
2. a short description
For example:
```ruby
parameter "SRC", "source file"
```
Like options, parameters are implemented as attributes of the command, with the default attribute name derived from the parameter name (in this case, "`src`"). By convention, parameter names are specified in uppercase, to make them obvious in usage help.
### Optional parameters
Wrapping a parameter name in square brackets indicates that it's optional, e.g.
```ruby
parameter "[TARGET_DIR]", "target directory"
```
### Multivalued (aka "greedy") parameters
Three dots at the end of a parameter name makes it "greedy" - it will consume all remaining command-line arguments. For example:
```ruby
parameter "FILE ...", "input files", :attribute_name => :files
```
Like multivalued options, greedy parameters are backed by an Array attribute (named with a "`_list`" suffix, by default).
Parsing and validation of options and parameters
------------------------------------------------
When you `#run` a command, it will first attempt to `#parse` command-line arguments, and map them onto the declared options and parameters, before invoking your `#execute` method.
Clamp will verify that all required (ie. non-optional) parameters are present, and signal a error if they aren't.
### Validation
Both `option` and `parameter` accept an optional block. If present, the block will be
called with the raw string argument, and is expected to validate it. The value returned by the block will be assigned to the underlying attribute, so it's also a good place to coerce the String to a different type, if appropriate.
For example:
```ruby
option "--port", "PORT", "port to listen on" do |s|
Integer(s)
end
```
If the block raises an ArgumentError, Clamp will catch it, and report that the value was bad:
```ruby
!!!plain
ERROR: option '--port': invalid value for Integer: "blah"
```
For multivalued options and parameters, the validation block will be called for each value specified.
More complex validation, e.g. those involving multiple options/parameters, should be performed within the `#execute` method. Use `#signal_usage_error` to tell the user what they did wrong, e.g.
```ruby
def execute
if port < 1024 && user != 'root'
signal_usage_error "port restricted for non-root users"
end
# ... carry on ...
end
```
### Advanced option/parameter handling
While Clamp provides an attribute-writer method for each declared option or parameter, you always have the option of overriding it to provide custom argument-handling logic, e.g.
```ruby
parameter "SERVER", "location of server"
def server=(server)
@server_address, @server_port = s
没有合适的资源?快使用搜索试试~ 我知道了~
electron-builder 解压后将文件夹内所有内容复制到/root/.cache目录中
共2000个文件
rb:2434个
c:404个
so:94个
需积分: 5 0 下载量 11 浏览量
2022-03-03
11:08:16
上传
评论
收藏 61.81MB ZIP 举报
温馨提示
electron-builder 解压后将文件夹内所有内容复制到/root/.cache目录中
资源详情
资源评论
资源推荐
收起资源包目录
electron-builder 解压后将文件夹内所有内容复制到/root/.cache目录中 (2000个子文件)
libz.so.1 81KB
libappindicator.so.1 51KB
libappindicator.so.1 50KB
libXss.so.1 14KB
libXss.so.1 9KB
mustache.1 3KB
mustache.1 3KB
libtermcap.so.2 13KB
ffi_call.3 2KB
ffi_call.3 2KB
ffi_prep_cif.3 1KB
ffi_prep_cif.3 1KB
ffi.3 648B
ffi.3 648B
libgconf-2.so.4 188KB
libgconf-2.so.4 182KB
libnotify.so.4 31KB
libnotify.so.4 26KB
libreadline.so.5 240KB
gemfile.5 20KB
mustache.5 8KB
mustache.5 8KB
libffi.so.6 31KB
libXtst.so.6 22KB
libXtst.so.6 22KB
libindicator.so.7 59KB
libindicator.so.7 54KB
74-l0ad9skb-2.7z 1.11MB
9r-l0adpysw-2.7z 1.02MB
SHASUMS256.txt-1.8.8 6KB
configure.ac 14KB
configure.ac 14KB
admin 463B
admin 463B
Makefile.am 6KB
Makefile.am 6KB
Makefile.am 4KB
Makefile.am 4KB
Makefile.am 249B
Makefile.am 249B
Makefile.am 169B
Makefile.am 169B
app-builder 4.32MB
appimagetool 759KB
appimagetool 304KB
appimagetool 201KB
appimagetool 185KB
CHANGELOG.asciidoc 2KB
CHANGELOG.asciidoc 2KB
AUTHORS 237B
AUTHORS 237B
.autotest 241B
.autotest 241B
gem.build_complete 0B
bundle 3KB
bundle 878B
bundle 499B
bundle-config 10KB
bundle-exec 6KB
bundle-gem 3KB
bundle-install 17KB
bundle-lock 2KB
bundle-package 3KB
bundle-platform 1KB
bundle-update 8KB
bundle_ruby 1KB
bundler 644B
bundler 500B
dlmalloc.c 179KB
dlmalloc.c 179KB
ffi.c 38KB
ffi.c 38KB
ffi_darwin.c 36KB
ffi_darwin.c 36KB
AbstractMemory.c 34KB
AbstractMemory.c 34KB
Function.c 29KB
Function.c 29KB
ffi.c 26KB
ffi.c 26KB
ffi.c 22KB
ffi.c 22KB
Struct.c 22KB
Struct.c 22KB
StructLayout.c 21KB
StructLayout.c 21KB
ffi.c 20KB
ffi.c 20KB
ffi.c 20KB
ffi.c 20KB
ffi.c 18KB
ffi.c 18KB
ffi64.c 17KB
ffi64.c 17KB
ffi.c 17KB
ffi.c 17KB
Call.c 17KB
Call.c 17KB
closures.c 16KB
closures.c 16KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
sanyuni
- 粉丝: 12
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0