Skip to content

Commit

Permalink
Allow multiple engine options per "-o" CLI argument
Browse files Browse the repository at this point in the history
Re: #375.

This reverts commit 62b1ed9.
  • Loading branch information
drmfinlay committed Mar 21, 2023
1 parent 2681aa2 commit 6898c8d
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions dragonfly/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ def _set_logging_level(args):


def _init_engine(args):
# Retrieve the engine option pairs from the arguments. The retrieval
# of engine options is complex because multiple arguments are allowed.
# Retrieve the engine option pairs from the arguments.
options = {}
for argument in args.engine_option:
for argument in args.engine_options:
for options_list in argument:
for option, value in options_list:
options[option] = value
Expand Down Expand Up @@ -282,31 +281,27 @@ def _smart_cast(value):
return value


def _engine_option_string(string):
def _engine_options_string(string):
if '=' not in string:
msg = "Invalid engine option: %r" % string
raise argparse.ArgumentTypeError(msg)

# Return a list of key/value arguments separated by commas or spaces.
options = []
sub_strings = [
s for s in re.split('[,\\s]', string)
if len(s) > 0 # Filter out empty strings.
]
if len(sub_strings) > 1:
msg = "Invalid engine option: %r" % string
raise argparse.ArgumentTypeError(msg)
for sub_string in re.split('[,\\s]', string):
if not sub_string: # Filter out empty strings.
continue

# There must be one valid engine option.
sub_string = sub_strings[0]
parts = sub_strings[0].split('=')
if len(parts) != 2 or not (parts[0] and parts[1]):
msg = "Invalid engine option: %r" % sub_string
raise argparse.ArgumentTypeError(msg)
# There must be one valid engine option per sub-string.
parts = sub_string.split('=')
if len(parts) != 2 or not (parts[0] and parts[1]):
msg = "Invalid engine option: %r" % sub_string
raise argparse.ArgumentTypeError(msg)

arg = parts[0]
value = _smart_cast(parts[1])
options.append((arg, value))

arg = parts[0]
value = _smart_cast(parts[1])
options.append((arg, value))
return options


Expand Down Expand Up @@ -358,15 +353,17 @@ def make_arg_parser():
"files", metavar="file", nargs="*", type=_valid_filename_or_pattern,
help="Command module file(s)."
)
engine_option_argument = _build_argument(
"-o", "--engine-option", default=[], nargs="+", action="append",
type=_engine_option_string,

engine_options_argument = _build_argument(
"-o", "--engine-options", default=[], nargs="+", action="append",
type=_engine_options_string,
help="One or more engine options to be passed to *get_engine()*. "
"Each option should specify a key word argument and value. "
"Multiple options should be separated by spaces or commas. "
"Values are treated as Python literals if possible, "
"otherwise as strings."
)

language_argument = _build_argument(
"--language", default="en",
help="Speaker language to use. Only applies if using an engine "
Expand Down Expand Up @@ -407,7 +404,7 @@ def make_arg_parser():
)
_add_arguments(
parser_test,
cmd_module_files_argument, engine_argument, engine_option_argument,
cmd_module_files_argument, engine_argument, engine_options_argument,
language_argument, no_input_argument, delay_argument,
log_level_argument, quiet_argument
)
Expand All @@ -430,7 +427,7 @@ def make_arg_parser():
)
_add_arguments(
parser_load,
cmd_module_files_argument, engine_argument, engine_option_argument,
cmd_module_files_argument, engine_argument, engine_options_argument,
language_argument, no_input_argument, no_recobs_messages_argument,
log_level_argument, quiet_argument
)
Expand All @@ -454,7 +451,7 @@ def make_arg_parser():
_add_arguments(
parser_load_directory,
module_dirs_argument, recursive_argument, engine_argument,
engine_option_argument, language_argument, no_input_argument,
engine_options_argument, language_argument, no_input_argument,
no_recobs_messages_argument, log_level_argument, quiet_argument
)

Expand Down

0 comments on commit 6898c8d

Please sign in to comment.