Skip to content

Conversation

svew
Copy link

@svew svew commented Dec 12, 2024

This is a prototype for lazy loading support (ticket #97). The main idea behind this PR is to support commands which are lazy wrappers around another command (as in, will lazily load the real command when needed). This helps reduce unnecessary module importing, by only importing when the command is needed. In practice, this means using the click.Command functions to fetch info about the command, instead of its fields (get_params() call instead of just reading the params field). The changes made were:

  • Added test_lazy_command.py, an interactive test script which runs trogon on a fake CLI that uses lazy loading. Should demonstrate:
    • That trogon is able to fetch the options, arguments, and help of the subcommand because it's being redirected by the LazyCommand implementation's overloaded functions
    • That trogon doesn't load the underlying commands (cmd_1 and cmd_2) until the user tries to view them in the app
  • Modified CommandSchema to be an abstract class instead of a data class, so that we can write custom properties in derived classes
  • Added a new ClickCommandSchema class inheriting CommandSchema, which lazily provides the values of its properties using almost identical logic to the below introspect_click_app() function which used to construct CommandSchema objects on its own
  • click.Context object is required for many of the click.Command function calls, so passed that object from the creation of the Trogon app down into the creation of ClickCommandSchema
  • Added optional run_if_no_command: bool argument to main tui() call. Normally if the root command is called, it will show help information for the CLI. If the run_if_no_command flag is enabled, instead of printing the help text, it will run the trogon app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant