-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix deprecation in SampleMain.java #147
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import static org.kohsuke.args4j.ExampleMode.ALL; | ||
import org.kohsuke.args4j.Argument; | ||
import org.kohsuke.args4j.CmdLineException; | ||
import org.kohsuke.args4j.CmdLineParser; | ||
import org.kohsuke.args4j.Option; | ||
import org.kohsuke.args4j.OptionHandlerFilter; | ||
import org.kohsuke.args4j.ParserProperties; | ||
import org.kohsuke.args4j.spi.BooleanOptionHandler; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* Sample program that shows how you can use args4j. | ||
* | ||
|
@@ -49,40 +49,25 @@ public static void main(String[] args) throws IOException { | |
} | ||
|
||
public void doMain(String[] args) throws IOException { | ||
CmdLineParser parser = new CmdLineParser(this); | ||
|
||
|
||
// if you have a wider console, you could increase the value; | ||
// here 80 is also the default | ||
parser.setUsageWidth(80); | ||
ParserProperties properties = ParserProperties.defaults().withUsageWidth(80); | ||
CmdLineParser parser = new CmdLineParser(this, properties); | ||
|
||
try { | ||
// parse the arguments. | ||
parser.parseArgument(args); | ||
|
||
// you can parse additional arguments if you want. | ||
// parser.parseArgument("more","args"); | ||
|
||
// after parsing arguments, you should check | ||
// if enough arguments are given. | ||
if( arguments.isEmpty() ) | ||
throw new CmdLineException(parser,"No argument is given"); | ||
|
||
} catch( CmdLineException e ) { | ||
// if there's a problem in the command line, | ||
// you'll get this exception. this will report | ||
// an error message. | ||
System.err.println(e.getMessage()); | ||
System.err.println("java SampleMain [options...] arguments..."); | ||
// print the list of available options | ||
parser.printUsage(System.err); | ||
System.err.println(); | ||
|
||
// print option sample. This is useful some time | ||
System.err.println(" Example: java SampleMain"+parser.printExample(ALL)); | ||
|
||
} catch (CmdLineException e) { | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
if( arguments.isEmpty() ) | ||
System.out.println("No args given. Example useage: java SampleMain" + parser.printExample(OptionHandlerFilter.ALL)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably go to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the help message on parsing error was a mistake, thanks for correcting that. The second change depends on whether the program should run given no arguments. However, programs that use args4j will likely require some arguments to run correctly. I agree with this change as well. |
||
|
||
// this will redirect the output to the specified output | ||
System.out.println(out); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The printing of the help message in case of a
CmdLineException
(malformed CLI options) is completely lost which was a good example in my humble opinion: