Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

apache commons-cli how to have help coexist with a required parameter?

 
Jordan Thompson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using apache commons-cli (their command line interface.)

From what I can see, if you have a required parameter and only provide the help option, the parser throws an exception looking for the required parameter and there is no way to determine if help was passed in as an option. I have tried different attempts with OptionGroup, to no avail.
Here is my code:



Here is what I get with only the -h:
test -h
usage: Note that you can append d,h,m,s to the times to specify durations
of days, hours, minutes, or seconds (default is in seconds).
For example: check_down -H192.168.1.122 -w5h -c10m
The parameters are as follows: -c <crit> -H <host> [-h] [-v] [-w
<warn>]
Missing required options: c, H
-c,--critical <crit> Time to begin alarming critical the host has been
up too long
-H,--hostname <host> Name or IP address of host to check
-h,--help this help info
-v,--version Version of this app
-w,--warning <warn> Time to begin alarming warn a host has been up too
long

If I pass in the required parameter and -h, the code works as expected:

test -h -Htest
usage: Note that you can append d,h,m,s to the times to specify durations
of days, hours, minutes, or seconds (default is in seconds).
For example: check_down -H192.168.1.122 -w5h -c10m
The parameters are as follows: [-c <crit>] -H <host> [-h] [-v] [-w
<warn>]
-c,--critical <crit> Time to begin alarming critical the host has been
up too long
-H,--hostname <host> Name or IP address of host to check
-h,--help this help info
-v,--version Version of this app
-w,--warning <warn> Time to begin alarming warn a host has been up too
long


I am looking for a way to be able to pass in only a -h and be able to nicely print out only the help.
Thanks for your help,
Jordan
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Other Open Source Products.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic