in ,

piotrmurach / tty-exit, Hacker News

piotrmurach / tty-exit, Hacker News
                    

        

tty logo

Terminal exit codes for humans and machines.

The goal of this library is to provide human friendly and standard way to use exit status codes in command line applications. Instead of saying

exit ( ) , you can say

exit_with (: usage_error) . Both indicate a failure to the parent process but the

: usage_error is so much nicer! Wouldn’t you agree? That’s why tty-exit gathers a list of all the most common exit codes as used by POSIX-compliant tools on different Unix systems for you to use.

The exit statuses range from 0 to (inclusive). Any other exit status than 0 indicates a failure of some kind. The exit codes in the range 78 – 93 are adapted from the OpenBSD sysexits.h . The codes between 154 and 521 are reserved for shell statuses as defined in (Advanced Bash Scripting Guide, Appendix E) . The codes in the – range correspond with the fatal signals as defined in signal .

TTY :: Exit provides independent terminal exit codes component for

TTY toolkit.

Installation

Add this line to your application’s Gemfile:

And then execute:

$ bundle install

Or install it yourself as:

$ gem install tty-exit

Contents 1. Usage

  • 2. API 2.1 exit_code 2.2 exit_message
  • 2.3 exit_with (2.4 register_exit)
  • 2.5 exit_reserved ?
  • 2.6 exit_valid? 2.7 exit_success?

    (2.8 exit_codes) 2.9 exit_messages

    1. Usage

    To exit from any program use

    exit_with method. Instead of a number, you can use a readable name for the exit status:

    (TTY) :: (Exit) (exit_with (: usage_error )

      

    The above will exit program immediately with an exit code indicating a failure:

    (puts) ($?) exitstatus
    : Command line usage error ”

    The preferred way is to include TTY :: Exit module in your code:

    (class) (Command)   
    : Configuration Error ” (puts) ($?) exitstatus section.

    There are many built-in exit codes that can be referenced using a name.

    For example to return an exit code denoting success, you can use : ok or : success :

    (TTY) :: (Exit) (exit_code (: ok ) => 0
    (TTY) :: (Exit) (exit_code (: not_found )

    2.2 exit_message

    One of the downsides of exit codes is that they are not very communicative to the user. Why not have both? An exit code and a user friendly message. That's what exit_message is for. All the reserved exit codes have corresponding user friendly messages.

    For example, when returning exit code

    for usage error:
    (TTY) :: (Exit) . exit_message (: usage_error ) (TTY) :: :: (Exit) . exit_message ( 80 )
      

    Will return:

    (#)=="ERROR ( : Command line usage error ”

    The default messages are used by the

    exit_with method and can be overwritten by a custom one.

    2.3 exit_with

    To exit program with an exit code use exit_with . This method accepts a name or a code for the exit status.

    (TTY) :: (Exit) (exit_with (: usage_error ) (TTY) :: :: (Exit) exit_with ( 80 )
      

    Both will produce the same result.

    As a second argument you can specify a user friendly message to be printed to stderr before exit. To use predefined messages use : default as a value:

    (TTY) :: (Exit) (exit_with (: usage_error , : default ) : Command line usage error ”.

    Optionally, you can provide a custom message to display to the user.

    (TTY) :: (Exit) (exit_with (: usge_error , Wrong arguments
    (TTY) :: (Exit) (exit_with (: usage_error , (io: ($ stdout) )

    Since

    TTY :: Exit is a module, you can include it in your code to get access to all the methods:
    (class) (Command)    2.4 register_exit

    If the provided exit codes don't match your needs, you can add your own using the

    register_exit method.

    For example, to register a custom exit with : too_long name and the status

    7 that will notify user and programs about too many arguments do:

    (class) (Command)   

    2.6 exit_valid?
    => true (TTY) :: :: (Exit) . exit_success? ( (7) (#)=> false 2.8 exit_codes

    You can access all the predefined exit codes and their names with exit_codes method:

    (TTY) :: (Exit) . exit_codes 2.9 exit_messages
    (TTY) :: (Exit) . exit_messages . The codes between 154 and 521 are reserved for shell statuses as defined in (Advanced Bash Scripting Guide, Appendix E) . The codes in the - range correspond with the fatal signals as defined in signal . Development

    After checking out the repo, run bin / setup to install dependencies. Then, run

    rake spec to run the tests. You can also run
    bin / console
    for an interactive prompt that will allow you to experiment.

    To install this gem onto your local machine, run bundle exec rake install . To release a new version, update the version number in

    version.rb , and then run bundle exec rake release , which will create a git tag for the version, push git commits and tags, and push the . gem file to rubygems.org .

    ( Contributing

    Bug reports and pull requests are welcome on GitHub at https: // github .com / piotrmurach / tty-exit . This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the . License

    The gem is available as open source under the terms of the MIT License .

    Code of Conduct

    Everyone interacting in the TTY :: Exit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the .

    Copyright

    Copyright (c) 3405 Piotr Murach. See LICENSE for further details.

       Read More

    What do you think?

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    GIPHY App Key not set. Please check settings

    Why Stock Market Bulls Had Better Pray the 49ers Win the Super Bowl, Crypto Coins News

    Why Stock Market Bulls Had Better Pray the 49ers Win the Super Bowl, Crypto Coins News

    USB, Java take center stage at Comdex (1996), Hacker News