38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
|
# File 'lib/timesheet/timesheet_parser.rb', line 38
def self.parse(args, outstream=STDOUT)
global_opts = Trollop::options(args) do
version "Timesheet #{Timesheet::VERSION} (c) 2009 John F. Schank III"
banner USAGE
stop_on SUB_COMMANDS
opt :debug, "Show debugging information while processing"
end
if (args.nil? || args.empty?)
Trollop::die "No parameters on command-line, use -h for help"
end
command = args.shift
command_opts = case command
when "add"
add_options = Trollop::options(args) do
banner <<-EOS
timesheet add [OPTIONS]
Adds an entry to the database.
If there is an overlap, it should present a choice to split the entry, or abort.
OPTIONS are:
EOS
opt :project, "Project name", {:type => :string, :required => true}
opt :start, "Start date-time", {:type => :time, :required => true}
opt :end, "End date-time", {:type => :time, :required => true}
opt :comment, "comment", {:type => :string}
depends :start, :end
end
add_options[:command] = :add
add_options
when "edit"
edit_options = Trollop::options(args) do
banner <<-EOS
timesheet edit [OPTIONS]
Allows editing of existing entries
OPTIONS are:
EOS
opt :record_number, "Record Number", {:type => :int, :required => true}
opt :project, "Project name", {:type => :string}
opt :start, "Start date-time", {:type => :time}
opt :end, "End date-time", {:type => :time}
opt :comment, "comment", {:type => :string}
end
edit_options[:command] = :edit
edit_options
when "delete"
delete_options = Trollop::options(args) do
banner <<-EOS
timesheet delete [OPTIONS]
Allows deletion of existing entries
OPTIONS are:
EOS
opt :record_number, "Record Number", {:type => :int, :required => true}
end
delete_options[:command] = :delete
delete_options
when "list"
list_options = Trollop::options(args) do
banner <<-EOS
timesheet list [OPTIONS]
Prints all matching entries. Any entry where the given date is covered by the entry.
Each entry's record number is displayed
OPTIONS are:
EOS
opt :today, "Current Day"
opt :current_week, "Current Week"
opt :current_month, "Current Month"
opt :yesterday, "Yesterday"
opt :last_week, "Last Week"
opt :last_month, "Last Month"
opt :start, "Start date-time", {:type => :time}
opt :end, "End date-time", {:type => :time}
depends :start, :end
conflicts :today, :current_week, :current_month, :yesterday, :last_week, :last_month, :start
end
list_options[:command] = :report
list_options[:detail] = true
TIME_PERIODS.each do |period|
list_options.merge!(convert_to_start_end(period)) if list_options[period]
list_options.delete(period)
end
list_options.merge!(convert_to_start_end(:today)) unless list_options[:start] list_options
when "report"
report_options = Trollop::options(args) do
banner <<-EOS
timesheet report [OPTIONS]
displays total hours by project. For entries in the period.
summary report shows hours spent on top level projects, and excludes comments
detail shows hours for each item.
byday shows hours for each day, with entries and comments. (daily breakout)
defaults: --summary, and --today
OPTIONS are:
EOS
opt :summary, "Summary report"
opt :detail, "Detailed report"
opt :byday, "By Day report"
opt :today, "Current Day"
opt :current_week, "Current Week"
opt :current_month, "Current Month"
opt :yesterday, "Yesterday"
opt :last_week, "Last Week"
opt :last_month, "Last Month"
opt :start, "Start date-time", {:type => :time}
opt :end, "End date-time", {:type => :time}
conflicts :summary, :detail, :byday
conflicts :today, :current_week, :current_month, :yesterday, :last_week, :last_month, :start
depends :start, :end
end
report_options[:command] = :report
TIME_PERIODS.each do |period|
report_options.merge!(convert_to_start_end(period)) if report_options[period]
report_options.delete(period)
end
report_options.merge!(convert_to_start_end(:today)) unless report_options[:start] report_options.merge!({:summary => true}) unless report_options[:summary] || report_options[:detail] || report_options[:byday]
report_options
else
Trollop::die "unknown subcommand #{command.inspect}"
end
opts = {}
opts.merge! global_opts
opts.merge! command_opts
opts.merge!({:remainder => args})
if (opts[:debug] )
outstream.puts "Command-line options hash..."
outstream.puts opts.inspect
end
raise ArgumentError.new("Command was not fully understood. Use --debug flag to diagnose.") unless opts[:remainder].empty?
opts
end
|