question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Batch mode and exit on error with code not 0

See original GitHub issue

I need to be able to perform one or more commands directly from CLI call and exit on finish. I’ve resolved this need overriding the onecmd_plus_hooks method of my Cmd class to return True in some circumstances and adding a flag to the CLI to set the batch mode:

def onecmd_plus_hooks(self, line):
    stop = Cmd.onecmd_plus_hooks(self, line)
   
    if self.mode == self.MODE_BATCH:
        # if there are commands in queue we exec all before stopping
        if self.cmdqueue:
            return stop
        # queue is empty and we are in batch mode, exit
        return True
    else:
        return stop           

(This works pretty well so if you think is useful I can try to integrate it in the package)

There is a problem anyway… The expected behaviour of a CLI call in batch mode is that the exit code of the whole call is 0 if the command is successful and different from zero if there are exceptions in command execution or if the user press CTRL+C during the execution.

With the current implementation the command always exits with a 0 code. I’m a bit stuck on how to implement this behaviour. I’m trying modifying a little bit the exceptions handling in onecmd_plus_hooks and _cmdloop methods but I don’t know if it is the correct way to get a solution. Do you have any comment/suggestion?

If you think is worth I can push a branch with my attempt

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tleonhardtcommented, Apr 19, 2018

The is some good discussion in PR #359 in relation to this issue.

I think the tentative resolution is that we will leave this issue open and revisit it after Issue #37 is complete and cmd2’s underlying parsing logic has been refactored.

We agree that some sort of standard way of returning a non-zero exit code to the shell is desirable, we just want to be careful about how it is implemented.

0reactions
tleonhardtcommented, Jun 29, 2018

@albertored Hi Alberto,

We are finished with all of the refactoring we wanted to get done before revisiting this issue. Would you like to take another shot at a PR for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my GDB batch mode exit status always 0?
Exit with nonzero status if an error occurs in executing the gdb commands in the command files. But even when there are errors,...
Read more >
Errorlevel - Windows CMD - SS64.com
You can make a batch file return a non-zero exit code by using the EXIT command. Exit 0. Exit /B 5. To force...
Read more >
Batch Script - Return Code - Tutorialspoint
By default when a command line execution is completed it should either return zero when execution succeeds or non-zero when execution fails.
Read more >
Stata batch-mode exit status on error - Statalist
The standard way to do this is to check the return code, but Stata in batch-mode always seems to return an exit status...
Read more >
Batch File Return Code - Explanation And Example
Exit Code /Error Code, Details/Description ; 0, Program successfully executed. ; 1, Incorrect function. Indicates that Action has attempted to execute non- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found