Sha256: f13d54107def33ba5c5581d5386ea1c0afbad031d34d5c0aab25f22e6080af01

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

#!/usr/bin/ruby
# --- Parse command line ---
#
# Author:	Magnus Engström
# Email:		magnus@gisab.se
# File:		cmdline_parse
#
# Description
# -----------
# Parses the commandline and
# returns a hash with the 
# switch as key and the 
# value as value :)
# --------------------------

class Commandline
  def Commandline::parse()

	 params	= Hash::new()	# Could be useful to have something to put the commandline in ;)
	 key		= false			# Just a really ugly hack to keep track on if we're reading key or value
	 foo		= ''				# Keeps the key name

	 # Iterate through the command line ($*) and put it in the params hash
	 $*.each { |param|
		key = ( key == true ? false : true ) # Negate the value of key
		if(key)
		  foo = param
		else
		  params[foo] = param
		end
	 }

	 # Validate the command line.
	 # Every key should begin with at least one '-' and have a value
	 if( !key ) # If key equals true, then the user should enter one more parameter
		passed = true
		params.each { |key, value| # Iterate through all parameters
		  passed = false if(key[0].chr() != '-') # Doesn't it start with a '-'? Not passed...
		  passed = false if(value == '') # I really want a value here, not just an empty string
		}
	 end

	 # Return params if check passed, else return -1
	 passed ? params : -1

  end
end

Version data entries

8 entries across 8 versions & 4 rubygems

Version Path
rwdaddresses-1.01 extras/cmdline_parse
rwdaddresses-0.99 extras/cmdline_parse
rwdaddresses-0.98 extras/cmdline_parse
rwddemo-0.91 extras/cmdline_parse
rwdschedule-0.98 extras/cmdline_parse
rwdschedule-0.97 extras/cmdline_parse
rwdshell-0.97 extras/cmdline_parse
rwdshell-0.96 extras/cmdline_parse