https://github.com/chocolateboy/cli-pasta
Handle Ctrl-C and broken-pipe errors gracefully in Ruby command-line tools
https://github.com/chocolateboy/cli-pasta
cli command-line ctrl-c epipe gem interrupt pipe ruby-gem rubygem sigint signal sigpipe zero-dependency
Last synced: about 2 months ago
JSON representation
Handle Ctrl-C and broken-pipe errors gracefully in Ruby command-line tools
- Host: GitHub
- URL: https://github.com/chocolateboy/cli-pasta
- Owner: chocolateboy
- License: mit
- Created: 2018-03-27T21:34:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-29T02:17:44.000Z (about 4 years ago)
- Last Synced: 2025-04-15T06:43:05.753Z (about 2 months ago)
- Topics: cli, command-line, ctrl-c, epipe, gem, interrupt, pipe, ruby-gem, rubygem, sigint, signal, sigpipe, zero-dependency
- Language: Ruby
- Homepage:
- Size: 43.9 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# cli-pasta
[](https://github.com/chocolateboy/cli-pasta/actions?query=workflow%3Atest)
[](https://rubygems.org/gems/cli-pasta)- [NAME](#name)
- [INSTALL](#install)
- [SYNOPSIS](#synopsis)
- [DESCRIPTION](#description)
- [BACKGROUND](#background)
- [COMPATIBILITY](#compatibility)
- [VERSION](#version)
- [SEE ALSO](#see-also)
- [AUTHOR](#author)
- [COPYRIGHT AND LICENSE](#copyright-and-license)# NAME
cli-pasta - handle Ctrl-C and broken-pipe errors gracefully in Ruby command-line tools
# INSTALL
```ruby
gem "cli-pasta"
```# SYNOPSIS
```ruby
#!/usr/bin/env rubyrequire "cli-pasta"
require "optparse"# ...
ARGF.each do |line|
puts process(line)
end
```# DESCRIPTION
cli-pasta packages boilerplate code which is commonly copied 'n' pasted into
Ruby CLI scripts to perform the following tasks:- set up a `SIGINT` handler to handle Ctrl-C in the same way as other CLI tools
- set up a `SIGPIPE` handler to handle broken pipes in the same way as other CLI toolsThese tasks are executed by loading the corresponding files, either separately, e.g.:
```ruby
require "cli-pasta/sigint"
require "cli-pasta/sigpipe"
```Or as a group:
```ruby
require "cli-pasta"
```## BACKGROUND
By default, ruby produces an ugly error message when scripts are interrupted by
Ctrl-C (`SIGINT`), e.g.:$ timeout --signal INT 1 ruby -e sleep
Output:
Traceback (most recent call last):
1: from -e:1:in `'
-e:1:in `sleep': InterruptThe same is true if a process encounters an error when trying to write to a
broken pipe (`EPIPE`), e.g.:$ ruby -e 'loop { puts "." }' | head -n0
Output:
Traceback (most recent call last):
5: from -e:1:in `'
4: from -e:1:in `loop'
3: from -e:1:in `block in '
2: from -e:1:in `puts'
1: from -e:1:in `puts'
-e:1:in `write': Broken pipe @ io_writev - (Errno::EPIPE)The snippets provided by this gem install signal handlers which handle these
errors in the same way as other CLI tools, e.g.:$ timeout --signal INT 1 ruby -r cli-pasta -e sleep
# No output$ ruby -r cli-pasta -e 'loop { puts "." }' | head -n0
# No output# COMPATIBILITY
- [Maintained ruby versions](https://www.ruby-lang.org/en/downloads/branches/)
# VERSION
2.0.1
# SEE ALSO
* [nice-sigint](https://github.com/xiongchiamiov/nice-sigint) - make Ruby handle SIGINTs in a less-ugly manner
# AUTHOR
[chocolateboy](mailto:[email protected])
# COPYRIGHT AND LICENSE
Copyright © 2018-2021 by chocolateboy.
This is free software; you can redistribute it and/or modify it under the terms
of the [MIT license](https://opensource.org/licenses/MIT).