Skip to content

Commit d51046d

Browse files
committed
Interactive if no args given
1 parent c54d744 commit d51046d

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

lib/cli.rb

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ def initialize(args)
1313

1414
def run
1515
parent = Controller.base_pvp
16-
17-
# # interactive picker is not polished yet but works
18-
# # there should also be a non interactive version
19-
# # by passing like controller_name:parent_name as cli arg
20-
# parent = pick_item(Controller.parents).value
21-
2216
if @args[:parent]
23-
parent = item_by_name(Controller.parents, @args[:parent]).value
17+
parent = item_by_name(Controller.parents, @args[:parent])
2418
options = Controller.parents.map(&:name).join(', ')
2519
raise "Parent controller '#{@args[:parent]}' not found. Options: #{options}" if parent.nil?
20+
21+
parent = parent.value
2622
end
2723

2824
mode = Gamemode.new(
@@ -55,8 +51,7 @@ def pick_item(items)
5551
items.each_with_index do |item, idx|
5652
puts "#{idx}. #{item.name} - #{item.description}"
5753
end
58-
print '> '
59-
choice = $stdin.gets.to_i
54+
choice = gets_number
6055
item = items[choice]
6156
return item unless item.nil?
6257

@@ -98,11 +93,36 @@ def parse_args(args)
9893
end
9994
end
10095

101-
validate_args
96+
fetch_args_interactive if @args[:name].nil?
10297
end
10398

104-
def validate_args
105-
raise 'Controller name can not be empty' if @args[:name].nil? || @args[:name].empty?
99+
def gets_non_empty
100+
loop do
101+
print '> '
102+
val = $stdin.gets.chomp
103+
return val unless val.empty?
104+
105+
puts 'Value can not be empty. Please try again.'
106+
end
107+
end
108+
109+
def gets_number
110+
loop do
111+
print '> '
112+
val = $stdin.gets.chomp
113+
return val.to_i if val.match?(/^\d+$/)
114+
115+
puts 'Please provide a valid number'
116+
end
117+
end
118+
119+
def fetch_args_interactive
120+
puts 'Choose your controller name'
121+
@args[:name] = gets_non_empty
122+
123+
puts 'Choose your parent controller'
124+
# passing parent as a string if we already have an object is hacky
125+
@args[:parent] = pick_item(Controller.parents).value.name.to_snake
106126
end
107127
end
108128

0 commit comments

Comments
 (0)