Processing of empty args with required parameters AND help=true for one of the declared parameters


If you are using JCommander to parse your commander, you may encounter the situations shown below:

You have an option that is required. For example:

@Parameter(names = { "-i", "--input-file"}, description = "Input file [.bed]", required = true)
private String input;
If you run your program without args (empty args) using SortBedErr.java, you will get the error message shown below:

Exception in thread "main" com.beust.jcommander.ParameterException: The following option is required: -i, --input-file 
 at com.beust.jcommander.JCommander.validateOptions(JCommander.java:321)
 at com.beust.jcommander.JCommander.parse(JCommander.java:283)
 at com.beust.jcommander.JCommander.parse(JCommander.java:265)
 at com.readbio.smRNACluster.bedtools.SortBed.main(SortBed.java:22)

To sovle this problem, you can catch ParameterException when the args is empty and print usage using usage() method. You can look into SortBed.java and get the detailed codes.
If there is no args, the output will be:
 The following option is required: -i, --input-file 

Usage: <main class> [options]
  Options:
    -h, -help, --help
       
       Default: false
  * -i, --input-file
       Input file [.bed]
    -o, --output
       Output file

References:
https://groups.google.com/forum/#!topic/jcommander/Um6Q8aaIrv8
https://github.com/cbeust/jcommander/issues/149
http://jcommander.org/


Comments

Popular posts from this blog

gspread error:gspread.exceptions.SpreadsheetNotFound

Miniconda installation problem: concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

P and q values in RNA Seq