about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-07-24 04:10:37 +0200
committerhut <hut@lavabit.com>2009-07-24 04:10:37 +0200
commit032e29bf97c681ae7740e5060a67b241c24e8e18 (patch)
treefe7a1be9d7e66be742ec7f4183d28bb9681f2122
parent4ca471d80f4cfa2b8b9257ab6e1e62ed121db4c0 (diff)
downloadranger-032e29bf97c681ae7740e5060a67b241c24e8e18.tar.gz
better argument parsing (without optparse though)
-rw-r--r--TODO3
-rwxr-xr-xranger60
2 files changed, 46 insertions, 17 deletions
diff --git a/TODO b/TODO
index 683f0884..515cb8e5 100644
--- a/TODO
+++ b/TODO
@@ -60,6 +60,7 @@ Features
 
 Aesthetics
 
-   ( ) #4   09/07/17  consider using optparse
+   (X) #4   09/07/17  consider using optparse
+               nevermind. i can do better
    ( ) #10  09/07/17  Debug module as an IO stream, for redirection of STDERR
    ( ) #24  09/07/20  seperate filetype detection from main program
diff --git a/ranger b/ranger
index 0cdc9674..7d1b7b68 100755
--- a/ranger
+++ b/ranger
@@ -25,30 +25,54 @@ $LOAD_PATH.unshift( MYDIR )
 ##--------------------------------------------------
 # parse arguments
 
-if ARGV[0] == '--workaround'
-	ARGV.shift(2)
-	ARGV.unshift('--cd')
-end
+ARGV.shift(2) if ARGV.first == '--workaround'
+
+pwd = nil
+
+while arg = ARGV.shift
+	case arg
+	when '-h', '--help', '-help'
+		puts "usage: ranger [options] [file/directory]"
+		puts "options are:"
+		puts "  -h, --help       show this message"
+		puts "  -v, --version    print the version of ranger"
+		puts "  -d, --debug      set the debug level to maximum"
+		puts "      --           stop option parsing"
+		exit
+
+	when '-v', '--version', '-version'
+		puts "ranger #{version}"
+		exit
+
+	when '-d', '--debug', '-debug'
+		arg_debug = true
+		
+	when '--'
+		break
+
+	else
+		if ARGV.size > 0
+			abort "ranger: Unrecognized option: #{arg}"
+		else
+			pwd = arg
+		end
 
-ARGV.delete('--cd') if cd = ARGV.include?('--cd')
-if ARGV.size > 0
-	case ARGV.first
-	when '-k'
-		exec "killall -9 #{File.basename($0)}"
 	end
-	pwd = ARGV.first
+end
+
+pwd ||= ARGV.last
+
+if pwd
 	if pwd =~ /^file:\/\//
 		pwd = $'
 	end
 
 	unless File.exists?(pwd)
-		pwd = nil
+		abort "ranger: No such file or directory: #{pwd}"
 	end
-
-else
-	pwd = nil
 end
 
+
 ##--------------------------------------------------
 # require files
 
@@ -80,7 +104,7 @@ opt = {
 	:debug_level            => 0,
 	:debug_file             => '/tmp/errorlog',
 	:colorscheme            => 'default',
-	:cd                     => cd,
+	:cd                     => true,
 	:evil                   => false
 }
 
@@ -108,6 +132,10 @@ rescue LoadError
 	abort "Can't find colorscheme at #{path}"
 end
 
+if defined? arg_debug
+	Option.debug_level = 3
+end
+
 
 include Debug
 
@@ -116,7 +144,7 @@ Debug.setup( :name   => 'ranger',
              :level  => Option.debug_level )
 
 ## run the file in arg1 and exit
-if pwd and !ARGV.empty? and !File.directory?(pwd)
+if pwd and !File.directory?(pwd)
 	Fm.reload_types
 	file = Directory::Entry.new(pwd)
 	file.get_data