Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the application run/stop cycle #45

Open
facundobatista opened this issue Nov 30, 2021 · 1 comment
Open

Simplify the application run/stop cycle #45

facundobatista opened this issue Nov 30, 2021 · 1 comment
Labels
triaged Will be worked on at some point

Comments

@facundobatista
Copy link
Contributor

The lib should provide a way to handle the application bootstrapping and finishing (in several ways: ok, error, etc).

The code is already in Charmcraft, we need to move it in here.

@facundobatista
Copy link
Contributor Author

More thinking about this.

I think we should create a context manager, something to be used like:

with craft_cli.app_wrapper(...) as dispatcher:
    dispatcher.pre_parse_args(sys.argv[1:])
    dispatcher.load_command(None)
    dispatcher.run()
sys.exit(dispatcher.return_code)

The app_wrapper signature would be:

def app_wrapper(
    app_name: str, 
    greeting_msg: str, 
    command_groups: List[CommandGroup], 
    *, 
    summary: str = "",
    init_emitter_mode: Optional[EmitterMode] = EmitterMode.NORMAL, 
    log_filepath: Optional[pathlib.Path] = None,
    extra_global_args: Optional[List[GlobalArgument]] = None,
)

The context manager would:

  • init emit with proper values
  • instantiate Dispatcher with proper values and return it as the object to be used
  • "hide" the handling of specific ArgumentParsingError and ProvideHelpException; for these both cases it would automatically show messages and cut further execution, leaving dispatcher.return_code with the proper value
  • also hide catching CraftError, KeyboardInterrupt and generic Exception, doing the right thing in each case
  • provide a simpler and direct to use return code

Improvements:

  • Much simpler to use
  • Less error prone as no comple structure is needed just for the lib to work
  • No need to expose internal exceptions in the simple usage at least: ArgumentParsingError should remain public because it's handy for the command to expose a complicated argument parsing failure, but it doesn't need to be presented to the developer the first time she reads about this lib; ProvideHelpException could be made private (OTOH is ok to leave it public if the developer needs to fallback into a more "manual" usage of the lib because of advance needs).
  • No more manual "print"s

@facundobatista facundobatista added the triaged Will be worked on at some point label Mar 9, 2022
tigarmo pushed a commit that referenced this issue Jun 6, 2023
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triaged Will be worked on at some point
Projects
None yet
Development

No branches or pull requests

1 participant