Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 Tatsuhiro Tsujikawa
   *
   * Permission is hereby granted, free of charge, to any person
   * obtaining a copy of this software and associated documentation
   * files (the "Software"), to deal in the Software without
   * restriction, including without limitation the rights to use, copy,
   * modify, merge, publish, distribute, sublicense, and/or sell copies
   * of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be
  * included in all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
 package net.sourceforge.argparse4j.inf;
 
 import java.util.List;
 import java.util.Map;
 

This interface defines behavior of ArgumentParser.

The typical usage is set description using description(java.lang.String) and add arguments using addArgument(java.lang.String[]). To add sub-command, first call addSubparsers() to obtain Subparsers object. Subparsers object provides necessary methods to add sub-commands. To make a conceptual group of arguments, first call addArgumentGroup(java.lang.String) to create ArgumentGroup object. And add argument to that group using ArgumentGroup.addArgument(java.lang.String[]) . To parse command-line arguments, call parseArgs(java.lang.String[]) or several overloaded methods.

 
 public interface ArgumentParser {

    

Creates new Argument object and adds to this parser and returns the object.

The nameOrFlags is either a single name of positional argument or a list of option strings for optional argument, e.g. foo or -f, --foo.

Parameters:
nameOrFlags A name or a list of option strings of new Argument.
Returns:
Argument object.
 
     Argument addArgument(String... nameOrFlags);

    

Creates new ArgumentGroup object and adds to this parser and returns the object.

The title is printed in help message as a title of this group. ArgumentGroup provides a way to conceptually group up command line arguments.

Parameters:
title The title printed in help message.
Returns:
ArgumentGroup object.
 
     ArgumentGroup addArgumentGroup(String title);

    

Returns Subparsers.

The method name is rather controversial because repeated call of this method does not add new Subparsers object. Instead, this method always returns same Subparsers object. Subparsers object provides a way to add sub-commands.

Returns:
Subparsers object.
 
     Subparsers addSubparsers();

    
Sets the text to display before the argument help.

Parameters:
description The text to display before the argument help.
Returns:
this
    ArgumentParser description(String description);

    
Sets the text to display after the argument help.

Parameters:
epilog The text to display after the argument help.
Returns:
this
    ArgumentParser epilog(String epilog);

    

Sets version string.

It will be displayed printVersion(). Please note that given version string is displayed as is: no text-wrapping will be made.

Parameters:
version The version string.
Returns:
this
    ArgumentParser version(String version);

    

If defaultHelp is true, the default values of arguments are printed in help message.

By default, the default values are not printed in help message.

Parameters:
defaultHelp Switch to display the default value in help message.
Returns:
this
    ArgumentParser defaultHelp(boolean defaultHelp);

    
Prints help message in stdout.
    void printHelp();

    
Prints help message in writer.

Parameters:
writer Writer to print message.
    void printHelp(PrintWriter writer);

    
Returns help message.

Returns:
The help message.
    String formatHelp();

    
Print a brief description of how the program should be invoked on the command line in stdout.
    void printUsage();

    
Print a brief description of how the program should be invoked on the command line in writer.

Parameters:
writer Writer to print message.
    void printUsage(PrintWriter writer);

    
Returns a brief description of how the program should be invoked on the command line.

Returns:
Usage text.
    String formatUsage();

    
Prints version string in stdout.
    void printVersion();

    
Prints version string in writer.

Parameters:
writer Writer to print version string.
    void printVersion(PrintWriter writer);

    
Returns version string.

Returns:
The version string.
    String formatVersion();

    

Sets parser-level default value of attribute dest.

The parser-level defaults always override argument-level defaults.

Parameters:
dest The attribute name.
value The default value.
Returns:
this
    ArgumentParser setDefault(String destObject value);

    

Sets parser-level default values from attrs.

All key-value pair in attrs are registered to parser-level defaults. The parser-level defaults always override argument-level defaults.

Parameters:
attrs The parser-level default values to add.
Returns:
this
    ArgumentParser setDefaults(Map<StringObjectattrs);

    

Returns default value of given dest.

Returns default value set by Argument.setDefault(java.lang.Object), setDefault(java.lang.String,java.lang.Object) or setDefaults(java.util.Map). Please note that while parser-level defaults always override argument-level defaults while parsing, this method examines argument-level defaults first. If no default value is found, then check parser-level defaults. If no default value is found, returns null.

Parameters:
dest The attribute name of default value to get.
Returns:
The default value of given dest.
    Object getDefault(String dest);

    

Parses command line arguments.

The resulted attributes are returned as Namespace object. This method must not alter the status of this parser and can be called multiple times.

Parameters:
args Command line arguments.
Returns:
Namespace object.
Throws:
ArgumentParserException If an error occurred.
    Namespace parseArgs(String args[]) throws ArgumentParserException;

    

Parses command line arguments.

Unlike parseArgs(java.lang.String[]), which returns Namespace object, this method stores attributes in given attrs.

Parameters:
args Command line arguments.
attrs Map object to store attributes.
Throws:
ArgumentParserException If an error occurred.
    void parseArgs(String[] argsMap<StringObjectattrs)
            throws ArgumentParserException;

    

Parses command line arguments.

Unlike parseArgs(java.lang.String[]), which returns Namespace object, this method stores attributes in given userData. The location to store value is designated using net.sourceforge.argparse4j.annotation.Arg annotations. User don't have to specify net.sourceforge.argparse4j.annotation.Arg for all attributes: the missing attributes are just skipped. This method performs simple java.util.List to generic array conversion. For example, user can assign List<Integer> attribute to generic array int[].

Parameters:
args Command line arguments.
userData Object to store attributes.
Throws:
ArgumentParserException If an error occurred.
    void parseArgs(String[] argsObject userData)
            throws ArgumentParserException;

    

Parses command line arguments.

This is a combination of parseArgs(java.lang.String[],java.util.Map) and parseArgs(java.lang.String[],java.lang.Object). The all attributes will be stored in attrs. The attributes specified in net.sourceforge.argparse4j.annotation.Arg annotations will be also stored in userData.

Parameters:
args Command line arguments.
attrs Map to store attributes.
userData Object to store attributes.
Throws:
ArgumentParserException If an error occurred.
    void parseArgs(String[] argsMap<StringObjectattrsObject userData)
            throws ArgumentParserException;

    

Prints usage and error message.

Please note that this method does not terminate the program.

Parameters:
e Error thrown by parseArgs(java.lang.String[]).
New to GrepCode? Check out our FAQ X