Class: Parabot::CLI::ArgumentParser

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

Overview

Handles parsing of command line arguments and options

Instance Method Summary collapse

Instance Method Details

#extract_options(args) ⇒ Object



7
8
9
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
44
45
46
47
48
49
50
51
52
53
# File 'lib/parabot/cli/argument_parser.rb', line 7

def extract_options(args)
  options = {}

  i = 0
  while i < args.length
    case args[i]
    when "--dry-run"
      options[:dry_run] = true
      i += 1
    when "--force"
      options[:force] = true
      i += 1
    when "--config", "-c"
      if i + 1 < args.length && !option?(args[i + 1])
        options[:config] = args[i + 1]
        i += 2
      else
        i += 1
      end
    when "--language", "-l"
      if i + 1 < args.length && !option?(args[i + 1])
        options[:language] = args[i + 1]
        i += 2
      else
        i += 1
      end
    when "--timeout", "-t"
      if i + 1 < args.length && !option?(args[i + 1])
        options[:timeout] = args[i + 1].to_i
        i += 2
      else
        i += 1
      end
    when "--project-root", "-p"
      if i + 1 < args.length && !option?(args[i + 1])
        options[:project_root] = args[i + 1]
        i += 2
      else
        i += 1
      end
    else
      i += 1
    end
  end

  options
end

#help_requested?(args) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/parabot/cli/argument_parser.rb', line 94

def help_requested?(args)
  args.include?("--help") || args.include?("-h") || args.include?("help")
end

#only_options?(args) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/parabot/cli/argument_parser.rb', line 87

def only_options?(args)
  # Check if all arguments are options (start with -) or option values
  # This handles cases like ["--dry-run", "--project-root", "/path"]
  remaining_args = remove_options_and_values(args, nil)
  remaining_args.empty?
end

#remove_options_and_values(args, command) ⇒ Object



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
# File 'lib/parabot/cli/argument_parser.rb', line 55

def remove_options_and_values(args, command)
  result = []
  i = 0

  while i < args.length
    arg = args[i]

    # Skip the command itself (if provided)
    if command && arg == command
      i += 1
      next
    end

    # Skip option flags and their values
    case arg
    when "--dry-run"
      i += 1  # Skip just the flag
    when "--config", "-c", "--language", "-l", "--timeout", "-t", "--project-root", "-p"
      i += 2  # Skip flag and its value
    when /^-/
      # Any other option flag - skip just the flag
      i += 1
    else
      # Regular argument - keep it
      result << arg
      i += 1
    end
  end

  result
end