-
Notifications
You must be signed in to change notification settings - Fork 128
How to run command after initialisation #1415
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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.
Beta Was this translation helpful? Give feedback.