Top Description Inners Fields Constructors Methods
org.python.util

pack-priv Class OptionScanner

extends Object
Class Inheritance

A somewhat general-purpose scanner for command options, based on CPython getopt.c.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
OptionScanner.LongSpec

Class representing an argument of the long type, where the whole program argument represents one option, e.g. "--help" or "-version".

Field Summary

Modifier and TypeField and Description
private int
argIndex

Index in argv of the arg currently being processed (or about to be started).

private String[]
args

Original argv passed at reset

pack-priv static final char
ARGUMENT

Return to indicate the next argument is a free-standing argument.

pack-priv static final char
DONE

Return to indicate argument processing is over.

pack-priv static final char
ERROR

Return to indicate option was not recognised.

private final OptionScanner.LongSpec[]
private String
message

Error message (after returning ERROR.

private String
optarg

Option argument (where present for returned option).

private int
optIndex

Character index within the current element of argv (of the next option to process).

private final String
programOpts

Valid options.

Constructor Summary

AccessConstructor and Description
pack-priv
OptionScanner(String[]
command-line arguments (which must not change during scanning)
args
,
String
the one-letter options (with : indicating an option argument
programOpts
,
OptionScanner.LongSpec[]
table of long options (like --help)
longSpec
)

Create the scanner from command-line arguments, and information about the valid options.

Method Summary

Modifier and TypeMethod and Description
pack-priv int
countRemainingArguments()

Number of arguments that remain unprocessed from the original array.

pack-priv char
error(String message, Object... args)

Set the error message as String.format(message, args) and return ERROR.

pack-priv String
getMessage()

Get the error message (when we previously returned ERROR.

pack-priv char

Returns:

next option from command line: the actual character or a code.
getOption
()

Get the next option (as a character), or return a code designating successful or erroneous completion.

pack-priv String
getOptionArgument()

Get the argument of the previously returned option or null if none.

pack-priv String
getWholeArgument()

Get a whole argument (not the argument of an option), for use after ARGUMENT was returned.

pack-priv String
peekWholeArgument()

Peek at a whole argument (not the argument of an option), for use after ARGUMENT was returned.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

argIndexback to summary
private int argIndex

Index in argv of the arg currently being processed (or about to be started).

argsback to summary
private String[] args

Original argv passed at reset

ARGUMENTback to summary
pack-priv static final char ARGUMENT

Return to indicate the next argument is a free-standing argument.

DONEback to summary
pack-priv static final char DONE

Return to indicate argument processing is over.

ERRORback to summary
pack-priv static final char ERROR

Return to indicate option was not recognised.

longSpecback to summary
private final OptionScanner.LongSpec[] longSpec
messageback to summary
private String message

Error message (after returning ERROR.

optargback to summary
private String optarg

Option argument (where present for returned option).

optIndexback to summary
private int optIndex

Character index within the current element of argv (of the next option to process).

programOptsback to summary
private final String programOpts

Valid options. ':' means expect an argument following.

Constructor Detail

OptionScannerback to summary
pack-priv OptionScanner(String[] args, String programOpts, OptionScanner.LongSpec[] longSpec)

Create the scanner from command-line arguments, and information about the valid options.

Parameters
args:String[]

command-line arguments (which must not change during scanning)

programOpts:String

the one-letter options (with : indicating an option argument

longSpec:OptionScanner.LongSpec[]

table of long options (like --help)

Method Detail

countRemainingArgumentsback to summary
pack-priv int countRemainingArguments()

Number of arguments that remain unprocessed from the original array.

errorback to summary
pack-priv char error(String message, Object... args)

Set the error message as String.format(message, args) and return ERROR.

getMessageback to summary
pack-priv String getMessage()

Get the error message (when we previously returned ERROR.

getOptionback to summary
pack-priv char getOption()

Get the next option (as a character), or return a code designating successful or erroneous completion.

Returns:char

next option from command line: the actual character or a code.

getOptionArgumentback to summary
pack-priv String getOptionArgument()

Get the argument of the previously returned option or null if none.

getWholeArgumentback to summary
pack-priv String getWholeArgument()

Get a whole argument (not the argument of an option), for use after ARGUMENT was returned. This advances the internal state to the next argument.

peekWholeArgumentback to summary
pack-priv String peekWholeArgument()

Peek at a whole argument (not the argument of an option), for use after ARGUMENT was returned. This does not advance the internal state to the next argument.

org.python.util back to summary

pack-priv Class OptionScanner.LongSpec

extends Object
Class Inheritance

Class representing an argument of the long type, where the whole program argument represents one option, e.g. "--help" or "-version". Such options must start with a '-'. The client supplies an array of LongSpec objects to the constructor to define the valid cases. Long options are recognised before single-letter options are looked for. Note that "-" itself is treated as a long option (even though it is quite short), returning OptionScanner#ERROR if not explicitly defined as a LongSpec.

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
pack-priv final String
pack-priv final char

Constructor Summary

AccessConstructor and Description
public
LongSpec(String
to match
key
,
char
to return when that matches
returnValue
,
boolean
an argument to the option is expected to follow
hasArgument
)

Define that the long argument should return a given char value in calls to OptionScanner#getOption(), and whether or not an option argument should appear following it on the command line.

public
LongSpec(String key, char returnValue)

The same as LongSpec(key, returnValue, false).

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hasArgumentback to summary
pack-priv final boolean hasArgument
keyback to summary
pack-priv final String key
returnValueback to summary
pack-priv final char returnValue

Constructor Detail

LongSpecback to summary
public LongSpec(String key, char returnValue, boolean hasArgument)

Define that the long argument should return a given char value in calls to OptionScanner#getOption(), and whether or not an option argument should appear following it on the command line. This character value need not be the same as any single-character option, and may be OptionScanner#DONE (typically for the key "--".

Parameters
key:String

to match

returnValue:char

to return when that matches

hasArgument:boolean

an argument to the option is expected to follow

LongSpecback to summary
public LongSpec(String key, char returnValue)

The same as LongSpec(key, returnValue, false).