Skip to content

Commit

Permalink
Improve self-documentation of 3llo by adding a --help switch, show …
Browse files Browse the repository at this point in the history
…errors without confusing tracebacks
  • Loading branch information
Ma27 committed Jul 28, 2019
1 parent 0310c8a commit 855ffd1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
29 changes: 23 additions & 6 deletions bin/3llo
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,29 @@ $container.register(:api_client, Tr3llo::HTTP::Client)
prompt = TTY::Prompt.new
$container.register(:interface, Tr3llo::Interface.new(prompt, $stdout))

def print_help(flavour)
Tr3llo::Presenter::HelpPresenter
.new($container.resolve(:interface), flavour)
.print!
end

unless ARGV.empty?
if ARGV[0] =~ /^-h|--help$/
print_help(Tr3llo::Presenter::FLAVOUR_HELP_CLI)
exit
end
end

configuration = Tr3llo::Configuration.new
configuration.user_id = ENV.fetch('TRELLO_USER') { raise "Have you set TRELLO_USER?" }
configuration.api_key = ENV.fetch('TRELLO_KEY') { raise "Have you set TRELLO_KEY?" }
configuration.api_token = ENV.fetch('TRELLO_TOKEN') { raise "Have you set TRELLO_TOKEN?" }
begin
configuration.user_id = ENV.fetch('TRELLO_USER') { raise "Have you set TRELLO_USER?" }
configuration.api_key = ENV.fetch('TRELLO_KEY') { raise "Have you set TRELLO_KEY?" }
configuration.api_token = ENV.fetch('TRELLO_TOKEN') { raise "Have you set TRELLO_TOKEN?" }
rescue => error
puts "\e[31mInvalid configuration: \e[1m#{error.message}\e[0m"
exit!
end

configuration.finalize!

$container.register(
Expand All @@ -24,8 +43,6 @@ $container.register(
user = Tr3llo::API::User.find($container.resolve(:configuration).user_id)
$container.register(:user, user)

Tr3llo::Presenter::HelpPresenter
.new($container.resolve(:interface))
.print!
print_help(Tr3llo::Presenter::FLAVOUR_HELP_INDENTED)

Tr3llo::Controller.new.start
12 changes: 10 additions & 2 deletions lib/3llo/presenter/help.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
module Tr3llo
module Presenter
FLAVOUR_HELP_INDENTED = 1
FLAVOUR_HELP_CLI = 2

class HelpPresenter
def initialize(interface)
def initialize(interface, flavour = FLAVOUR_HELP_INDENTED)
@interface = interface
@flavour = flavour
end

def print!
interface.print_frame do
interface.puts menu_text
text = menu_text
if @flavour == FLAVOUR_HELP_CLI
text = text.split("\n").map(&:lstrip).drop(1).join("\n")
end
interface.puts text
end
end

Expand Down

0 comments on commit 855ffd1

Please sign in to comment.