#!/usr/bin/env ruby require "rationalist" require "unibits" argv = Rationalist.parse( ARGV, string: '_', alias: { e: 'encoding', c: 'convert', v: 'version', }, boolean: [ 'help', 'version', 'stats' ], default: { stats: true }, ) if argv[:version] puts Unibits::VERSION exit(0) end if argv[:help] puts <<-HELP #{Paint["USAGE", :underline]} #{Paint["unibits", :bold]} [--encoding ] [--convert ] [--no-stats] data Supported encodings: #{Unibits::SUPPORTED_ENCODINGS.join(', ')} Explanation of stats: bytes / codepoints / glyphs / expected terminal width More info and examples at: https://github.com/janlelis/unibits HELP exit(0) end if argv[:_] && argv[:_][0] data = argv[:_][0] elsif !$stdin.tty? data = $stdin.read else data = nil end begin Unibits.of(data, encoding: argv[:encoding], convert: argv[:convert], stats: argv[:stats]) rescue ArgumentError $stderr.puts Paint[$!.message, :red] exit(1) end