Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions args4j/examples/SampleMain.java
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.
*
Expand Down Expand Up @@ -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();
Copy link

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:

           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();

return;
}

if( arguments.isEmpty() )
System.out.println("No args given. Example useage: java SampleMain" + parser.printExample(OptionHandlerFilter.ALL));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably go to System.err, as goes e.printStackTrace()

Copy link
Author

Choose a reason for hiding this comment

The 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);

Expand Down