Method: Tracking::CLI#parse

Defined in:
lib/tracking/cli.rb

#parseObject

Use option parser to parse command line arguments and run the selected command with its selected options



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/tracking/cli.rb', line 160

def parse
	#options = {}
	done = false

	OptionParser.new do |opts|
		# Setup
		version_path = File.expand_path('../../VERSION', File.dirname(__FILE__))
		opts.version = File.exist?(version_path) ? File.read(version_path) : ''
		# Start of help text
		opts.banner = 'Usage: tracking [mode]'
		opts.separator '                                     display tasks'
		opts.separator '    <task description>               start a new task with the given text (spaces allowed)'
		# Modes
		opts.on('-r', '--rename', 'rename the last task') do
			List.rename ARGV.join(' ').gsub("\t",'')
			display
			done = true
			return
		end
		opts.on('-d', '--delete', 'delete the last task') do
			List.delete
			display
			done = true
			return
		end
		opts.on('-c', '--clear', 'delete all tasks') do
			List.clear
			puts 'List cleared.'
			done = true
			return
		end
		opts.on('-h', '--help', 'display this help information') do
			puts opts
			done = true
			return
		end
	end.parse!

	# Basic modes (display and add)
	unless done
		if ARGV.count == 0
			# Display all tasks
			display
		else
			# Start a new task
			List.add ARGV.join(' ').gsub("\t",'')
			display
		end
	end
end