Suppress calls to System.exit()
Brought to you by:
lglawrence
Hi,
it would be great if I could configure JCmdLine to NOT call System.exit() in any case (not only for
errors via setDieOnParseError( false )).
Maybe something like CmdLineHandler.setDieOnEnd( false ) could be a solution.
In my project I need to do something at the end of the program regardless if the user has called its
functionality or only the help (-?) but the HelpCmdLineHandler calls System.exit() in any case. :-((
Since everything (output of help and exit) is done in one method I can't overwrite something and
change the behaviour. :-((
Thanks,
Alex
Logged In: YES
user_id=642830
The BasicCmdLineHandler class does not call System.exit() at all if DieOnParseError is not set, it is the HelpCmdLineHandler and DefaultCmdLineHandler decorator classes that implement that behavior.
If these classes were modified not to call System.exit(), what would you have them do? Maybe return false, with a special message available through getParseError() (getParseError().equals(CmdLineParser.USER_CHOSE_TO_EXIT)...)? I am not too in favor of returning true because the API states that a return of true implies that all required parameters have been specified, and that would not be the case.
Logged In: YES
user_id=815069
> If these classes were modified not to call System.exit(), what
> would you have them do? Maybe return false, with a special
> message available through getParseError()
> (getParseError().equals(CmdLineParser.
> USER_CHOSE_TO_EXIT)...)?
Don't think that we should handle it like an error of the
parser.
Maybe with a new method like isEndRequested().
> I am not too in favor of returning true because the API states
> that a return of true implies that all required parameters
have
> been specified, and that would not be the case.
That's ok. The existing API should stay the same.
If you introduce a new method then it would look like:
parser.setDieOnEnd( false );
boolean result = parser.parse( args );
if( !result )
{
if( parser.isEndRequested() )
{
// do something
System.exit(0);
}
else
{
// handle error
}
}
else
{
// run the app
}
I think that'll be fine. If an app do not set setDieOnEnd(
false ) everything stays the same, if it sets to true the
above checking has to be done.