Skip to content

Commit 2d315cf

Browse files
committed
Set command line arg parser's default value into instead of
1 parent d3119ef commit 2d315cf

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

interpreter/terminal_interface/start_terminal_interface.py

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -258,50 +258,31 @@ def start_terminal_interface(interpreter):
258258
for arg in arguments:
259259
action = arg.get("action", "store_true")
260260
nickname = arg.get("nickname")
261-
default = arg.get("default")
261+
262+
name_or_flags = [f'--{arg["name"]}']
263+
if nickname:
264+
name_or_flags.append(f"-{nickname}")
262265

263266
if arg["type"] == bool:
264-
if nickname:
265-
parser.add_argument(
266-
f"-{nickname}",
267-
f'--{arg["name"]}',
268-
dest=arg["name"],
269-
help=arg["help_text"],
270-
action=action,
271-
default=default,
272-
)
273-
else:
274-
parser.add_argument(
275-
f'--{arg["name"]}',
276-
dest=arg["name"],
277-
help=arg["help_text"],
278-
action=action,
279-
default=default,
280-
)
267+
parser.add_argument(
268+
*name_or_flags,
269+
dest=arg["name"],
270+
help=arg["help_text"],
271+
action=action,
272+
default=None,
273+
)
281274
else:
282275
choices = arg.get("choices")
283276

284-
if nickname:
285-
parser.add_argument(
286-
f"-{nickname}",
287-
f'--{arg["name"]}',
288-
dest=arg["name"],
289-
help=arg["help_text"],
290-
type=arg["type"],
291-
choices=choices,
292-
default=default,
293-
nargs=arg.get("nargs"),
294-
)
295-
else:
296-
parser.add_argument(
297-
f'--{arg["name"]}',
298-
dest=arg["name"],
299-
help=arg["help_text"],
300-
type=arg["type"],
301-
choices=choices,
302-
default=default,
303-
nargs=arg.get("nargs"),
304-
)
277+
parser.add_argument(
278+
*name_or_flags,
279+
dest=arg["name"],
280+
help=arg["help_text"],
281+
type=arg["type"],
282+
choices=choices,
283+
default=None,
284+
nargs=arg.get("nargs"),
285+
)
305286

306287
args = parser.parse_args()
307288

@@ -313,7 +294,7 @@ def start_terminal_interface(interpreter):
313294
open_storage_dir("models")
314295
return
315296

316-
if args.reset_profile != "NOT_PROVIDED":
297+
if args.reset_profile is not None and args.reset_profile != "NOT_PROVIDED":
317298
reset_profile(
318299
args.reset_profile
319300
) # This will be None if they just ran `--reset_profile`
@@ -349,7 +330,7 @@ def start_terminal_interface(interpreter):
349330

350331
### Apply profile
351332

352-
interpreter = profile(interpreter, args.profile)
333+
interpreter = profile(interpreter, args.profile or get_argument_dictionary(arguments, "profile")["default"])
353334

354335
### Set attributes on interpreter, because the arguments passed in via the CLI should override profile
355336

@@ -418,9 +399,7 @@ def start_terminal_interface(interpreter):
418399
def set_attributes(args, arguments):
419400
for argument_name, argument_value in vars(args).items():
420401
if argument_value is not None:
421-
argument_dictionary = [a for a in arguments if a["name"] == argument_name]
422-
if len(argument_dictionary) > 0:
423-
argument_dictionary = argument_dictionary[0]
402+
if argument_dictionary := get_argument_dictionary(arguments, argument_name):
424403
if "attribute" in argument_dictionary:
425404
attr_dict = argument_dictionary["attribute"]
426405
setattr(attr_dict["object"], attr_dict["attr_name"], argument_value)
@@ -431,6 +410,12 @@ def set_attributes(args, arguments):
431410
)
432411

433412

413+
def get_argument_dictionary(arguments: list[dict], key: str) -> dict:
414+
if len(argument_dictionary_list := list(filter(lambda x: x["name"] == key, arguments))) > 0:
415+
return argument_dictionary_list[0]
416+
return {}
417+
418+
434419
def main():
435420
interpreter = OpenInterpreter(import_computer_api=True)
436421
try:

0 commit comments

Comments
 (0)