Module: Rtt::CmdLineParser
- Included in:
- Rtt
- Defined in:
- lib/rtt/cmd_line_parser.rb
Constant Summary collapse
- COMMAND_MAPPING =
{ :project => SetProjectCommand, :client => SetClientCommand, :report => ReportCommand, :stop => StopCommand, :start => StartCommand, :rename => RenameCommand, :list => QueryCommand, :pause => PauseCommand, :resume => StartCommand, :user => SetUserCommand, :delete => DeleteCommand, :configure => ConfigureCommand, :export => ExportCommand, :import => ImportCommand }
Instance Method Summary collapse
- #capture(arguments) ⇒ Object
- #env_filters ⇒ Object
- #env_variable(key) ⇒ Object
- #execute(cmds) ⇒ Object
- #execute_single(cmd) ⇒ Object
- #handle_error(e) ⇒ Object
- #puts_usage ⇒ Object
Instance Method Details
#capture(arguments) ⇒ Object
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 |
# File 'lib/rtt/cmd_line_parser.rb', line 72 def capture(arguments) unless arguments.length == 0 operation = arguments.shift.to_sym if COMMAND_MAPPING.keys.include?(operation) klazz = COMMAND_MAPPING[operation] if arguments.length >= klazz::NUMBER_OF_PARAM_REQUIRED command = klazz.new first_argument = arguments.shift if /^--.*$/.match(first_argument) command.name = nil command.optional = [first_argument] else command.name = first_argument command.optional = arguments if arguments.present? end Array(command) end elsif operation.present? command = StartCommand.new command.name = operation command.optional = arguments if arguments.present? Array(command) end else puts_usage end end |
#env_filters ⇒ Object
100 101 102 103 104 105 |
# File 'lib/rtt/cmd_line_parser.rb', line 100 def env_filters [ 'date', 'nickname', 'from', 'to', 'client', 'project' ].inject({}) do |filters, key| filters[key.to_sym] = env_variable(key) if env_variable(key).present? filters end end |
#env_variable(key) ⇒ Object
107 108 109 |
# File 'lib/rtt/cmd_line_parser.rb', line 107 def env_variable(key) ENV[key] || ENV[key.upcase] || ENV[key.downcase] end |
#execute(cmds) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/rtt/cmd_line_parser.rb', line 111 def execute(cmds) cmds.each { |cmd| execute_single(cmd) } say "Operation(s) succeded." rescue => e handle_error(e) end |
#execute_single(cmd) ⇒ Object
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 |
# File 'lib/rtt/cmd_line_parser.rb', line 118 def execute_single(cmd) case cmd when SetProjectCommand client = cmd.optional.shift if cmd.optional.present? && (/^--.*$/.match(cmd.optional.first)).blank? set_project(cmd.name, client) when SetClientCommand set_client(cmd.name) when StartCommand start(cmd.name) when RenameCommand rename(cmd.name) when PauseCommand if current_task.present? pause else raise TaskNotStartedError end when StopCommand stop when ReportCommand = env_filters.merge!(:pdf => cmd.name) report() when QueryCommand list(env_filters) when SetUserCommand set_user(cmd.name) when ImportCommand import(cmd.name) when ExportCommand export(cmd.name) when DeleteCommand name = cmd.name = name.present? ? env_filters.merge!(:name => name) : env_filters delete() when ConfigureCommand case cmd.name.downcase when 'task' update_task(cmd.optional, env_filters) when 'project' name = cmd.next_optional client = cmd.next_optional set_project(name, client, true) when 'client' name = cmd.next_optional set_client(name, true) when 'user' name = cmd.next_optional set_user(name, true) else raise CommandNotFoundError end else raise CommandNotFoundError end end |
#handle_error(e) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/rtt/cmd_line_parser.rb', line 174 def handle_error(e) case e when CommandNotFoundError return puts_usage when TaskNotStartedError say "There is no active task. Pause is not valid at this point." end end |
#puts_usage ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/rtt/cmd_line_parser.rb', line 183 def puts_usage say('') File.open(File.join( File.dirname(__FILE__), '..', '..', "USAGE.txt")) do |file| while content = file.gets say content end end say('') end |