Skip to content

Commit c54d744

Browse files
committed
option to provide parent as cli arg
1 parent 3eac217 commit c54d744

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

lib/cli.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ def initialize(args)
99
# helps "gets"
1010
# https://stackoverflow.com/a/2166914
1111
ARGV.clear
12+
end
1213

14+
def run
1315
parent = Controller.base_pvp
1416

1517
# # interactive picker is not polished yet but works
1618
# # there should also be a non interactive version
1719
# # by passing like controller_name:parent_name as cli arg
1820
# parent = pick_item(Controller.parents).value
1921

22+
if @args[:parent]
23+
parent = item_by_name(Controller.parents, @args[:parent]).value
24+
options = Controller.parents.map(&:name).join(', ')
25+
raise "Parent controller '#{@args[:parent]}' not found. Options: #{options}" if parent.nil?
26+
end
27+
2028
mode = Gamemode.new(
2129
name: @args[:name],
2230
parent: parent
@@ -56,6 +64,11 @@ def pick_item(items)
5664
end
5765
end
5866

67+
# TODO: move these item methods to the item.rb file? idk
68+
def item_by_name(items, name)
69+
items.find { |item| item.name == name }
70+
end
71+
5972
## TODO: add unit tests once the args are finalized
6073
def parse_args(args)
6174
@args = {}
@@ -74,7 +87,14 @@ def parse_args(args)
7487
else
7588
raise "Unexpected argument '#{arg}'" unless @args[:name].nil?
7689

77-
@args[:name] = arg
90+
name = arg
91+
if name.include?(':')
92+
parts = name.split(':')
93+
@args[:name] = parts.shift
94+
@args[:parent] = parts.join(':')
95+
else
96+
@args[:name] = name
97+
end
7898
end
7999
end
80100

@@ -86,4 +106,4 @@ def validate_args
86106
end
87107
end
88108

89-
Cli.new(ARGV)
109+
Cli.new(ARGV).run

0 commit comments

Comments
 (0)