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

Per-command clean-up registration #1197

Open
anselor opened this issue Feb 1, 2022 · 1 comment
Open

Per-command clean-up registration #1197

anselor opened this issue Feb 1, 2022 · 1 comment
Assignees

Comments

@anselor
Copy link
Contributor

anselor commented Feb 1, 2022

Currently the only way to handle a SIGINT is overriding sigint_handler() in the cmd2.Cmd main class.

We now have CommandSets, allowing different sets of functionality to be written in relative isolation from each other. What would be useful would be a way to optionally register a custom cleanup function for a command in the event of an interrupt.

Thinking an optional decorator on a command that points to another method or function that can perform custom cleanup. This allows the cleanup for a command in a CommandSet to be located in the CommandSet.

@kmvanbrunt
Copy link
Member

On a related note, cmd2 does provide a way for SIGINT protection during sections of code that need to fully run to avoid putting the app in an unstable state.

Using self.sigint_protection you can do stuff like:

try:
    # Get SIGINT protection while we set up the environment
    with self.sigint_protection:
        setup_step1()
        setup_step2()
        setup_step3()

    # Do stuff now that the environment is set up
     ...
finally:
    # Get SIGINT protection while we tear down the environment
    with self.sigint_protection:
        teardown_step1()
        teardown_step2()
        teardown_step3()

See cmd2.py for actual uses.

When self.sigint_protection is set, cmd2.Cmd.sigint_handler() won't raise a KeyboardInterrupt.

anselor added a commit that referenced this issue Sep 25, 2023
The sigint_handler() in Cmd will now delegate to the CommandSet.

Addresses #1197
anselor added a commit that referenced this issue Sep 26, 2023
The sigint_handler() in Cmd will now delegate to the CommandSet.

Addresses #1197
@anselor anselor self-assigned this Sep 26, 2023
anselor added a commit that referenced this issue Sep 27, 2023
The sigint_handler() in Cmd will now delegate to the CommandSet.

Addresses #1197
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants