forked from ladybug-tools/ladybug
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Implement a standard for running CLI functions
This commit also includes a new commandutil module that helps format CLI-like inputs for the command.
- Loading branch information
1 parent
041986a
commit 6a3ca6b
Showing
3 changed files
with
157 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# coding=utf-8 | ||
"""Utility functions for running the functions of CLI commands outside of the CLI.""" | ||
|
||
|
||
def run_command_function(command_function, arguments=None, options=None): | ||
"""Run a function used within the Ladybug Tools CLI. | ||
Args: | ||
command_function: The CLI function to be executed. | ||
arguments: A list of required arguments for the command_function. None can | ||
be used if the command function has no required arguments. (Default: None). | ||
options: A dictionary of options to be passed to the function. The keys | ||
of this dictionary should be the full names of the options or flags | ||
in the CLI and formatted with dashes (eg. --output-file). The values | ||
of the dictionary should be the values to be passed for each of | ||
the options. For the case of flag arguments, the value should simply | ||
be an empty string. None can be used here to indicate that all default | ||
values for options should be used. (Default: None). | ||
""" | ||
# process the arguments and options | ||
args = () if arguments is None else arguments | ||
kwargs = {} | ||
if options is not None: | ||
for key, val in options.items(): | ||
clean_key = key[2:] if key.startswith('--') else key | ||
clean_key = clean_key.replace('-', '_') | ||
clean_val = True if val == '' else val | ||
kwargs[clean_key] = clean_val | ||
|
||
# run the command using arguments and keyword arguments | ||
return command_function(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters