Class: Iriq::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/iriq/cli.rb

Overview

Tiny CLI wrapper around the public API. Construct with explicit IO so specs can run it without shelling out.

Constant Summary collapse

COMMANDS =
%w[parse normalize explain classify cluster help version].freeze
USAGE =
<<~TXT
  Usage: iriq <command> [options] [args]

  Commands:
    parse <input>          Parse an identifier and print its fields
    normalize <input>      Print the shape-normalized form
    explain <input>        Annotate each path segment
    classify <segment>     Classify a single segment
    cluster [file]         Cluster identifiers from FILE or stdin (one per line)
    help                   Show this message
    version                Print version

  Options:
    -j, --json             Emit JSON instead of human-readable output
    -h, --help             Show this message

  Examples:
    iriq parse https://foo.com/users/123
    iriq normalize foo.com/users/456
    echo "https://foo.com/users/1\\nhttps://foo.com/users/2" | iriq cluster
TXT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr) ⇒ CLI

Returns a new instance of CLI.



34
35
36
37
38
# File 'lib/iriq/cli.rb', line 34

def initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr)
  @stdin  = stdin
  @stdout = stdout
  @stderr = stderr
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



32
33
34
# File 'lib/iriq/cli.rb', line 32

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



32
33
34
# File 'lib/iriq/cli.rb', line 32

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



32
33
34
# File 'lib/iriq/cli.rb', line 32

def stdout
  @stdout
end

Instance Method Details

#run(argv) ⇒ Object

Returns an integer exit code.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/iriq/cli.rb', line 41

def run(argv)
  args, opts = parse_options(argv)

  cmd = args.shift
  return print_usage(stdout, 0) if cmd.nil? || cmd == "help" || opts[:help]

  unless COMMANDS.include?(cmd)
    stderr.puts "iriq: unknown command #{cmd.inspect}"
    print_usage(stderr, 1)
    return 1
  end

  send("cmd_#{cmd}", args, opts)
rescue Iriq::ParseError => e
  stderr.puts "iriq: parse error: #{e.message}"
  2
rescue OptionParser::ParseError => e
  stderr.puts "iriq: #{e.message}"
  1
end