Skip to content

Instantly share code, notes, and snippets.

@MatteoRagni
Created January 22, 2018 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatteoRagni/ceb92ace52034591a1b6b2ba1f569923 to your computer and use it in GitHub Desktop.
Save MatteoRagni/ceb92ace52034591a1b6b2ba1f569923 to your computer and use it in GitHub Desktop.
Converts windows path to WSL linux path
#!/usr/bin/env ruby
require 'optparse'
def convert_to_path(f, is_file)
ret = f.gsub(/^([A-Z]):/) { |m| "/mnt/#{m[0].downcase}" }
ret = ret.gsub(/\\/, "/")
ret = ret + "/../" if is_file
return ret
end
options = {}
OptionParser.new do |opts|
opts.banner = "Convert Windows directory in Linux directory"
opts.on("-f", "--file PATH", "The input is a file path") do |f|
if options[:path]
puts "Specify only one path"
exit 1
end
options[:path] = convert_to_path(f, true)
end
opts.on("-d", "--directory PATH", "The input is a directory path") do |d|
if options[:path]
puts "Specify only one path"
exit 1
end
options[:path] = convert_to_path(d, false)
end
opts.on("-cd", "--cd", "Run directly change direcotry command") do
options[:cd] = true
end
end.parse!
if __FILE__ == $0
if !options[:path]
puts "You must specify a path!"
exit 1
end
if Dir.exist? options[:path]
if options[:cd]
Dir.chdir options[:path]
else
puts options[:path]
end
else
puts "The direcotry does not exist"
exit 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment