Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to run command after initialisation #1415

Answered by kotfu
KelvinChung2000 asked this question in Q&A
Discussion options

I know I can do commands at invocation based on https://cmd2.readthedocs.io/en/2.5.9/features/startup_commands/#commands-at-invocation

However, I need to use argparse before starting the CLI, so I will need to do allow_cli_args=False. I want the CLI to work something like python3 ./app.py -p "cmd1; cmd2". After calling it, the shell will run the command and exit automatically.

My current workaround is to append the commands to the self._startup_commands, which runs them. However, this led to a problem: it will continue executing cmd2 even if cmd1 fails, which is not ideal.

What do you think is the best way to implement such a feature?

You must be logged in to vote

I've done exactly this in my cmd2 based app. Here's the code: https://github.com/tomcatmanager/tomcatmanager/blob/main/src/tomcatmanager/__main__.py#L174

My version doesn't allow multiple commands, but you could easily add that by checking the return value of the onecmd_plus_hooks() call. If true, you shouldn't run subsequent commands.

Replies: 1 comment

Comment options

I've done exactly this in my cmd2 based app. Here's the code: https://github.com/tomcatmanager/tomcatmanager/blob/main/src/tomcatmanager/__main__.py#L174

My version doesn't allow multiple commands, but you could easily add that by checking the return value of the onecmd_plus_hooks() call. If true, you shouldn't run subsequent commands.

You must be logged in to vote
0 replies
Answer selected by KelvinChung2000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /