Class: Readwise::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/readwise/cli.rb,
lib/readwise/cli/base_command.rb,
lib/readwise/cli/command_registry.rb,
lib/readwise/cli/document/create_command.rb

Defined Under Namespace

Modules: Document Classes: BaseCommand, CommandRegistry

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(args = ARGV) ⇒ Object



6
7
8
# File 'lib/readwise/cli.rb', line 6

def self.start(args = ARGV)
  new.start(args)
end

Instance Method Details

#start(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/readwise/cli.rb', line 10

def start(args)
  if args.empty?
    show_help
    exit 1
  end

  if args.first == '--help' || args.first == '-h'
    show_help
    return
  end

  resource = args.shift&.downcase
  action = args.shift&.downcase

  unless resource && action
    puts "Error: Resource and action are required"
    puts "Usage: readwise <resource> <action> [options] [arguments]"
    puts "Run 'readwise --help' for more information"
    exit 1
  end

  command_class = CommandRegistry.find(resource, action)
  unless command_class
    puts "Error: Unknown command '#{resource} #{action}'"
    puts "Run 'readwise --help' to see available commands"
    exit 1
  end

  command = command_class.new
  command.execute(args)
rescue => e
  puts "Unexpected error: #{e.message}"
  exit 1
end