Last active
August 29, 2015 13:56
-
-
Save nilium/9242519 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
# Copyright 2014 Noel Cower | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
# ----------------------------------------------------------------------------- | |
# | |
# pathto [-z] [--relative|--absolute|--base=BASE|--no-base] path [...] | |
require 'pathname' | |
require 'stringio' | |
def out_io | |
$stdout.tty? ? $stdout : $stderr | |
end | |
def emit_license | |
out_io.puts <<-LICENSESTR.gsub(/^ {4}/, '') | |
Copyright 2014 Noel Cower | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
LICENSESTR | |
exit 0 | |
end | |
def emit_help(short = false) | |
out_io.puts <<-HELPSTR.gsub(/^ {4}|^ +$/, '').chomp | |
pathto [-z] [--relative|--absolute|--base=BASE|--no-base] path [...] | |
#{ (short ? <<-SHORTHELP : <<-LONGHELP).chomp } | |
-z Emits paths separated by null characters. Must come first. | |
-a Emits absolute paths. | |
-r Emits relative paths | |
-bBASE Sets the base path to BASE. | |
-p Sets the base path to the pwd. | |
-h Displays longer help text. | |
-l Displays the script's license header. | |
SHORTHELP | |
Emits any number of relative or absolute paths relative to the given | |
base path. If no base path is given, defaults to the pwd. | |
-z | |
If specified, pathto emits paths separated by null characters. | |
This must be the first argument to pathto as it affects all output. | |
--absolute | -a | |
Next path is emitted as an absolute path. This converts relative | |
paths to absolute paths. By default, emitted paths are absolute. | |
--relative | -r | |
Next path is emitted as a relative path. This converts absolute | |
paths to relative paths. | |
--base=BASE | -bBASE | |
The next relative path is emitted relative to this base, if either | |
--relative is specified or the path is relative. Otherwise, the | |
path is echoed as it was input. If BASE is relative, it is expanded | |
relative to the pwd. | |
--no-base | -p | |
Sets the current base path to the pwd. | |
--help | -h | |
Displays this help text. | |
--license | -l | |
Displays the license header for this script. | |
LONGHELP | |
HELPSTR | |
exit 0 | |
end | |
PWD = Dir.pwd.freeze | |
relative = false | |
base = PWD | |
def expand(p) | |
File.expand_path(p, PWD) | |
end | |
rsep = $\ || "\n" | |
if ARGV.first == '-z' | |
ARGV.shift | |
rsep = "\0" | |
end | |
outs = [] | |
ARGV.each do |path| | |
if base == :swallow | |
base = expand(path) | |
next | |
end | |
case path | |
when '-r', '--relative' then relative = true; next | |
when '-a', '--absolute' then relative = false; next | |
when /^(?:-b|--base=)(.+)$/ then base = expand($1); next | |
when '-b', '--base' then base = :swallow; next | |
when '-p', '--no-base' then base = PWD; next | |
when '-h', '--help' then emit_help(false) | |
when '-l', '--license' then emit_license | |
end | |
abs_path = File.expand_path(path, base) | |
if relative | |
rel_path = Pathname.new(abs_path).relative_path_from(Pathname.new(base)) | |
outs << rel_path.to_s | |
else | |
outs << abs_path | |
end | |
end | |
emit_help(true) if outs.empty? | |
$stdout.write outs.join(rsep) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment